Haskell?

Started by
8 comments, last by neilski_2003 18 years, 1 month ago
Hi Guys, I've been giving Haskell a go in an attempt to emulate a train timetable that i have created in C++, which seemed a nice easy thing to do at the time. However having created the list structure and a couple of snazy menus i have come unstuck on the functions themselves. I won't go into it too much for starters, but if there is anyone out there with some experience who might help shed some light on list comprehension then post a reply and i'll get back here with more information. Cheers for any replies Neil
Advertisement
Quote:Original post by neilski_2003
I won't go into it too much for starters, but if there is anyone out there with some experience who might help shed some light on list comprehension then post a reply and i'll get back here with more information.


It's been a while but you can try me out, i love haskell [grin].
I haven't done any Haskell programming since my functional programming course, one and a half years ago.
But I'll see if I can contribute in some way to answering your questions.

Regards,
/Omid
Best regards, Omid
Right OK, basically i have created a list which hold all the relevant information, and i need (well firstly anyway to find out the time it takes to say get from kings cross to victoria station.

Therefore I need to get access to the 3rd element of the list and then recurse through all those times whilst adding them up.

I presume thats 3 functions which may need to passed into each other etc etc...

But unfortunately being a total newbie i wouldn't have a scooby doo what to do, infact i'd really like to know how to access the third element of the list but heres an idea for that based on what i have done so far.

bakerloo :: Station->Element0->[Element3]
bakerloo dBase findIt = [element3 | (element0,element1,element2,elemnt3)<-dbase, element0 = findIt]

As you can probably tell thats 'moulded' aka stolen from a tutors example and i'm not entirely sure i understand it all!!!

Anyhow heres what i have so far - it is literally just the list and then the menus that i have used in the c++ version converted for haskell.

import Preludetype Element0 = Stringtype Element1 = Inttype Element2 = Floattype Element3 = Booltype Element4 = Booltype Element5 = Booltype Element6 = Booltype Element7 = Booltype Element8 = Booltype Element9 = Booltype Element10 = Booltype Element11 = Booltype Station = [ (Element0, Element1, Element2, Element3, Element4, Element5, Element6, Element7, Element8, Element9, Element10, Element11) ]victoriaLine :: StationvictoriaLine = [ ("Walthamstow Central", 1, 0, False, True, False, False, False, False, False, False, False),	         ("BlackHorse Road", 2, 3.0, False, True, False, False, False, False, False, False, False),	         ("Tottenham Hale", 3, 3.3, False, True, False, False, False, False, False, False, False),	    	 ("Seven Sisters", 4, 3, False, True, False, False, False, False, False, False, False),            	 ("Finsbury Park", 5, 4.0, False, True, False, False, False, False, False, False, True),	    	 ("Highbury and islington", 6, 4.0, False, True, False, False, False, False, False, False, False),            	 ("Kings Corss And St Pancras", 7, 3.3, False, True, False, True, False, False, True, True, True),            	 ("Euston", 8, 1.3, False, True, False, False, False, False, False, True, False),             	 ("Warren Street", 9, 2.0, False, False, False, False, False, False, False, True, False),            	 ("Oxford Circus", 10, 1.45, True, False, True, False, False, False, False, False, False),            	 ("Green Park", 11, 2.0, False, False, False, False, False, True, False, False, True),            	 ("Victoria", 12, 2.3, False, True, False, False, True, True, False, False, False),            	 ("Pimlico", 13, 1.0, False, False, False, False, False, False, False, False, False),             	 ("Vauxhall", 14, 1.45, False, True, False, False, False, False, False, False, False),            	 ("Stockwell", 15, 1.3, False, False, False, False, False, False, False, True, False),            	 ("Brixton", 16, 2.0, False, True, False, False, False, False, False, False, False) ]mainMenu :: IO()mainMenu = do		putStr"\n"		putStr"Welcome To the Victoria Line Timetable System\n"		putStr"Please Make A Choice from those Listed Below\n\n"		putStr"Detail Station Information = stationMenu\n"		putStr"For All Stations that link to the Bakerloo Line = Bakerloo\n"		putStr"For All Stations that link to British Rail Services = British\n"		putStr"For All Stations that link to the Central Line = Central\n"		putStr"For All Stations that link to the Circle Line = Circle\n"		putStr"For All Stations that link to the District Line = District\n"		putStr"For All Stations that link to the Jubilee Line = Jubilee\n"		putStr"For All Stations that link to the Metropolitan Line = Metropolitan\n"		putStr"For All Stations that link to the Northern Line = Northern\n"		putStr"For All Stations that link to the Piccadilly Line = Piccadilly\n"stationMenu::IO()stationMenu = do		putStr"\n"		putStr"Please Select The station You are located at\n\n"		putStr"Walthamstow Central\n"		putStr"BlackHorse Road\n"		putStr"Tottenham Hale\n"		putStr"Seven Sisters\n"		putStr"Finsbury Park\n"		putStr"Higbury and Islington\n"		putStr"Kings Cross and St Pancras\n"		putStr"Euston\n"		putStr"Warren Street\n"		putStr"Oxford Circus\n"		putStr"Green Park\n"		putStr"Victoria\n"		putStr"Pimlico\n"		putStr"Vauxhall\n"		putStr"Stockwell\n"		putStr"Brixton\n"lineMenu::IO()lineMenu = do		putStr"\n"		putStr"Please Select The Line you wish to connect to\n"		putStr"Bakerloo Line = nrBakerloo\n"		putStr"British Rail = nrBritish\n"		putStr"Central Line = nrCentral\n"		putStr"Circle Line = nrCircle\n"		putStr"District Line = nrDistrict\n"		putStr"Jubilee Line = nrJubillee\n"		putStr"Metropolitan Line = nrMetropolitan\n"		putStr"Northern Line = nrNorthern\n"		putStr"Piccadilly Line = nrPicadilly\n"


So as i said, i have a list of station information, and basically need ways of accessing it and then using it in some calculation or another.

Cheers for your help!

Neil
Quote:Original post by neilski_2003
But unfortunately being a total newbie i wouldn't have a scooby doo what to do, infact i'd really like to know how to access the third element of the list but heres an idea for that based on what i have done so far.

bakerloo :: Station->Element0->[Element3]
bakerloo dBase findIt = [element3 | (element0,element1,element2,elemnt3)<-dbase, element0 = findIt]

As you can probably tell thats 'moulded' aka stolen from a tutors example and i'm not entirely sure i understand it all!!!


This doesn't look quite correct, first it looks like you have a typo with dBase and dbase. Secondly your tuple pattern doesn't match the type of the elements stored in the list. Station is a type alias for list of 11-tuple but your tuple pattern is of type 4-tuple. Lastly haskell's equality operator is == not single = and there is no "assignment" operations in a purely function language like haskell. That should look something like this:

bakerloo dBase findIt = [ y | (x, _, _, y, _, _, _, _, _, _, _, _) <- dBase, x == findIt ]                          ^                        ^                           ^                          |                        |                           | Resulting expression. (what goes in the new list) |                           |                                                   |                           |  This is called a *generator* with a tuple pattern (including the <- dBase).  |                                                                                 |                                  This is a *guard expression* used to filter out elements


Now i don't know if that is the intended behaviour you are looking for since i haven't read your problem properly and it might be against the rules if this is for homework however i will come back to your problem later on.

This seems like good info on list comprehensions.

Quote:Original post by neilski_2003
So as i said, i have a list of station information, and basically need ways of accessing it and then using it in some calculation or another.


As i said earlier i'll come back to re-read your problem and give a more in-depth answer if i can, in the meantime i recommend you have alook at the haskell prelude (standard library), in particular have at look at the List docs to access elements of list etc, etc without reinventing wheels.

[Edited by - snk_kid on March 16, 2006 4:08:48 PM]
As an aside, list comprehension was stolen by some procedural languages (like python).

Spec:
http://www.python.org/doc/peps/pep-0202/
Tutorial:
http://www.network-theory.co.uk/docs/pytut/tut_38.html

Looking at how other languages deal with the same pattern can sometimes be useful. =)

Cheers guys,

Yeah - its not a homework assignment, its a bit of a challenge i have set myself given some previous work i have done - why only do things one way when there are many more options out there?

Thanks for spotting those errors i will give it a go and see what happens, and to everyone else thanks for all the information on the tutorials etc which i will be sure to go through

Thanks again

Neil
Hi there,

Back again - basically i wrote in the function as you suggested it and it came back with an error

Main> bakerloo
ERROR - Cannot find "show" function for:
*** Expression : bakerloo
*** Of type : Station -> Element0 -> [Element2]

(The same error occurs when i run my tutors example)

i'm going to google it - and if i find it i'll edit this post - if not does anyone know what thats all about?

Cheers for your help guys - as i say i am a complete novice in this so apologies for the newbiness!

Thanks

Neil

P.S - I am using WinHugs
Show is a built in function that let's you define how your type gets printed. This page has a description toward the bottom.
I've had a look at that actually - but being a bit of a novice i'm not entirely sure what my problem is with regard to what i have already done - i'll keep trying to work it out though!

Thanks

Neil

This topic is closed to new replies.

Advertisement