
Reading http://learnyouahaskell.com/higher-order-functions I understand that with the function sum' :: (Num a) => [a] -> a sum' = foldl (+) 0 the call ghci>>> sum' [1,2,3] will be evaluated as 0 + 1 + 2 + 3 = 6 But what about the function elem' :: (Eq a) => a -> [a] -> Bool elem' y ys = foldl (\acc x -> if x == y then True else acc) False ys and calling it with ghci>>> elem' 3 [1,2,3] How is that evaluated to True by foldl in elem'? Thanks in advance for any explanation to this!

Hi.
Your function gets passed numbers one by one in the place of x, and its
previous result in the place of acc, and it returns a Bool. Initial value
in place of acc parameter ("previous result") is put as False (since you
begin with answer "no" to question "is it elem?").
Hope this helps.
24 вер. 2015 19:04 "goforgit ."
Reading http://learnyouahaskell.com/higher-order-functions
I understand that with the function
sum' :: (Num a) => [a] -> a sum' = foldl (+) 0
the call
ghci>>> sum' [1,2,3]
will be evaluated as
0 + 1 + 2 + 3 = 6
But what about the function
elem' :: (Eq a) => a -> [a] -> Bool elem' y ys = foldl (\acc x -> if x == y then True else acc) False ys
and calling it with
ghci>>> elem' 3 [1,2,3]
How is that evaluated to True by foldl in elem'?
Thanks in advance for any explanation to this!
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Thanks I got it now :)
On Thu, Sep 24, 2015 at 9:45 PM, Kostiantyn Rybnikov
Hi.
Your function gets passed numbers one by one in the place of x, and its previous result in the place of acc, and it returns a Bool. Initial value in place of acc parameter ("previous result") is put as False (since you begin with answer "no" to question "is it elem?").
Hope this helps. 24 вер. 2015 19:04 "goforgit ."
пише: Reading http://learnyouahaskell.com/higher-order-functions
I understand that with the function
sum' :: (Num a) => [a] -> a sum' = foldl (+) 0
the call
ghci>>> sum' [1,2,3]
will be evaluated as
0 + 1 + 2 + 3 = 6
But what about the function
elem' :: (Eq a) => a -> [a] -> Bool elem' y ys = foldl (\acc x -> if x == y then True else acc) False ys
and calling it with
ghci>>> elem' 3 [1,2,3]
How is that evaluated to True by foldl in elem'?
Thanks in advance for any explanation to this!
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Note that you would like the elem function to stop at the matching element and return True rather than checking the rest of the list needlessy, which can be done with foldr but not with foldl. The actual implementation of elem does this, so you can say:
elem 1 [1..] True
which would fail to terminate with the foldl version.
On Fri, Sep 25, 2015 at 11:10 AM goforgit .
Thanks I got it now :)
On Thu, Sep 24, 2015 at 9:45 PM, Kostiantyn Rybnikov
wrote: Hi.
Your function gets passed numbers one by one in the place of x, and its previous result in the place of acc, and it returns a Bool. Initial value in place of acc parameter ("previous result") is put as False (since you begin with answer "no" to question "is it elem?").
Hope this helps. 24 вер. 2015 19:04 "goforgit ."
пише: Reading http://learnyouahaskell.com/higher-order-functions
I understand that with the function
sum' :: (Num a) => [a] -> a sum' = foldl (+) 0
the call
ghci>>> sum' [1,2,3]
will be evaluated as
0 + 1 + 2 + 3 = 6
But what about the function
elem' :: (Eq a) => a -> [a] -> Bool elem' y ys = foldl (\acc x -> if x == y then True else acc) False ys
and calling it with
ghci>>> elem' 3 [1,2,3]
How is that evaluated to True by foldl in elem'?
Thanks in advance for any explanation to this!
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Interesting, I did not know that, thank you!
On Sat, Sep 26, 2015 at 12:16 AM, Rein Henrichs
Note that you would like the elem function to stop at the matching element and return True rather than checking the rest of the list needlessy, which can be done with foldr but not with foldl. The actual implementation of elem does this, so you can say:
elem 1 [1..] True
which would fail to terminate with the foldl version.
On Fri, Sep 25, 2015 at 11:10 AM goforgit .
wrote: Thanks I got it now :)
On Thu, Sep 24, 2015 at 9:45 PM, Kostiantyn Rybnikov
wrote: Hi.
Your function gets passed numbers one by one in the place of x, and its previous result in the place of acc, and it returns a Bool. Initial value in place of acc parameter ("previous result") is put as False (since you begin with answer "no" to question "is it elem?").
Hope this helps. 24 вер. 2015 19:04 "goforgit ."
пише: Reading http://learnyouahaskell.com/higher-order-functions
I understand that with the function
sum' :: (Num a) => [a] -> a sum' = foldl (+) 0
the call
ghci>>> sum' [1,2,3]
will be evaluated as
0 + 1 + 2 + 3 = 6
But what about the function
elem' :: (Eq a) => a -> [a] -> Bool elem' y ys = foldl (\acc x -> if x == y then True else acc) False ys
and calling it with
ghci>>> elem' 3 [1,2,3]
How is that evaluated to True by foldl in elem'?
Thanks in advance for any explanation to this!
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
goforgit .
-
Kostiantyn Rybnikov
-
Rein Henrichs