howto make a simple selection in haskell? - haskell

i have a problem, this code dont work and i dont know why:
foo :: [String] -> IO [String]
foo input = do
choice <- getLine
if choice == "1" then do
putStrLn "good choice"
return input
else
return []

As is (with a bit of retabbing) the code works for me. if/else is difficult to get correct with the tabs. The Wiki article if/then/else should help.
foo :: [String] -> IO [String]
foo input = do
choice <- getLine
if choice == "1" then do
putStrLn "good choice"
return input
else
return []
Eliminating the inner do expression makes the if/then block a little more easy to indent.
foo2 input = do
choice <- getLine
if (choice == "1")
then (putStrLn "good choice" >> return input)
else (return [])

Related

How does Haskell "desugar" getline in this do block?

I've read a few books on Haskell but haven't coded in it all that much, and I'm a little confused as to what Haskell is doing in a certain case. Let's say I'm using getLine so the user can push a key to continue, but I don't really want to interpret that person's input in any meaningful way. I believe this is a valid way of doing this:
main = do
_ <- getLine
putStrLn "foo"
I understand the basic gist of what's this is doing. getLine returns an IO String, and putStrLn takes a String and returns IO (), so if I theoretically wanted to print what the user typed into the console, I'd basically utilize the >>= operator from the Monad class. In my case, I believe my code is equivalent to getLine >> putStrLn "foo" since I'm discarding the return value of getLine.
However, what if I do this instead?
main = do
let _ = getLine
putStrLn "foo"
In this case, we're setting up a sort of lambda to work with something that will take an IO String, right? I could write a printIOString function to print the user's input and that would work fine. When I'm not actually using that IO String, though, the program behaves strangely... getLine doesn't even prompt me for input; the program just prints out "foo".
I'm not really sure what the "desugared" syntax would be here, or if that would shed some light on what Haskell is doing under the hood.
Let's warm up with a few more complicated examples.
main = do
x
x
x
putStrLn "foo"
where
x = do
getLine
What do you expect this to do? I don't know about you, but what I expect is for the program to get three lines and then print something. If we desugar the second do block, we get
main = do
x
x
x
putStrLn "foo"
where x = getLine
Since this is the desugaring of the other one, it behaves the same, getting three lines before printing. There's another line of thought that arrives at the same answer, if you don't find this first one intuitive. "Referential transparency", one of the defining features of Haskell, means exactly that you can replace a "reference" to something (that is, a variable name) with its definition, so the previous program should be exactly the same program as
main = do
getLine
getLine
getLine
putStrLn "foo"
if we are taking the equation x = getLine seriously. Okay, so we have a program that reads three lines and prints. What about this one?
main = do
x
x
putStrLn "foo"
where x = getLine
Get two lines and print. And this one?
main = do
x
putStrLn "foo"
where x = getLine
Get one line and then print. Hopefully you see where this is going...
main = do
putStrLn "foo"
where x = getLine
Get zero lines and then print, i.e. just print immediately! I used where instead of let to make the opening example a bit more obvious, but you can pretty much always replace a where block with its let cousin without changing its meaning:
main = let x = getLine in do
putStrLn "foo"
Since we don't refer to x, we don't even need to name it:
main = let _ = getLine in do
putStrLn "foo"
and this is the desugaring of the code you wrote.
The first case is desugared like you expected:
main = getLine >>= \_ -> putStrLn "foo"
which is equivalent to
main = getLine >> putStrLn "foo"
In the second case,
main = do
let _ = getLine
putStrLn "foo"
is desugared as
main = let _ = getLine in putStrLn "foo"
Since the _ = getLine value is not needed to evaluate the RHS of the let expression, the compiler is free to ignore it and the IO effect is never executed, which is why you're not prompted for CLI input anymore.
Even though both cases ignored the result of getLine the difference is that the first case evaluates getLine in an IO context while the second case evaluates getLine as a pure value. In IO the side-effects must executed and sequenced together, but in a pure context the compiler is free to ignore unused values.
I wouldn't recommend doing this as it's not very idiomatic, but you could write something like
printIOString :: IO String -> IO ()
printIOString ios = ios >>= putStrLn
and use it like printIOString getLine
According to https://stackoverflow.com/tags/do-notation/info,
do { let { _ = getLine } ; putStrLn "foo" }
=
do { let { _ = getLine } in putStrLn "foo" }
=
let { _ = getLine } in putStrLn "foo"
which by Haskell semantics is equivalent to
getLine & (\ _ -> putStrLn "foo")
=
putStrLn "foo"
(with x & f = f x), whereas indeed
do { _ <- getLine ; putStrLn "foo" }
=
getLine >>= (\ _ -> putStrLn "foo")
which can't be further simplified.

