0

This is a simple function:

len [] = 0    
len (x:xs) = 1 + len xs

and I have loaded it in to GHCi using :l, but I aways got this error parse error on input =.

I run this in another computer, then it's OK. My computer is a Mac. Is there something wrong with my Haskell?

1
  • 1
    It works in my computer, with Ubuntu 12.04 and GHC 7.4.1. I copy pasted your code snippet and it works just fine. Commented Apr 20, 2013 at 16:50

1 Answer 1

6

You need a newline between the two patterns for the len function. Then it works fine:

$ cat len.hs
len [] = 0
len (x:xs) = 1 + len xs
$ ghci
GHCi, version 7.4.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :l len.hs
[1 of 1] Compiling Main             ( len.hs, interpreted )
Ok, modules loaded: Main.
*Main> len []
0
*Main> len [1]
1
*Main> len [1,2,3]
3
*Main> 

Since you mentioned that it's a Mac, perhaps you have a newline-convention incompatibility. Make sure that your text editor and GHCi agree about what constitutes a newline on your platform.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, there is a new line in my file, not working. Wrong format on the website.
As I said, make sure it's the sort of newline that GHC is expecting, because the error you are getting is exactly the error you would get if the newline were missing. Is it a carriage return, linefeed, or both?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.