[Haskell-beginners] Maybe Return?

Ramnath R Iyer casual.dodo at gmail.com
Sun Jul 31 18:44:14 UTC 2016


Hi all,

I'm trying to write a fairly simple program to understand Haskell in
practice. I am implementing a simple REPL where I can write some text, and
that text can be parsed into a command with arguments. Then, I would like
to be able to turn this into a 'plugin' system of sorts, with a
module/logic per command.

I understand the Text.Parsec library may already provide these things out
of the box, but for now, I would prefer to reinvent the wheel.

The program below is what I have working right now (very trivial). I want
to modify this program, so that the `evaluate input` returns not a String,
but a type X that includes information on whether to "continue" and show a
prompt after rendering the result, or exit altogether. So the specific
questions I have here are:

1. What would be a sensible type signature for X?
2. Assuming X to exist, what is the right way to modify main to use it? My
current intuition is to modify the outputStrLn ... loop statements to
include an if-then-else, but I'm not sure what the right modification is
(all my attempts seemed like imperative programming and failed compilation
unsurprisingly).

import Evaluator
import System.Console.Haskeline

main :: IO ()
main = runInputT defaultSettings loop
  where
    loop :: InputT IO ()
    loop = do
      line <- getInputLine ">> "
      case line of
        Nothing     -> return ()
        Just "quit" -> return ()
        Just input  -> do
          outputStrLn $ "Executing: " ++ (evaluate input)
          loop

module Evaluator where

evaluate :: String -> String
evaluate value = value
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160731/63beb504/attachment.html>


More information about the Beginners mailing list