Saturday 30 April 2016

Haskell: Read Input from console


You can read input by using getLine.

For example,
name <- getLine

The value returned by getLine method is assigned to the variable name.

print_data.hs
user_info = do
    putStrLn "Please enter your name:"
    name <- getLine
    putStrLn "Please enter your age:"
    age <- getLine
    putStrLn ("Hello, " ++ name ++ ", You are " ++ age ++ " Years old")

*Main> :load print_data.hs
[1 of 1] Compiling Main             ( print_data.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> user_info
Please enter your name:
ptr
Please enter your age:
28
Hello, ptr, You are 28 Years old


Note
<- can be used with any action(statement) except the last.

Previous                                                 Next                                                 Home

No comments:

Post a Comment