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. Two small questions from the section "Making a safe RPN calculator" in LYAH (Olumide) 2. Re: Two small questions from the section "Making a safe RPN calculator" in LYAH (David McBride) 3. The type class Read (mrx) 4. Re: The type class Read (PY) 5. Re: The type class Read (Francesco Ariis) ---------------------------------------------------------------------- Message: 1 Date: Wed, 11 Jul 2018 18:24:19 +0100 From: Olumide <50...@web.de> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Two small questions from the section "Making a safe RPN calculator" in LYAH Message-ID: <d89b0202-bc66-85ca-e600-e6dba1821...@web.de> Content-Type: text/plain; charset=utf-8; format=flowed Dear List, Question 1: In the section "Making a safe RPN calculator" of LYAH (Chapter 14 -- http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions) there is an expression: read numberString:xs, take from the last line of the function foldingFunction :: [Double] -> String -> [Double] foldingFunction (x:y:ys) "*" = (x * y):ys foldingFunction (x:y:ys) "+" = (x + y):ys foldingFunction (x:y:ys) "-" = (y - x):ys foldingFunction xs numberString = read numberString:xs My first question is why is the read function called before the cons operator? Question 2: The same section of the book also introduces the reads function which is used to implement the readMaybe function and a refactored foldingFunction readMaybe :: (Read a) => String -> Maybe a readMaybe st = case reads st of [(x,"")] -> Just x _ -> Nothing foldingFunction :: [Double] -> String -> Maybe [Double] foldingFunction (x:y:ys) "*" = return ((x * y):ys) foldingFunction (x:y:ys) "+" = return ((x + y):ys) foldingFunction (x:y:ys) "-" = return ((y - x):ys) foldingFunction xs numberString = liftM (:xs) (readMaybe numberString) I'd like to know why the foldingFunction returns Nothing in the following example: ghci> foldingFunction [] "1 wawawawa" Nothing Considering that reads "1 wawawawa" does not return Nothing, as follows ghci> reads "1 wawawawa" :: [(Double,String)] [(1.0," wawawawa")] Regards, - Olumide ------------------------------ Message: 2 Date: Wed, 11 Jul 2018 13:30:51 -0400 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Two small questions from the section "Making a safe RPN calculator" in LYAH Message-ID: <can+tr42bytuiyeliptgbca_mhcn+ynpeemqfvgdhe9eqigx...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 1) Not entirely sure what you mean. The expression is parsed as (read numberString) : xs, which means that the result number is prepended to the front of the list. 2) Because [(x,"")] does not match [(1.0," wawawawa")]. An empty string does not match a string with characters in it. On Wed, Jul 11, 2018 at 1:24 PM, Olumide <50...@web.de> wrote: > Dear List, > > Question 1: > In the section "Making a safe RPN calculator" of LYAH (Chapter 14 -- > http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions) > there is an expression: read numberString:xs, take from the last line of > the function > > foldingFunction :: [Double] -> String -> [Double] > foldingFunction (x:y:ys) "*" = (x * y):ys > foldingFunction (x:y:ys) "+" = (x + y):ys > foldingFunction (x:y:ys) "-" = (y - x):ys > foldingFunction xs numberString = read numberString:xs > > My first question is why is the read function called before the cons > operator? > > Question 2: > The same section of the book also introduces the reads function which is > used to implement the readMaybe function and a refactored foldingFunction > > readMaybe :: (Read a) => String -> Maybe a > readMaybe st = case reads st of [(x,"")] -> Just x > _ -> Nothing > > foldingFunction :: [Double] -> String -> Maybe [Double] > foldingFunction (x:y:ys) "*" = return ((x * y):ys) > foldingFunction (x:y:ys) "+" = return ((x + y):ys) > foldingFunction (x:y:ys) "-" = return ((y - x):ys) > foldingFunction xs numberString = liftM (:xs) (readMaybe numberString) > > I'd like to know why the foldingFunction returns Nothing in the following > example: > > ghci> foldingFunction [] "1 wawawawa" > Nothing > > Considering that reads "1 wawawawa" does not return Nothing, as follows > > ghci> reads "1 wawawawa" :: [(Double,String)] > [(1.0," wawawawa")] > > Regards, > > - Olumide > > _______________________________________________ > 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/20180711/0353776d/attachment-0001.html> ------------------------------ Message: 3 Date: Thu, 12 Jul 2018 12:05:52 +0200 From: mrx <patrik....@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] The type class Read Message-ID: <CANzOjBicRt1z9KyaFcwmy_0Wr=hd5CFNHLE1Eh=rsfgafhb...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hi, I'm trying to make sense of the type class `Read`. It feels to be like a function performing casting to me, similar to what you have in C and Java. The weird thing is that the casting doesn't have to make sense, type-wise. So `read "5" :: Int` produce the integer 5 which make sense but `read "(3,'a')" :: Int` doesn't make sense to me but ghci accepts it! This is seeing Read as a function, as it's a type class i suspect I'm missing something. Quite possibly quite a lot... Why is the type class called `Read`? What am I missing above? // Patrik Iselind -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20180712/24086d45/attachment-0001.html> ------------------------------ Message: 4 Date: Thu, 12 Jul 2018 14:08:29 +0300 From: PY <aqua...@gmail.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] The type class Read Message-ID: <e4517791-c30b-0908-2e29-12af807f9...@gmail.com> Content-Type: text/plain; charset="utf-8"; Format="flowed" read "(3,'a')"::(Int, Char) returns expected tuple. But with ":: Int" leads to runtime exception, because it calls `readsPrec` of Int and can not parse such string. But readsPrec of (a,b) where both are readable parses it successfully. 12.07.2018 13:05, mrx wrote: > Hi, > > I'm trying to make sense of the type class `Read`. It feels to be like > a function performing casting to me, similar to what you have in C and > Java. > > The weird thing is that the casting doesn't have to make sense, > type-wise. So `read "5" :: Int` produce the integer 5 which make sense > but `read "(3,'a')" :: Int` doesn't make sense to me but ghci accepts it! > > This is seeing Read as a function, as it's a type class i suspect I'm > missing something. Quite possibly quite a lot... > > Why is the type class called `Read`? > What am I missing above? > > // Patrik Iselind > > > _______________________________________________ > 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/20180712/b55af5cf/attachment-0001.html> ------------------------------ Message: 5 Date: Thu, 12 Jul 2018 13:14:32 +0200 From: Francesco Ariis <fa...@ariis.it> To: beginners@haskell.org Subject: Re: [Haskell-beginners] The type class Read Message-ID: <20180712111432.ht3mrwj6x2gwt...@x60s.casa> Content-Type: text/plain; charset=utf-8 Hello Patrik, On Thu, Jul 12, 2018 at 12:05:52PM +0200, mrx wrote: > Why is the type class called `Read`? > What am I missing above? we can say any instance of `Read` has to implement read :: Read a => String -> a -- the actual instance implements a different function, -- but that's not relevant for our example So, a typeclass (Read, capital `r`) gives us a function (`read`, lower-case `r`). The function goes from `String` (and no other things) to our implemented type. As you discovered, the conversion happens at runtime, the compiler has no way to check it beforehand, and things might explode during execution λ> read "18" :: Char *** Exception: Prelude.read: no parse λ> read "diciotto" :: Int *** Exception: Prelude.read: no parse It is often a good idea to use `readMay` (from Safe) to handle failures locally without throwing an exception readMay :: Read a => String -> Maybe a Does this answers your question? If you have any further doubts, shoot again -F ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 121, Issue 13 ******************************************