[Haskell-beginners] find an element in a list
Max Voit
max.voit+mlhb at with-eyes.net
Sat Dec 26 14:46:19 UTC 2015
Hi Fabien,
On Sat, 26 Dec 2015 15:19:17 +0100
Fabien R <theedge456 at free.fr> wrote:
> extractIfBegins x [xs] | [xs] == (x:ys) = [ys]
> | otherwise = [xs]
>
> But ghci complains that ys is not defined.
That is because you cannot pattern match whilst equality testing. The
statement
xs == (x:ys)
is problematic therefore. You expect the compiler to see "oh, I don't
know ys, but xs is a list, so I'm just checking x and put the rest into
ys while I'm at it".
> Without giving the answer, can someone give a hint about the approach
> to follow ?
Try pattern matching on the list xs instead. Also take care that it's
xs, not [xs] (the latter notation implies a list with one element xs).
Best,
Max
More information about the Beginners
mailing list