Reputation: 8058
I'm having trouble taking in an input. Running it through functions. And outputting it. I have tried to do this in 2 different ways but neither work. Looking around online I see everyone only using the variable of the input inside the main. This would explain why I'm getting a "not in scope" error. But how would this be possible? Here are my two attempts.
result = lcm 3 inp
main = do
inp <- getLine
putStr result
and this:
main = do
inp <- getLine
putStr result
where
result = lcm 3 inp
Upvotes: 1
Views: 91
Reputation: 3521
Let's see the types of the things you use:
*Main> :t lcm
lcm :: Integral a => a -> a -> a
*Main> let result inp = lcm 3 inp
*Main> :t result
result :: Integral a => a -> a
But you read in a String:
*Main> :t getLine
getLine :: IO String
So, you need to convert a String to something like an integer, and convert an Inegral the result
returned back to Striing for printing.
main = do
inp <- getLine
putStr $ show $ result (read inp)
Upvotes: 0
Reputation: 3428
inp
exists only within the scope of the do
expression, which explains why you get an error in the first version.
As for the second version. it can be rewritten to:
main = e
where
e = do
inp <- getLine
putStr result
result = lcm 3 inp
The two where
bindings have different scopes, which is why a local binding from one expression is inaccessible from the other.
This, on the other hand, should work:
main = do
inp <- getLine
let result = lcm 3 inp
putStr result
result
is now defined within the do
notation scope, so it can use inp
. If you still want to use the where
clause, result
will need to accept inp
as an argument:
main = do
inp <- getLine
putStr result
where
result inp = lcm 3 inp
Upvotes: 4