in Education by
I'm learning Haskell in the hope that it will help me get closer to functional programming. Previously, I've mostly used languages with C-like syntax, like C, Java, and D. I have a little question about the coding style of an if/else control block used by the tutorial on Wikibooks. The code looks like the following: doGuessing num = do putStrLn "Enter your guess:" guess <- getLine if (read guess) < num then do putStrLn "Too low!" doGuessing num else if (read guess) > num then do putStrLn "Too high!" doGuessing num else do putStrLn "You Win!" It makes me confused, because this coding style totally violates the recommended style in C-like languages, where we should indent if, else if, and else at the same column. I know it just does not work in Haskell, because it would be a parse error if I indented else at the same column as if. But what about the following style? I think it is much more clear than the above one. But since the above is used by Wikibooks and Yet Another Haskell Tutorial, which is marked "best tutorial available online" at the official Haskell website, I'm not sure whether this coding style is a convention in Haskell programs. doGuessing num = do putStrLn "Enter your guess:" guess <- getLine if (read guess) < num then do putStrLn "Too low!" doGuessing num else if (read guess) > num then do putStrLn "Too high!" doGuessing num else do putStrLn "You Win!" So, I'm curious about which coding style is used more often—or is there another coding style for this piece of code? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Haskell style is functional, not imperative! Rather than "do this then that," think about combining functions and describing what your program will do, not how. In the game, your program asks the user for a guess. A correct guess is a winner. Otherwise, the user tries again. The game continues until the user guesses correctly, so we write that: main = untilM (isCorrect 42) (read `liftM` getLine) This uses a combinator that repeatedly runs an action (getLine pulls a line of input and read converts that string to an integer in this case) and checks its result: untilM :: Monad m => (a -> m Bool) -> m a -> m () untilM p a = do x <- a done <- p x if done then return () else untilM p a The predicate (partially applied in main) checks the guess against the correct value and responds accordingly: isCorrect :: Int -> Int -> IO Bool isCorrect num guess = case compare num guess of EQ -> putStrLn "You Win!" >> return True LT -> putStrLn "Too high!" >> return False GT -> putStrLn "Too low!" >> return False The action to be run until the player guesses correctly is read `liftM` getLine Why not keep it simple and just compose the two functions? *Main> :type read . getLine :1:7: Couldn't match expected type `a -> String' against inferred type `IO String' In the second argument of `(.)', namely `getLine' In the expression: read . getLine The type of getLine is IO String, but read wants a pure String. The function liftM from Control.Monad takes a pure function and “lifts” it into a monad. The type of the expression tells us a great deal about what it does: *Main> :type read `liftM` getLine read `liftM` getLine :: (Read a) => IO a It's an I/O action that when run gives us back a value converted with read, an Int in our case. Recall that readLine is an I/O action that yields String values, so you can think of liftM as allowing us to apply read “inside” the IO monad. Sample game: 1 Too low! 100 Too high! 42 You Win!

Related questions

0 votes
    What would be a good place to go to understand arrows? Ideally, I am just looking for some place ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I am trying to define instances for Functor,Applicative and Monad for the following type: data BTree a=Leaf ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Still new to Haskell, I have hit a wall with the following: I am trying to define some type ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Source coding block is used for? a) Compressing b) Digitizing c) A/D conversion d) All of the mentioned...
asked Dec 31, 2022 in Technology by JackTerrance
0 votes
    The client expects a timely response from the service and might even block while it waits. This represents the _________ client ... B. One to one C. One to many D. Synchronous...
asked Jan 10, 2023 in Technology by JackTerrance
0 votes
    I was just discussing with some colleagues about Java constructors, design-patterns and good way to initialize ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    My If Else statement in VB.net is randomly displaying either of the If or ElseIf condition that I made ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    My If Else statement in VB.net is randomly displaying either of the If or ElseIf condition that I made ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I need to determine the status of a parent object based on 2 variables of each of its children. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I need to determine the status of a parent object based on 2 variables of each of its children. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    I am using AA for almost 6 months and I found that by default I am not able to see any lines/dotted lines/ ... to be set in properties? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am using AA for almost 6 months and I found that by default I am not able to see any lines/dotted lines/ ... to be set in properties? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    int fun(int n) { if(n!=0) { return n – fun(n-5); } else { return n; } } int main(){ int n = 20, z; z = fun(n); printf(“%d”, z); } Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    int find(int j) { if(>1X j=find(j/ 10)+(%10); } else { j=0; return j; void main() int i=1000, int k; k=find(i); printf(“%d”, k); Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    s=ScHoO12@CoM' k=1en(s) m= for i in range (0 , k ) : if(s ( i ) . isupper ( ) ) : m=m+s ( i ) . ... else : m=m+ bb print ( m )| Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
...