So I'm trying to print out a line of text to the terminal window using a returned value in a compiled code. The program looks like this:
module Main
where
import System.IO
main = do
askForWords
askForWords = do
putStrLn "Pleas enter a word:"
word <- getLine
if word == ""
then return []
else do
rest <- askForWords
return (word ++ " " ++ rest)
When I run it in the GHCi it works fine
*Main> main
Pleas enter a word:
Hello
Pleas enter a word:
World
Pleas enter a word:
"Hello World "
*Main>
When I try to run the Unix executable file the program don't print the last string
% /Users/tobyone/Workspace/Haskell/yaht/Yaht ; exit;
Pleas enter a word:
Hello
Pleas enter a word:
World
Pleas enter a word:
[Process completed]
I have tried printing out askForWords in main with putStrLn but get the error
<interactive>:2:10: error:
Couldn't match type ‘IO [Char]’ with ‘[Char]’
...
...
putStrLn? It seems you did that wrong, but if you don't show your mistake we can't tell how to correct it.1+2, it actualy runsprint (1+2). When you writereturn (1+2)it actually runsdo it <- return (1+2) ; print it. In your code, you runaskForWordsin yourmain, but silently discard its result. GHCi would have converted that todo it <- askForWords ; print it, but in a program you have to be explicit about that (and possibly useputStrLninstead ofprint).