Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7556471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:51:54+00:00 2026-05-30T11:51:54+00:00

I have written a small piece of code which handles the input of a

  • 0

I have written a small piece of code which handles the input of a console:

main :: IO ()
main = do
  input <- readLine "> "
  loop input

loop :: String -> IO ()
loop input = do
  case input of
    [] -> do
      new <- readLine "> "
      loop new
    "quit" ->
      return ()
    _ -> do
      handleCommand input
      new <- readLine "> "
      loop new

handleCommand :: String -> IO ()
handleCommand command = do
  case command of
    "a" -> putStrLn "it was a"
    "b" -> putStrLn "it was b"
    _ -> putStrLn "command not found"

readLine :: String -> IO String
readLine prompt = do
  putStr prompt
  line <- getLine
  return line

The code works fine, but it looks ugly and is redundant. In Scala I succeeded to write it shorter:

object Test extends App {
  val reader = Iterator.continually(readLine("> "))
  reader takeWhile ("quit" !=) filter (_.nonEmpty) foreach handleCommand

  def handleCommand(command: String) = command match {
    case "a" => println("it was a")
    case "b" => println("it was b")
    case _ => println("command not found")
  }
}

I tried to use higher-order functions with the IO Monad in Haskell but I failed. Can someone give me an example how to shorten the Haskell code?

Another problem is that the order of output is different. In Scala it is correct:

$ scala Test
> hello
command not found
> a
it was a
> b
it was b
> quit

Whereas in Haskell it is not:

$ ./test
hello
> command not found
a
> it was a
b
> it was b
quit
> %

How to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:51:55+00:00Added an answer on May 30, 2026 at 11:51 am
    import System.IO
    
    main = putStr "> " >> hFlush stdout >> getLine >>= \input ->
        case input of
            "quit" -> return ()
            "a"    -> putStrLn "it was a" >> main
            "b"    -> putStrLn "it was b" >> main
            _      -> putStrLn "command not found" >> main
    

    Shorter and clearer than Scala imo.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small piece of code, written in python which has a Generate
What I have written a small piece of code to find and remove if
I have just written a small piece of code and it struck me that
I have written some code in which a small part of the code takes
I have written a small piece of code to calculate quadratic equations, but if
I have written a small piece of code regarding the dynamic loading of assemblies
I've written a small piece of code which opens up an Excel document on
I have written small C++ console application and this is source code : #include<stdio.h>
i have written a small piece of code. This code first blocks the {SIGSEGV},
I have written a small piece of code that would perform Run length encoding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.