0

I am trying to work through Write Yourself a Scheme in 48 Hours. But I am already running into problems on page 6.

The code provided is:

module Main where
import System.Environment
import Text.ParserCombinators.Parsec hiding ( spaces )

main :: IO ()
main = do 
    args <− getArgs
    putStrLn (readExpr ( args !! 0))

symbol :: Parser Char
symbol = oneOf "!$%&|*+-/:<=?>@^_~#"

readExpr :: String −> String
readExpr input =  case parse symbol "lisp" input of
    Left err −> "No match: " ++ show err
    Right val −> "Found value"

when I run the following command

ghc -o parser simpleparser1.hs

the file is called simpleparser1.hs. I get the following error message. Which I find very strange considering that the code is from a classic book.

[1 of 1] Compiling Main             ( simpleparser1.hs, simpleparser1.o )

simpleparser1.hs:15:5: error:
    Parse error in pattern: Left err −> "No match: " ++ show err
   |
15 |     Left err −> "No match: " ++ show err
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I did look at this , this and this post on stackoverflow. I am a completely unfamiliar with Haskell so any help would be welcome.

HelloWorld
  • 123
  • 5
  • 1
    A good rule of thumb is _never, ever, copy&paste from a PDF_. If the author had intended you to use the code as-is, they would have given you a source file, or just published the whole thing on Hackage so you can import it without even looking at the source code. The point of tutorials like this is that you go through everything in the code as you proceed; a good way to ensure this is to actually type the code yourself into your keyboard. – leftaroundabout Nov 22 '22 at 17:02
  • @leftaroundabout you are 100% correct! :) – HelloWorld Nov 22 '22 at 17:13

1 Answers1

7

The PDF contains odd unicode symbols like which is not a normal dash, but rather a unicode "Minus Sign". To fix this simply replace them:

readExpr :: String -> String
readExpr input =  case parse symbol "lisp" input of
    Left err -> "No match: " ++ show err
    Right val -> "Found value"
chi
  • 111,837
  • 3
  • 133
  • 218
Noughtmare
  • 9,410
  • 1
  • 12
  • 38
  • @Nougthmare it works now. But is there no analog to something like `SyntaxError: invalid character '−' (U+2212)` in Python? – HelloWorld Nov 22 '22 at 18:27
  • 2
    @HelloWorld No, because there's no such thing as an invalid character. (Okay, there is, but `−` is not one.) Indeed, `−>`, `–>`, `–>`, and `—>` are all valid operators in Haskell. They're just not the one you're looking for. It's no more "invalid" than if you had written `Loft` instead of `Left`. – A. R. Nov 22 '22 at 20:14