Wednesday, September 8, 2010

Simple commandline/eval loop in Haskell

Played with Haskell a little more, and put together a simple eval loop that you can tie your own functions to. It gives a prompt, reads input, calls a function on that input, and then loops again. The next step is probably to put an honest parser on it, but for now I think it shows off how simply you can do the loop. (There may be even better ways, if you know of one post a comment)


doSomething s =
  "**" ++ s ++ "**"
commandln = do
  putStr "> "
  x <- getLine
  if x == "quit"
    then return ()
    else putStrLn (doSomething x) >> commandln


Edit: Does anyone know if that's tail-recursive? And, if it's not, how it could be changed to be?

No comments:

Post a Comment