consing an element to a list inside a file

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 -- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/atbrowser

On Wed, Feb 10, 2010 at 09:52:48PM +0100, kane96@gmx.de wrote:
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
A Haskell file is simply a list of declarations, i.e. definitions of data types, classes, and functions. You cannot just have an expression by itself in a Haskell file. You can write expressions at a ghci prompt (which I assume is what you meant) because that is what ghci is for: evaluating expressions. Why do you want 0:mylist by itself in a Haskell file? What is its purpose? Note that it will not modify the value of mylist (indeed, you *cannot* modify the value of mylist once it has been defined). -Brent

It's just a part of a more complex exercise I have to read an input of the user. After that he can decide if he wants to add another input. If not, all inputs the user did should be printed on the screen. So I want to add each input to a list and print the list at the end. -------- Original-Nachricht --------
Datum: Wed, 10 Feb 2010 16:00:35 -0500 Von: Brent Yorgey
An: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file
On Wed, Feb 10, 2010 at 09:52:48PM +0100, kane96@gmx.de wrote:
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
A Haskell file is simply a list of declarations, i.e. definitions of data types, classes, and functions. You cannot just have an expression by itself in a Haskell file. You can write expressions at a ghci prompt (which I assume is what you meant) because that is what ghci is for: evaluating expressions.
Why do you want 0:mylist by itself in a Haskell file? What is its purpose? Note that it will not modify the value of mylist (indeed, you *cannot* modify the value of mylist once it has been defined).
-Brent _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Am Mittwoch 10 Februar 2010 21:52:48 schrieb kane96@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 ?

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
An: beginners@haskell.org CC: kane96@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@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

Am Donnerstag 11 Februar 2010 21:09:37 schrieb kane96@gmx.de:
mysecondlist = 0:mylist
that's was I was searching for, but how can I add the element to the same list?
You can't. When you add an element to a list, it's a different 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.
Make the list a parameter of the input loop. getInputs previous = do user <- getUserInput if endNow user then printList previous else getInputs (user:previous)

the problem is the function should be readMyDatatype :: IO MyDatatype readMyDatatype = do ... Is it possible to do it somehow different without a parameter? -------- Original-Nachricht --------
Datum: Thu, 11 Feb 2010 21:41:15 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file
Am Donnerstag 11 Februar 2010 21:09:37 schrieb kane96@gmx.de:
mysecondlist = 0:mylist
that's was I was searching for, but how can I add the element to the same list?
You can't. When you add an element to a list, it's a different 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.
Make the list a parameter of the input loop.
getInputs previous = do user <- getUserInput if endNow user then printList previous else getInputs (user:previous)
-- NEU: Mit GMX DSL über 1000,- ¿ sparen! http://portal.gmx.net/de/go/dsl02

Am Donnerstag 11 Februar 2010 22:34:45 schrieb kane96@gmx.de:
the problem is the function should be readMyDatatype :: IO MyDatatype readMyDatatype = do ... Is it possible to do it somehow different without a parameter?
Separate the worker from the API function. API: readMyDatatype :: IO MyDatatype readMyDatatype = call worker with appropriate arguments worker: readMD :: t1 -> t2 -> ... -> tn -> IO MyDatatype
-------- Original-Nachricht --------
Datum: Thu, 11 Feb 2010 21:41:15 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file Am Donnerstag 11 Februar 2010 21:09:37 schrieb kane96@gmx.de:
mysecondlist = 0:mylist
that's was I was searching for, but how can I add the element to the same list?
You can't. When you add an element to a list, it's a different 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.
Make the list a parameter of the input loop.
getInputs previous = do user <- getUserInput if endNow user then printList previous else getInputs (user:previous)

