[Haskell-beginners] consing an element to a list inside a file

Daniel Fischer daniel.is.fischer at web.de
Wed Feb 10 16:21:13 EST 2010


Am Mittwoch 10 Februar 2010 21:52:48 schrieb kane96 at gmx.de:
> Hi,
> I want to add an element to a list inside a haskell file:
> mylist = [1,2,3,4]
> 0:mylist
> in Prelude it works fine, but when I do it in a file I get the error:
> parse error (possibly incorrect indentation)
> Failed, modules loaded: none.
> in a line after this two that doesn't exist

0:mylist

is a plain value. At the ghci or hugs prompt, if you enter an expression 
(4, 3*7+5, [0 .. 10],...), that means "evaluate the expression and print 
the result" (unless it's a value of type IO a, then it means "execute this 
action").
In a source file, you write definitions,

name = expression to be bound to name

func arg1 arg2 = body

etc.

You can have pattern bindings in a source file,

(a,b) = ([0 .. 100],True)

-- defines a and b

0:mylist = map (`mod` 7) [14 .. 100]

-- defines mylist as [1,2,3,4,5,6,0,1,...,1,2]

0:myotherlist = [4,5,6]

{- will fail with
ghci> myotherlist
*** Exception: PatTest.hs:4:0-22: Irrefutable pattern failed for pattern 0 
: myotherlist

when demanded
-}
, so that's what the parser expected, a '=' and an expression which defines 
mylist.
Since it didn't find a '=' on the same line, it tried the next (where it 
found end of file, another definition or whatever) where it reported the 
parse error.

Probably you wanted

mysecondlist = 0:mylist

?



More information about the Beginners mailing list