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 (Imants Cekusins) 2. Re: Alternative (Imants Cekusins) 3. Re: Alternative (Yitzchak Gale) 4. Re: Alternative (Imants Cekusins) 5. Re: Alternative (Tony Morris) ---------------------------------------------------------------------- Message: 1 Date: Wed, 28 Dec 2016 13:41:07 +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: <cap1qinbccpau0pwp9pw8-n+_8z0fzspfsmzghnh6ktywk8a...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" > something even simpler: cheers Oliver. asum *is* better. how is it possible to (<|>) results in: type Am m = (Alternative m, Monad m) (Am m, Am n) => (a -> m (n b)) -> [a] -> m (n b) ? say, m (n b) is IO (Maybe b) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161228/dfc1d49f/attachment-0001.html> ------------------------------ Message: 2 Date: Wed, 28 Dec 2016 14:08:22 +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: <cap1qiny2iprdmtpofu9zdpg6zsnf162bycvzzmsxnruigvf...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" > how is it possible to (<|>) results in: .. sequence + asum did it: do lm2 <- sequence m1 asum lm2 `shouldBe` (Just 1) where m1 = [pure Nothing, pure (Just 1), pure (Just 2), pure Nothing]::[IO (Maybe Int)] thank you Oliver. good tip. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161228/f4ba477c/attachment-0001.html> ------------------------------ Message: 3 Date: Wed, 28 Dec 2016 22:02:18 +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: <CAOrUaLYbcFy0-c4HMO3ELZhNQQX=yh71waecfdztcnrossf...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 Imants Cekusins wrote: > foldl (<|>) Nothing [Nothing, Just 1, Nothing, Just 2] > Just 1 Apart from Ollie's suggestion of asum, here are a few more interesting points that come up from your observation: In your solution, you would want to use foldr, not foldl. The left fold forces reading the entire list to the end even though you already hit your Just. A right fold will short circuit. That also means the right fold will even work with an infinite list, as long as there is at least one Just somewhere in the list. (Note that asum uses foldr under the hood. :)) And if you still do use a left fold, you'll want foldl' (from Data.List) rather than foldl. The foldl from the Prelude will build up a pile of unevaluated lazy thunks the size of your list, and only then evaluate all the <|> calculations all at once. So this will crash with a stack overflow error if your list is very large. Whereas foldl' will perform all of the <|> calculations one by one in constant memory. Yitz ------------------------------ Message: 4 Date: Wed, 28 Dec 2016 21:43:25 +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: <CAP1qina8g7XF9nnipFvu4dEzVX9fv_vS2=76jadf-dkagmr...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" > you would want to use foldr, not foldl foldl vs foldr baffles me a bit. in Erlang http://erlang.org/doc/man/lists.html#foldl-3 use of *foldl* is suggested. I got used to this and usually use foldl. another reason is: it appears that in Haskell - same as Erlang - *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. however: *foldr* arg order: a -> acc is more natural. I very rarely deal with large lists and never (so far) with inifinites. for this case for which I consider and use Alternatives - the list would be 2 - 5 items long. The order though is important. >>> asum [Just 1, Just 2] Just 1 >>> asum [Nothing, Just 1, Nothing, Just 2, Nothing] Just 1 looks "naturally" ordered: tested left to right. I may not understand the workings (memory and such) of foldl vs foldr however I hope that for small lists it is sufficient to focus on the order of element processing. order matters. This example hopefully confirms that foldr begins @ end, foldl begins @ start. Same as in Erlang ;) Prelude Data.Foldable> *foldr* (\i1 acc1 -> i1 + acc1 * 2) 0 [1,2] 5 Prelude Data.Foldable> *foldl* (\acc1 i1 -> i1 + acc1 * 2) 0 [1,2] 4 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161228/c88d6194/attachment-0001.html> ------------------------------ Message: 5 Date: Thu, 29 Dec 2016 07:07:02 +1000 From: Tony Morris <tonymor...@gmail.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Alternative Message-ID: <6925f9a5-2e6a-924d-4d30-67bafa0ff...@gmail.com> Content-Type: text/plain; charset="utf-8" foldr doesn't begin anywhere. https://vimeo.com/64673035 On 29/12/16 06:43, Imants Cekusins wrote: > > you would want to use foldr, not foldl > > foldl vs foldr baffles me a bit. > > in Erlang http://erlang.org/doc/man/lists.html#foldl-3 > use of *foldl* is suggested. I got used to this and usually use foldl. > > another reason is: it appears that in Haskell - same as Erlang - > *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. > > however: *foldr* arg order: a -> acc is more natural. > > I very rarely deal with large lists and never (so far) with inifinites. > > for this case for which I consider and use Alternatives - the list > would be 2 - 5 items long. The order though is important. > > >>> asum [Just 1, Just 2] > Just 1 > >>> asum [Nothing, Just 1, Nothing, Just 2, Nothing] > Just 1 > > looks "naturally" ordered: tested left to right. > > I may not understand the workings (memory and such) of foldl vs foldr > however I hope that for small lists it is sufficient to focus on the > order of element processing. > > order matters. This example hopefully confirms that foldr begins @ > end, foldl begins @ start. Same as in Erlang ;) > > Prelude Data.Foldable> *foldr* (\i1 acc1 -> i1 + acc1 * 2) 0 [1,2] > 5 > Prelude Data.Foldable> *foldl* (\acc1 i1 -> i1 + acc1 * 2) 0 [1,2] > 4 > > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161229/32f65a94/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 488 bytes Desc: OpenPGP digital signature URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161229/32f65a94/attachment.sig> ------------------------------ 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 18 ******************************************