How do I create a menu in Haskell for my application [duplicate]

I would like to know what is the best solution to create simple menu with functionality described below (pseudo code) just like I'm used to:
while (true) {
x = readLine();
case (x):
x == "1" then do sth1 function
x == "2" then do sth2 function
}
Or maybe any other ideas on how to make a menu not in the pattern described above?
There's a few cool packages for high level ways to construct command line systems in general:
ui-command: A framework for friendly commandline programs
haskeline: A command-line interface for user input, written in Haskell.
HCL: High-level library for building command line interfaces.
I particularly like ui-command, as it is an entire framework for your command line tools: It will dispatch to handler functions that you provide for each command, and also provide command-specific help to the user.
The goal is a polished feeling, rather than a hackish feeling.
Something like
menu :: IO ()
menu = do
putStrLn . unlines $ map concatNums choices
choice <- getLine
case validate choice of
Just n -> execute . read $ choice
Nothing -> putStrLn "Please try again"
menu
where concatNums (i, (s, _)) = show i ++ ".) " ++ s
validate :: String -> Maybe Int
validate s = isValid (reads s)
where isValid [] = Nothing
isValid ((n, _):_)
| outOfBounds n = Nothing
| otherwise = Just n
outOfBounds n = (n < 1) || (n > length choices)
choices :: [(Int, (String, IO ()))]
choices = zip [1.. ] [
("DoSomething", foo)
, ("Quit", bar)
]
execute :: Int -> IO ()
execute n = doExec $ filter (\(i, _) -> i == n) choices
where doExec ((_, (_,f)):_) = f
foo = undefined
bar = undefined
You could probably split the enumerating in "choices" so you only have the descriptions and functions inside it, a little bit of separation, but this works. Evaluating the "menu" function will let you choose what to do!
Here's another example that is a little more menu-like, in that it reads single characters in reacts directly, without requiring the user to press enter.
import System.IO
import System.Exit
import Control.Monad
main = forever (printMenu >> readChoice >>= menuAction)
printMenu = putStr "\np)rint 'Hello, world!'\ne)xit\nyour choice: " >> hFlush stdout
readChoice = hSetBuffering stdin NoBuffering >> hSetEcho stdin False >> getChar
menuAction 'p' = putStrLn "\nHello, world!"
menuAction 'e' = exitSuccess
menuAction _ = hPutStrLn stderr "\nInvalid choice."

Syntax confusion (do block)

