Two small questions from the section "Making a safe RPN calculator" in LYAH

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Two small questions from the section "Making a safe RPN calculator" in LYAH

Olumide
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Two small questions from the section "Making a safe RPN calculator" in LYAH

David McBride
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 <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners