Reputation: 59
I wrote a function in Haskell to return a list. I want to get the return value of the function and store in another variable for future use. The code I have written is given below
module Main
where
import System.IO
main =do
let list=doCal
doCal =do
hSetBuffering stdin LineBuffering
putStrLn "Give me a number (or 0 to stop):"
num <- getLine
let number = read num
if number==0
then
return []
else do
rest <-doCal
return (number:rest)
When I try to run this, I got an error
Cal.hs:7:9: error:
The last statement in a 'do' block must be an expression
let list = doCal
|
7 | let list=doCal
| ^^^^^^^^^^^^^^
Failed, no modules loaded.
How to store the return value of a function to a variable in Haskell?
Upvotes: 0
Views: 2456
Reputation: 17806
The same way you already did it with rest
in doCal
.
main = do
list <- doCal
But you also need to have main
do something and return a value. So you might write
main = do
list <- doCal
print list
This will work because print list
returns ()
, which is a value in Haskell (its just the only value of its type).
Upvotes: 8