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

kane96 at gmx.de kane96 at gmx.de
Thu Feb 11 15:09:37 EST 2010


mysecondlist = 0:mylist

that's was I was searching for, but how can I add the element to the same list? I have to get any number of inputs from the user and at the end, if he doesn't want to make more inputs, I have to print a list with all the inputs he did.

-------- Original-Nachricht --------
> Datum: Wed, 10 Feb 2010 22:21:13 +0100
> Von: Daniel Fischer <daniel.is.fischer at web.de>
> An: beginners at haskell.org
> CC: kane96 at gmx.de
> Betreff: Re: [Haskell-beginners] consing an element to a list inside a file

> 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
> 
> ?

-- 
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser


More information about the Beginners mailing list