but the exercise was nearly complete on the sheet and I just have to complete two parts and one of them is to ask the user if he want's to do more input. If so, I have to let him input a new value (triple) for MyDatatype, otherwise I have to print the list with all triples -------- Original-Nachricht --------
Datum: Thu, 11 Feb 2010 22:54:59 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file
Am Donnerstag 11 Februar 2010 22:34:45 schrieb kane96@gmx.de:
the problem is the function should be readMyDatatype :: IO MyDatatype readMyDatatype = do ... Is it possible to do it somehow different without a parameter?
Separate the worker from the API function.
API:
readMyDatatype :: IO MyDatatype readMyDatatype = call worker with appropriate arguments
worker:
readMD :: t1 -> t2 -> ... -> tn -> IO MyDatatype
-------- Original-Nachricht --------
Datum: Thu, 11 Feb 2010 21:41:15 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file Am Donnerstag 11 Februar 2010 21:09:37 schrieb kane96@gmx.de:
mysecondlist = 0:mylist
that's was I was searching for, but how can I add the element to the same list?
You can't. When you add an element to a list, it's a different 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.
Make the list a parameter of the input loop.
getInputs previous = do user <- getUserInput if endNow user then printList previous else getInputs (user:previous)
-- NEU: Mit GMX DSL über 1000,- ¿ sparen! http://portal.gmx.net/de/go/dsl02

Am Donnerstag 11 Februar 2010 23:04:01 schrieb kane96@gmx.de:
but the exercise was nearly complete on the sheet and I just have to complete two parts and one of them is to ask the user if he want's to do more input. If so, I have to let him input a new value (triple) for MyDatatype, otherwise I have to print the list with all triples
But to print it, you need to have a reference to the list of inputs. The easiest way is to have it as a parameter in the worker loop. What *exactly* is your task? What is given and what are the specifications for a solution?
Am Donnerstag 11 Februar 2010 22:34:45 schrieb kane96@gmx.de:
the problem is the function should be readMyDatatype :: IO MyDatatype readMyDatatype = do ... Is it possible to do it somehow different without a parameter?
Separate the worker from the API function.
API:
readMyDatatype :: IO MyDatatype readMyDatatype = call worker with appropriate arguments
worker:
readMD :: t1 -> t2 -> ... -> tn -> IO MyDatatype

the exercise looks like that: readMyCal :: IO MyCalendar readMyCal = do putStr "day: " d <- readInt putStr "month: " m <- readMonth putStr "year: " y <- readInt let mydate = (d,m,y) if not (legalDate mydate) then do putStrLn "wrong" --COMPLETE-1-- else --COMPLETE-2-- COMPLETE-1: the user can give in a new date. I did it by simple calling the function again COMPLETE-2: the user can choose if he wants to enter a new. If not, all dates the user has put in should be put out as a calendar (a list of dates) -------- Original-Nachricht --------
Datum: Thu, 11 Feb 2010 23:30:05 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file
Am Donnerstag 11 Februar 2010 23:04:01 schrieb kane96@gmx.de:
but the exercise was nearly complete on the sheet and I just have to complete two parts and one of them is to ask the user if he want's to do more input. If so, I have to let him input a new value (triple) for MyDatatype, otherwise I have to print the list with all triples
But to print it, you need to have a reference to the list of inputs. The easiest way is to have it as a parameter in the worker loop.
What *exactly* is your task? What is given and what are the specifications for a solution?
Am Donnerstag 11 Februar 2010 22:34:45 schrieb kane96@gmx.de:
the problem is the function should be readMyDatatype :: IO MyDatatype readMyDatatype = do ... Is it possible to do it somehow different without a parameter?
Separate the worker from the API function.
API:
readMyDatatype :: IO MyDatatype readMyDatatype = call worker with appropriate arguments
worker:
readMD :: t1 -> t2 -> ... -> tn -> IO MyDatatype
-- Sicherer, schneller und einfacher. Die aktuellen Internet-Browser - jetzt kostenlos herunterladen! http://portal.gmx.net/de/go/atbrowser