Sorry for a poor title, feel free to edit. I can't understand what the problem is, so it might be altogether wrong. Below is the code (this is after I've done like a hundred of permutations and different sequences of let-do-if and tabulation, and I'm exhausted):
-- The last statement in a 'do' construct must be an expression
numberOfGoods :: IO String
numberOfGoods = do putStrLn "Enter year (2000-2012):\n"
let intYear = readYear
in if (intYear < 2000 || intYear > 2012)
then error "Year must be withing range: 2000-2012"
else
c <- readIORef connection
[Only i] <- query_ c ("select count('*')" ++
"from table" ++
"where ((acquisition_date <= " ++
(formatDate intYear) ++
") and ((sale_date is null) or " ++
"(sale_date < " ++
(formatDate intYear) ++ ")))")
return i
readYear :: Integer
readYear = do
year <- getLine
read year :: Integer
Something that would meant to be so simple... I still don't understand what is wrong with the code above. Please, if you could kindly explain the source of the error, that would be great.
I did read about do, let-in and if-then-else, and I don't see any errors here from what I could understand from the manual.
Ideally, if there are alternatives, I would like very much to reduce the amount of the wasted white space on the left.
Thank you.
readYear is not an Integer, it's an IO action that can be run to read input and convert the input to an integer -- in other words, IO Integer. And as it's an IO action, you'll need a return to use whatever read year as result of getYear. That is:
getYear :: IO Integer
getYear = do year <- getLine
return (read year)
This also means you use it like intYear <- readYear instead of using let (well, you could, but you'd store the IO action instead of running it, and the type of intYear would be wrong). That is:
numberOfGoods :: IO String
numberOfGoods = do putStrLn "Enter year (2000-2012):\n"
intYear <- readYear
...
do does not extend over if, rather you need to start again with do if you want a sequence of actions in the then or else branch. That is:
else
c <- readIORef connection
...
return i
should be roughly:
else do c <- readIORef connection
...
return i
As for reducing whitespace, consider pushing the validation logic into readYear. Implementing this is left as an exercise to the reader ;)
As an aside, you don't need in when using let in a do block (but only there!), you can simply state:
do do_something
let val = pure_compuation
something_else_using val
You need a new do for every block of monadic functions: simply writing functions in a row has no meaning, regardless of whether they're monadic or pure. And everything where the value comes from the IO monad must itself give its return value in the monad.
numberOfGoods :: IO String
numberOfGoods = do putStrLn "Enter year (2000-2012):\n" -- why extra '\n'?
intYear <- readYear -- readYear expects user input <- must be monadic
if (intYear < 2000 || intYear > 2012)
then error "Year must be withing range: 2000-2012"
else do
c <- readIORef connection
[Only i] <- query_ c ("select count('*')" ++
"from table" ++
"where ((acquisition_date <= " ++
(formatDate intYear) ++
") and ((sale_date is null) or " ++
"(sale_date < " ++
(formatDate intYear) ++ ")))")
return i
readYear :: IO Integer
readYear = do
year <- getLine
return $ read year :: Integer
Why is an extra do needed...
Well, the thing with do in Haskell is that it's really just syntactic sugar. Let's simplify your function a little
nOG :: IO String
nOG = do putStrLn "Prompt"
someInput <- inputSth
if condition someInput
then error "Bloap"
else do c <- inputSthElse
[only] <- query_ c
return only
what this actually means is
nOG :: IO String
nOG = putStrLn "Prompt"
>> inputSth
>>= (\someInput ->
if condition someInput
then error "Bloap"
else inputSthElse
>>= (\s -> query_ c
>>= (\[only] -> return only )
)
)
Where you should be able to see that if behaves in exactly the same way as it does in a pure functional expression like shade (r,g,b) = if g>r && g>b then "greenish" else "purpleish". It doesn't in any way "know" about all the IO monad stuff going on around it, so it can't infer that there should again be a do block in one of its branches.

Haskell IF Else

