[Haskell-cafe] Wheres this going wrong

Sebastian Sylvan sebastian.sylvan at gmail.com
Wed Mar 22 13:51:45 EST 2006


On 3/22/06, Neil Rutland <neilrutland2 at hotmail.com> wrote:
>
>
>
> Hi there,
>
> Thanks to some advise by one of the other posters i have chosen to try and
> set up a list that uses lookup to find the values of the elements within it.
>
> However what i have attempted so far has resulted in odd answers or errors.
> Anyway here it is, i have given each element a string at the start to put
> the lookup domain in - anyway here it is
>
> type Line =
> [((String,String),(String,Int),(String,Int),(String,Bool),(String,Bool),(String,Bool),(String,Bool),(String,Bool),(String,Bool),(String,Bool),(String,Bool),(String,Bool))]
>
> vicsLine :: Line
> vicsLine = [(("a1","Walthamstow
> Central"),("a2",1),("a3",2),("a4", False), ("a5", True),
> ("a6", False), ("a7", False), ("a8", False), ("a9", False), ("a10", False),
> ("a11", False), ("a12", False))]
>
> Anyway when i enter something such as lookup "a1" i get back a load of stuff
> about Eq.....
>
> So the question is - what am i doing wrong. I am hoping that when i enter a1
> it should return for me Walthamstow Central.
>

What shoul happen if you enter "a2"?
lookup takes a "key" and  a list of key/value pairs, and maybe returns
the value.
For instance, one list may look like this:
db = [(1,"Hello"), (2,"world")]

If you then use 'lookup 1 db', you'd get 'Just "Hello"', if you type
'lookup 12 db' you'd get 'Nothing' since there is no pair where 12 is
the first value.

Now, it's important that the list you pass to lookup is of the form: [(a,b)]
In other words it MUST be a list of PAIRS. The second value of the
pair may be of any type (including a complex type with lists and
tuples and what-not) but it must be the SAME type for all the elements
in the list. This isn't just a restriction on lookup, btw, the
elements of a list must always be of the same type:
OK: [(1,"hello"),(2,"bye")]
Not OK: [(2,"hello"),(2,14)] -- 14 has a different type than "hello"!

So, what I think you want is something like this:

type Name = String
type Minutes = Int

type StationInfo = [Line] -- maybe something more here?
type Station = (Name,StationInfo)

type LineInfo = [(Minutes, Station)] -- maybe somthing more here?
type Line = (Name,LineInfo)

-- use lookups on these two
-- Its important that both Station, and Line are of the form (a,b)
(and not, say (a,b,c))
type StationDB = [Station]
type LineDB = [Line]

StationInfo would then be a large tuple, perhaps containing a list of
the names of the lines which pass through it (then you could have
another list containing these lines which would map the name of a line
to a LineInfo which would, I suppose, contain a list of station
names).

/S
--
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862


More information about the Haskell-Cafe mailing list