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: Question about code I found (KC) ---------------------------------------------------------------------- Message: 1 Date: Wed, 17 Jul 2019 11:48:36 -0700 From: KC <kc1...@gmail.com> To: Haskell Beginners <beginners@haskell.org> Subject: Re: [Haskell-beginners] Question about code I found Message-ID: <camlkxykykvmrahujphawa1mpzrkh+xqj_+b-zva-alak4fq...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" It's more correct to say the List Monad Is a Monad instance who is a List But people look at you funny In my case Funnier than usual On Sun., Jul. 7, 2019, 9:47 a.m. Terry Phelps, <tgphelp...@gmail.com> wrote: > I have written enough code in the IO monad to fairly well understand how > the 'do' and 'bind' forms work. But I've never seen monadic code that was > NOT in the IO monad. Your explanation tells me where to go do more study. > Thank you.If I go read the definition of bind in the List monad, it will > probably become clear. In the original code, the List monad is completely > invisible to my untrained eye, and I was confused. > > > On Sun, Jul 7, 2019 at 12:13 PM Francesco Ariis <fa...@ariis.it> wrote: > >> Hello Terry >> >> On Sun, Jul 07, 2019 at 11:24:47AM -0400, Terry Phelps wrote: >> > I found this code on the net somewhere. It compiles and works properly: >> > >> > import qualified Data.ByteString as BS >> > import Text.Printf (printf) >> > toHex :: BS.ByteString -> String >> > toHex bytes = do >> > hex <- BS.unpack bytes >> > printf "%02x" hex >> > >> > I cannot understand the 'do' notation is required, because it seems to >> be a >> > pure function. I guess there's a monad hiding somewhere that my newbie >> mind >> > can't see. >> >> `toHex` is pure (non IO), but it has an /effect/. In this case, it takes >> advantage of the list monad to achieve non-determinism. >> >> Specifically, since >> >> unpack :: ByteString -> [Word8] >> >> printf (which in our case has signature (`String -> Char`) gets called >> on each of those [Word8]. The result will obviously be [Char], which >> `String` is an alias of. >> >> > So, I rewrote the code to remove the 'do stuff': >> > >> > [...] >> > toHex :: BS.ByteString -> String >> > toHex bytes = printf "02x" (BS.unpack bytes) >> >> A do-less version still is /monadic/, hence it will have >>= or >> >> or similar somewhere. This works: >> >> toHex2 :: BS.ByteString -> String >> toHex2 bytes = BS.unpack bytes >>= printf "%02x" >> >> and follows the reasoning above (feed every every Word8 to >> `printf "%02x"`). >> >> Does this answer your questions? >> -F >> >> _______________________________________________ >> 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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20190717/7710c233/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 133, Issue 2 *****************************************