<div dir="ltr"><div>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.<br></div><div><br></div><div>2) Because [(x,"")] does not match [(1.0," wawawawa")]. An empty string does not match a string with characters in it.<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 11, 2018 at 1:24 PM, Olumide <span dir="ltr"><<a href="mailto:50295@web.de" target="_blank">50295@web.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Dear List,<br>
<br>
Question 1:<br>
In the section "Making a safe RPN calculator" of LYAH (Chapter 14 -- <a href="http://learnyouahaskell.com/for-a-few-monads-more#useful-monadic-functions" rel="noreferrer" target="_blank">http://learnyouahaskell.com/fo<wbr>r-a-few-monads-more#useful-mon<wbr>adic-functions</a>) there is an expression: read numberString:xs, take from the last line of the function<br>
<br>
foldingFunction :: [Double] -> String -> [Double]<br>
foldingFunction (x:y:ys) "*" = (x * y):ys<br>
foldingFunction (x:y:ys) "+" = (x + y):ys<br>
foldingFunction (x:y:ys) "-" = (y - x):ys<br>
foldingFunction xs numberString = read numberString:xs<br>
<br>
My first question is why is the read function called before the cons operator?<br>
<br>
Question 2:<br>
The same section of the book also introduces the reads function which is used to implement the readMaybe function and a refactored foldingFunction<br>
<br>
readMaybe :: (Read a) => String -> Maybe a<br>
readMaybe st = case reads st of [(x,"")] -> Just x<br>
_ -> Nothing<br>
<br>
foldingFunction :: [Double] -> String -> Maybe [Double]<br>
foldingFunction (x:y:ys) "*" = return ((x * y):ys)<br>
foldingFunction (x:y:ys) "+" = return ((x + y):ys)<br>
foldingFunction (x:y:ys) "-" = return ((y - x):ys)<br>
foldingFunction xs numberString = liftM (:xs) (readMaybe numberString)<br>
<br>
I'd like to know why the foldingFunction returns Nothing in the following example:<br>
<br>
ghci> foldingFunction [] "1 wawawawa"<br>
Nothing<br>
<br>
Considering that reads "1 wawawawa" does not return Nothing, as follows<br>
<br>
ghci> reads "1 wawawawa" :: [(Double,String)]<br>
[(1.0," wawawawa")]<br>
<br>
Regards,<br>
<br>
- Olumide<br>
<br>
______________________________<wbr>_________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bi<wbr>n/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>