input <- readLn
if (input == 0)
then
putStr "0"
else if (input ==1)
then
putStr "1"
else if (input ==2)
in this kind of senario how to use multiple putStr with in a then or else if ?
when i try getting a error
Type error in application
*** Expression : putStr "0" putStr "0"
*** Term : putStr
*** Type : String -> IO ()
*** Does not match : a -> b -> c -> d
Use do-notation:
do
a <- something
if a
then
do
cmd1
cmd2
else
do
cmd3
cmd4
cmd5 -- this comes after the 'then' and the 'else'
The canonical explanation for this is that you want to form a new monadic value out of two existing ones. Let's look at the type of putStr,
IO ()
That means it's some black box, that when executed, will "return" the (one-and-only) value of unit type. The key idea behind monadic computation is that you have a combinator >>= which will put together two monadic expressions, feeding the result of one into the next (more accurately, a function that creates the next). One critical point is that the IO library provides this combinator, meaning that,
It [IO in this case] could skip the second monadic value, for example when the first one throws an exception.
It can pass other data around, in the case of IO a RealWorld state containing open file handles, etc.
It can "ensure" that the first one evaluates first, unlike most lambda expression evaluations, where the outermost ("last") terms are expanded first. This is important for print, where the first print needs to change the world first.
In your case, use it like this,
putStr "0" >>= (\c -> putStr "0")
There's a shortcut, of course,
putStr "0" >> putStr "0"
and the do-notation, as mentioned by another poster, which is yet more syntax sugar,
do
putStr "0"
putStr "0"
For this contrived example, you may as well use a case, like this:
main = readLn >>= \input -> case input of
0 -> putStrLn "0"
1 -> putStrLn "0"
2 -> putStr "0"
>> putStrLn "0"
3 -> putStr "0"
>> putStr "0"
>> putStrLn "0"
_ -> putStrLn "infinite"
This could be perhaps be more readable with do syntax, but I wanted to show it without do syntax first, just to emphasize that do-syntax is just syntax and doesn't actually do anything special. Here it is with do-syntax.
main = do
input <- readLn
case input of
0 -> putStrLn "0"
1 -> putStrLn "0"
2 -> do putStr "0"
putStrLn "0"
3 -> do putStr "0"
putStr "0"
putStrLn "0"
_ -> putStrLn "infinite"

Simple text menu in Haskell

I would like to know what is the best solution to create simple menu with functionality described below (pseudo code) just like I'm used to:
while (true) {
x = readLine();
case (x):
x == "1" then do sth1 function
x == "2" then do sth2 function
}
Or maybe any other ideas on how to make a menu not in the pattern described above?
There's a few cool packages for high level ways to construct command line systems in general:
ui-command: A framework for friendly commandline programs
haskeline: A command-line interface for user input, written in Haskell.
HCL: High-level library for building command line interfaces.
I particularly like ui-command, as it is an entire framework for your command line tools: It will dispatch to handler functions that you provide for each command, and also provide command-specific help to the user.
The goal is a polished feeling, rather than a hackish feeling.
Something like
menu :: IO ()
menu = do
putStrLn . unlines $ map concatNums choices
choice <- getLine
case validate choice of
Just n -> execute . read $ choice
Nothing -> putStrLn "Please try again"
menu
where concatNums (i, (s, _)) = show i ++ ".) " ++ s
validate :: String -> Maybe Int
validate s = isValid (reads s)
where isValid [] = Nothing
isValid ((n, _):_)
| outOfBounds n = Nothing
| otherwise = Just n
outOfBounds n = (n < 1) || (n > length choices)
choices :: [(Int, (String, IO ()))]
choices = zip [1.. ] [
("DoSomething", foo)
, ("Quit", bar)
]
execute :: Int -> IO ()
execute n = doExec $ filter (\(i, _) -> i == n) choices
where doExec ((_, (_,f)):_) = f
foo = undefined
bar = undefined
You could probably split the enumerating in "choices" so you only have the descriptions and functions inside it, a little bit of separation, but this works. Evaluating the "menu" function will let you choose what to do!
Here's another example that is a little more menu-like, in that it reads single characters in reacts directly, without requiring the user to press enter.
import System.IO
import System.Exit
import Control.Monad
main = forever (printMenu >> readChoice >>= menuAction)
printMenu = putStr "\np)rint 'Hello, world!'\ne)xit\nyour choice: " >> hFlush stdout
readChoice = hSetBuffering stdin NoBuffering >> hSetEcho stdin False >> getChar
menuAction 'p' = putStrLn "\nHello, world!"
menuAction 'e' = exitSuccess
menuAction _ = hPutStrLn stderr "\nInvalid choice."

Resources