I'm trying to get a handle on the basics of Haskell by creating a Notational Velocity style find-as-you-type file search on the command line. Instead of tackling the whole problem, I'm trying a very basic version of it: There exists a file with 10 lines, and a 3 letter word on each line. After each letter I type, I want to update the display of a list of line numbers that might contain the word I'm typing, based on my input so far.
Can someone demonstrate a Haskell program that does this? I think my problem lies in forcing reevaluation on every character input. Thanks a million in advance.
I won't try to write the whole Haskell program you're asking for, but here is a very short example showing just the bit you claim to be stuck on at the moment: doing something after every key press. We won't do anything exciting (just bump a number and print it out), but it will show how to do that one small task, and you perhaps can start hacking from there.
The only thing you really need to know that you seem not to is that you can turn off line-buffering on the input.
import System.IO
loop n = do
c <- getChar
print n
-- do whatever recalculation you need to do here, using
-- n (which can be more complicated than an Integer, as
-- it is here, of course) and c (which is a Char
-- representing the key the user whacked)
-- our recalculation is just to increase n by one
loop (n+1)
main = do
hSetBuffering stdin NoBuffering -- do this once before anything else
loop 0
Studing Reactive programming could be a good starting.
For this a good library seem to be reactive banana
There some base example.
If you want to know more, on FRP, an excellent topic on stack will give you a clear overview.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With