[Haskell-beginners] find problem
aditya siram
aditya.siram at gmail.com
Fri Jul 22 22:06:06 CEST 2011
Hi Roelof,
The function itself will not work. Do you have a working Haskell
environment? If so, please try the function out first and let us know
what errors you see. I have a feeling from this and previous questions
that you're trying to understand the code just by trying to reading
it. You really need to execute it. Since you're posting here I presume
you have access to a computer, or at least the Web. Have you used
http://tryhaskell.org/?
That said I'll try and guess what "find" is supposed to do. It seems
to take a key "k" and output the values associated with the key. It
also appears that "t" is a list of key-value pairs eg. [("a",
"aardvard"), ("a", "armadillo"), ("b", bull")]. From your definition
of "find" it is different from a standard lookup table in that it will
will return all values associated with a key instead of just the
first. Eg. find "a" [("a", "aardvark"), ("a", "armadillo"), ("b",
bull")] == ["aardvark", "armadillo"].
This definition of "find":
find :: (Eq a) => a -> [(a,b)] -> [b]
find k t = [v' | (k', v') <- t , k == k']
seems to be what you want.
Firstly you're chunking the list comprehension incorrectly, the above
is the same as:
find k t = [v' | (k',v') <- t,
k == k']
The first part of the comprehension "(k',v') <- t" gets a tuple out of
the list and assigns first part to "k'" and the second to "v'". The
second part "k == k'" places a constraint on the list so that only
tuples whose first element is equal to the key passed in "k" are
allowed. The result of the comprehension, the v' before the "|"
character indicates that the second element of the tuple should be
output.
Taking the above definition try to trace by hand the execution of
"find". It'll give you a better idea of how result is generated.
-deech
On Fri, Jul 22, 2011 at 2:14 PM, Roelof Wobben <rwobben at hotmail.com> wrote:
> Hello,
>
> Sorry for asking so much but I really want to understand this.
>
> I have the function find k t [v, (k',v') <- t,k = v]
>
> Let's say k = false and t = [false, true, false, true]
>
> Then It will be find "false" ["false", "true", "false", "true]
> But where on earth are t,k = v and (k', v') come from ?
>
> Roelof
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
More information about the Beginners
mailing list