Am Freitag 12 Februar 2010 20:15:26 schrieb kane96@gmx.de:
the exercise looks like that:
readMyCal :: IO MyCalendar readMyCal = do putStr "day: " d <- readInt putStr "month: " m <- readMonth putStr "year: " y <- readInt let mydate = (d,m,y) if not (legalDate mydate) then do putStrLn "wrong" --COMPLETE-1-- else --COMPLETE-2--
COMPLETE-1: the user can give in a new date. I did it by simple calling the function again
Reasonable.
COMPLETE-2: the user can choose if he wants to enter a new. If not, all dates the user has put in should be put out as a calendar (a list of dates)
Okay, that's not nice. But since you have no global variables to store the list of dates entered in (yes, there are ways to have global variables, but it's not a good idea unless you really have to), call readMoreDates :: [MyDate] -> IO MyCaledar readMoreDates previous = do ... (code duplication where it isn't really necessary, but what the heck).

ok, now I did it by just calling from the original function by to a new one with an empty list at the beginning. How can I add the date at the end of the list? By creating a new list from the new date and using concat or is there a simpler way? -------- Original-Nachricht --------
Datum: Fri, 12 Feb 2010 23:18:06 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file
Am Freitag 12 Februar 2010 20:15:26 schrieb kane96@gmx.de:
the exercise looks like that:
readMyCal :: IO MyCalendar readMyCal = do putStr "day: " d <- readInt putStr "month: " m <- readMonth putStr "year: " y <- readInt let mydate = (d,m,y) if not (legalDate mydate) then do putStrLn "wrong" --COMPLETE-1-- else --COMPLETE-2--
COMPLETE-1: the user can give in a new date. I did it by simple calling the function again
Reasonable.
COMPLETE-2: the user can choose if he wants to enter a new. If not, all dates the user has put in should be put out as a calendar (a list of dates)
Okay, that's not nice. But since you have no global variables to store the list of dates entered in (yes, there are ways to have global variables, but it's not a good idea unless you really have to), call
readMoreDates :: [MyDate] -> IO MyCaledar readMoreDates previous = do ...
(code duplication where it isn't really necessary, but what the heck).
-- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

Am Freitag 19 Februar 2010 00:25:18 schrieb kane96@gmx.de:
ok, now I did it by just calling from the original function by to a new one with an empty list at the beginning.
How can I add the date at the end of the list? By creating a new list from the new date and using concat or is there a simpler way?
Better stick it to the front by (:) and reverse the list just before printing.

ok, looks much easier and works :) But what are now the correct signatures for the function? Normally the "one and only" function should be: readCal :: IO Calendar My function are: readCal = readCal2 datelist and: readCal2 datelist = do... which returns the Calendar (the list of dates) -------- Original-Nachricht --------
Datum: Fri, 19 Feb 2010 00:41:56 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file
Am Freitag 19 Februar 2010 00:25:18 schrieb kane96@gmx.de:
ok, now I did it by just calling from the original function by to a new one with an empty list at the beginning.
How can I add the date at the end of the list? By creating a new list from the new date and using concat or is there a simpler way?
Better stick it to the front by (:) and reverse the list just before printing.
-- NEU: Mit GMX DSL über 1000,- ¿ sparen! http://portal.gmx.net/de/go/dsl02

I checked it by using :t readCal :: IO Calendar and readCal2 :: Calendar -> IO Calendar so the function to call (readCal) is what it should be. thanks! -------- Original-Nachricht --------
Datum: Fri, 19 Feb 2010 01:54:05 +0100 Von: kane96@gmx.de An: Daniel Fischer
CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file
ok, looks much easier and works :)
But what are now the correct signatures for the function? Normally the "one and only" function should be: readCal :: IO Calendar My function are: readCal = readCal2 datelist and: readCal2 datelist = do... which returns the Calendar (the list of dates)
-------- Original-Nachricht --------
Datum: Fri, 19 Feb 2010 00:41:56 +0100 Von: Daniel Fischer
An: kane96@gmx.de CC: beginners@haskell.org Betreff: Re: [Haskell-beginners] consing an element to a list inside a file Am Freitag 19 Februar 2010 00:25:18 schrieb kane96@gmx.de:
ok, now I did it by just calling from the original function by to a new one with an empty list at the beginning.
How can I add the date at the end of the list? By creating a new list from the new date and using concat or is there a simpler way?
Better stick it to the front by (:) and reverse the list just before printing.
-- NEU: Mit GMX DSL über 1000,- ¿ sparen! http://portal.gmx.net/de/go/dsl02 _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT! Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
participants (3)
-
Brent Yorgey
-
Daniel Fischer
-
kane96@gmx.de