[Haskell-beginners] Streams

mike h mike_k_houghton at yahoo.co.uk
Thu Jul 27 08:33:33 UTC 2017


Hi,

I'm looking at this:

data Stream a = Cons a (Stream a) 

and with 

streamFromSeed :: (a -> a) -> a -> Stream a
streamFromSeed f a = Cons a (streamFromSeed f (f a)) 

I can do, for example,

evens :: Stream Integer
evens = streamFromSeed (+2) 0

what I'd like to do is take one item at a time from the stream. My first shot was 

next :: (Stream a) -> (Stream a, a)

but that means I need to keep a ref to the stream that is returned in the tuple which makes for messy code
for subsequent calls to next. I sense that the State monad needs to be involved but I'm not sure exactly how as 
my experiments with State still required I keep a ref to the 'new' stream. Conceptually I see this as a 'global' state
and  next is just

next :: a

but that's 30 years of imperative programming speaking and is, I think, wrong! 

Any help would be really appreciated.

Thanks 

Mike



More information about the Beginners mailing list