find an element in a list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

find an element in a list

Fabien R
As a newbie, I'm studying the pdf 'the Haskell road to logic, math and
programming' and I'm stuck with one exercise.
I want to extract x if x is at the beginning of a list.
I thought to use something like this:
extractIfBegins x [xs] | [xs] == (x:ys) = [ys] | otherwise = [xs]

But ghci complains that ys is not defined.
Without giving the answer, can someone give a hint about the approach to
follow ?

--
Fabien
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: find an element in a list

Max Voit-2
Hi Fabien,

On Sat, 26 Dec 2015 15:19:17 +0100
Fabien R <[hidden email]> 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

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: find an element in a list

David McBride
In reply to this post by Fabien R
It sounds like you want to include x only if it is not already at the head of the list.  Try this:

extractIfBegins x (y:ys)
  | x == y = ys
  | otherwise = x:ys

On Sat, Dec 26, 2015 at 9:19 AM, Fabien R <[hidden email]> wrote:
As a newbie, I'm studying the pdf 'the Haskell road to logic, math and programming' and I'm stuck with one exercise.
I want to extract x if x is at the beginning of a list.
I thought to use something like this:
extractIfBegins x [xs] | [xs] == (x:ys) = [ys]                          | otherwise = [xs]

But ghci complains that ys is not defined.
Without giving the answer, can someone give a hint about the approach to follow ?

--
Fabien
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: find an element in a list

Fabien R
In reply to this post by Max Voit-2
On 26/12/15 15:46, Max Voit wrote:
> 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".
Thanks Max,
It's more clear now.

--
Fabien
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners