user2299776
user2299776

Reputation: 3

Parse error on input '=' something wrong with my Haskell

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?

Upvotes: 0

Views: 212

Answers (1)

Mark Reed
Mark Reed

Reputation: 95315

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.

Upvotes: 6

Related Questions