I'm trying to read data as Doubles from stdin, manipulate them and write them as well. What I've come up with so far is:
import qualified Data.ByteString.Lazy as B
import Data.Binary.IEEE754
import Data.Binary.Get
-- gives a list of doubles read from stdin
listOfFloat64le = do
empty <- isEmpty
if empty
then return []
else do v <- getFloat64le
rest <- listOfFloat64le
return (v : rest)
-- delay signal by one
delay us = 0 : us
-- feedback system, add delayed version of signal to signal
sys us = zipWith (+) us (delay us)
main = do
input <- B.getContents
let hs = sys $ runGet listOfFloat64le input
print $ take 10 hs
The idea is to fead data to the program which is then passed through a feedback system before it is written to stdout. Although right now it just prints the first 10 values.
This works but does not seem to evaluate lazily. I.e it has to read all the input into memory. So:
dd if=/dev/urandom bs=8 count=10 | runhaskell feedback.hs
will work just fine but:
dd if=/dev/urandom | runhaskell feedback.hs
will not. My guess is it's the listOfFloat64le
function that makes things not work properly. So how do I create an iterable to pass into my sys
function without having to read everything into memory?
I'm not a very experienced haskeller.