<br><br><div class="gmail_quote">On Mon, Dec 15, 2008 at 12:17 PM, Jeff C. Britton <span dir="ltr"><<a href="mailto:jcb@iteris.com">jcb@iteris.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello,<br>
<br>
I have started reading "Yet Another Haskell Tutorial" by Hal Daum“e III which can be found here<br>
<a href="http://www.cs.utah.edu/%7Ehal/docs/daume02yaht.pdf" target="_blank">http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf</a><br>
<br>
One of the early examples in section 3.8 pg. 35<br>
is this<br>
<br>
askForWords = do<br>
putStrLn "Please enter a word:"<br>
word <- getLine<br>
if word == ""<br>
then return []<br>
else do<br>
rest <- askForWords<br>
return (word : rest)<br>
<br>
I want to print the returned list and everything I try fails.<br>
<br>
I have tried the following:<br>
<br>
printList l =<br>
if length l >= 1<br>
then do putStrLn (head l)<br>
printList (tail l)<br>
else putStrLn("")<br>
<br>
f = printList askForWords<br>
<br>
and I get<br>
Expression : printList askForWords<br>
*** Term : askForWords<br>
*** Type : IO [[Char]]<br>
*** Does not match : [[Char]]</blockquote><div><br>I believe one of the following will work for you:<br><br>f = askForWords >>= printList<br>f = do<br> words <- askForWords<br> printList words<br> <br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>
<br>
*************************************<br>
The exercise right below this asks for a very slight modification to read numbers instead.<br>
<br>
However, I am confused about how to convert strings to numbers.<br>
If I type in the hugs interactive console<br>
read "5" + 3 --> 8 -- ok perfect<br>
<br>
However<br>
read "5" gives<br>
ERROR - Unresolved overloading<br>
*** Type : Read a => a<br>
*** Expression : read "5"<br>
<br>
Yet page 33 of the tutorial has the following code:<br>
doGuessing num = do<br>
putStrLn "Enter your guess:"<br>
guess <- getLine<br>
let guessNum = read guess -- ok in let stmt, but not at repl prompt?</blockquote><div><br>The problem here is type inference. The statement read "5" has type "(Read a) => a", which basically means anything that implements the class "Read." When you do read "5" + 3, the read "5" gets the type of the 3. I assume that in the latter case, you use the expression guessNum in a way later on that the compiler can infer its type. <br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>
<br>
Anyway I take the info that has been presented and create this function:<br>
askForNumbers = do<br>
hSetBuffering stdin LineBuffering<br>
putStrLn "Give me a number (or 0 to stop)"<br>
numStr <- getLine<br>
let num = read numStr<br>
if num == 0<br>
then return []<br>
else do<br>
rest <- askForNumbers<br>
return (num : rest)<br>
<br>
However, when I try to use it, like say<br>
<br>
map sqrt askForNumbers<br>
<br>
ERROR - Type error in application<br>
*** Expression : map sqrt askForNumbers<br>
*** Term : askForNumbers<br>
*** Type : IO [Integer]<br>
*** Does not match : [a]</blockquote><div><br>Similar to above, try this:<br>do nums <- askForNumbers<br> map sqrt nums<br> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
<br>
*********************************************************<br>
<br>
Is there a way to write printList to handle Strings or numbers?<br>
Or should I write<br>
printList (map show askForNumbers)</blockquote><div><br>Note: you should probably do this using mapM_, but for simplicity, I'll do it using explicit recursion:<br><br><span style="font-family: courier new,monospace;">printList [] = putStrLn "" -- or return () if you don't want the extra blank line</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">printList (x:xs) = do putStrLn (show x)<br> printList xs<br><br></span>If you have any questions about how these worked, let me know!<br></div></div><br>
Michael<br>