6

I have a bit of code in my haskell program like so:

evaluate :: String -> IO ()
evaluate = ...

repl = forever $ do
  putStr "> " >> hFlush stdout
  getLine >>= evaluate

Problem is, when I press the delete key (backspace on windows), instead of deleting a character from the buffer, I get a ^? character instead. What's the canonical way of getting delete to delete a character when reading from stdin? Similarly, I'd like to be able to get the arrow keys to move a cursor around, etc.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
limp_chimp
  • 13,475
  • 17
  • 66
  • 105
  • 4
    The [haskeline](http://hackage.haskell.org/package/haskeline) library is the standard approach. I haven't used it, so I can't provide a simple example. – Carl Feb 22 '14 at 23:30
  • Yep, [haskeline](http://hackage.haskell.org/package/haskeline) is great and [LambdaCalculator](http://hackage.haskell.org/package/LambdaCalculator) is a really simple example use of that. – Thomas M. DuBuisson Feb 22 '14 at 23:39
  • Thanks for the tip; I'll definitely look into haskeline. Looks like lots of cool features. – limp_chimp Feb 23 '14 at 17:27
  • Just to follow up: wow, that's an awesome library. It even supports my favorite `Ctrl+K -> Ctrl+A` to clear a line! Woohoo! And it works in `ghci`. Thanks. – limp_chimp Feb 23 '14 at 18:41

1 Answers1

13

Compile the program and then run the compiled executable. This will give the correct behavior for the Delete key. For some reason interpreting the program screws up the use of Delete.

To compile the program, just invoke ghc like this:

$ ghc -O2 myProgram.hs

This will generate a myProgram executable that you can run from the command line:

$ ./myProgram

That will then give the correct behavior for Delete.

Gabriella Gonzalez
  • 34,863
  • 3
  • 77
  • 135
  • 1
    That's a bummer of a bug. Hopefully it gets fixed soon. Thanks a bunch for letting me know, Gabriel -- compiling to binary is one of the very last things I do, so it would have been a while before I tried that :) – limp_chimp Feb 23 '14 at 17:28
  • NB. To compile the program, the `.hs` file must contain `main` module. i.e. DONOT contain `module XXXX where` in the first line of the `.hs` file. Besides, if an alternative name of executive file is needed use `ghc -o nickname hakellcode.hs` when compiling. Correspondingly, use `./nickname` to run the executable file. – CN_Cabbage Aug 10 '22 at 08:45