Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Alternative (Yitzchak Gale) 2. Re: Alternative (Imants Cekusins) 3. Re: Alternative (Yitzchak Gale) 4. Re: Alternative (Imants Cekusins) ---------------------------------------------------------------------- Message: 1 Date: Wed, 28 Dec 2016 23:47:10 +0200 From: Yitzchak Gale <g...@sefer.org> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Alternative Message-ID: <caorualbnra7xaykstzsygx1xtfzobebsrhhrzrcwuov9hkm...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 I wrote: >> you would want to use foldr, not foldl Imants Cekusins wrote: > foldl enumerates items in "natural" order: > in [1,2] 1 is passed to the fn, then 2 > foldr on the other hand begins with 2 and ends with 1. The list is not reversed by either one of them. And in both of them, the first element of the list is examined first. With foldl, you apply the complete operation to each element of the list in turn, all the way to the end of the list. With foldr, you start with the first element and say: Let's think about what will happen when we apply the operation to the first element with the result of the entire fold from the second element onward. Knowing only the first element at this time, can we already say what the result will be? In this case, there are two possibilities: If the first element is Just, then yes, we already know the result, and we can discard the rest of the fold without ever actually computing it. If the first element is Nothing, then we don't know the result yet. But we do know that the first element is irrelevant, so we discard it and move on. Doesn't foldr seem like a more natural way to do what you want here? The reason this doesn't work in Erlang is because Erlang is a strict language. When we try to apply the operation with first parameter the first element of the list and second parameter the rest of the fold, Erlang does the entire calculation of that second parameter - the entire fold - before it even starts thinking about the operation. Regards, Yitz ------------------------------ Message: 2 Date: Wed, 28 Dec 2016 23:14:18 +0100 From: Imants Cekusins <ima...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Alternative Message-ID: <CAP1qinZQw3V-UWQVDM8rgre==coxabfzovj06cvgcmdw_hi...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" thank you Tony and Yitzchak. I may understand it one day ;) it is important to understand it alright. I remember spending half a day over unexpected (for me) results of recursive functions that contained folds. In the end I settled for TChan - a queue with predictable behaviour (my view of it - not necessarily correct). Yitzchak, how about this: Prelude Control.Applicative> *foldl* (<|>) Nothing [Just 1, Nothing, Just 2, Nothing] Just 1 Prelude Control.Applicative> *foldr* (<|>) Nothing [Just 1, Nothing, Just 2, Nothing] Just 1 ? I guess this is due to the nature of (<|>), no? Anyway, I'd use asum instead of fold_ for Alternatives. for non-Alternative cases, Erlang analogy seems to be a useful rule of thumb for foldl & foldr over shorter lists . After all, what matters most is what results to expect. *foldl* and *foldr* may yield different results for the same list & similar processing fn (save for different arg order). -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161228/088cac3c/attachment-0001.html> ------------------------------ Message: 3 Date: Thu, 29 Dec 2016 01:24:25 +0200 From: Yitzchak Gale <g...@sefer.org> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Alternative Message-ID: <caorualyf4pizbyhitrtashv7gkmrhn1cr1mbinonscuw3ja...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 Imants Cekusins wrote: > Yitzchak, how about this: > > Prelude Control.Applicative> foldl (<|>) Nothing [Just 1, Nothing, Just 2, > Nothing] > Just 1 > Prelude Control.Applicative> foldr (<|>) Nothing [Just 1, Nothing, Just 2, > Nothing] > Just 1 > > ? I guess this is due to the nature of (<|>), no? Yes, you are right. <|> for Maybe satisfies the associative law: (x <|> y) <|> z == x <|> (y <|> z) for all x, y, and z. So it does not matter if you apply it from right to left or from left to right. > Anyway, I'd use asum instead of fold_ for Alternatives. Agreed. For the actual problem, Ollie is right. I just thought you might be interested in these other concepts that come up from your good thinking. Regards, Yitz ------------------------------ Message: 4 Date: Thu, 29 Dec 2016 00:37:54 +0100 From: Imants Cekusins <ima...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Alternative Message-ID: <cap1qinyii0x6tsm8xa+56fcg_jyqfagraex7aloof8d3z+w...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Yitzchak, I am interested alright. However for very practical purpose. > (x <|> y) <|> z == x <|> (y <|> z) understood. thank you Yitzchak. we will revisit *recurse & fold *problem I ran into earlier. probably in Jan though. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161229/677d4a38/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 102, Issue 19 ******************************************