[Haskell-cafe] Lazy Evaluation in Monads

Antoine Latter aslatter at gmail.com
Wed Jun 1 01:13:14 CEST 2011


On Tue, May 31, 2011 at 6:10 PM, Antoine Latter <aslatter at gmail.com> wrote:
>
> You could use a different type:
>
>> type IOStream a = (a, IO (IOStream a))
>
>> unfold :: ([a] -> IO a) -> IO (IOStream a)
>> unfold f =
>>     let go prev = do
>>           next <- f prev
>>           return (next, go (next:prev))
>>     in do
>>       z <- f []
>>       go [z]
>
>> toList :: Int -> IOStream a -> IO [a]
>> toList 0 _ = return []
>> toList n (x,rest) = do
>>   xs <- toList (n-1) rest
>>   return (x:xs)
>

Let's pretend I did that right:

> toList :: Int -> IOStream a -> IO [a]
> toList 0 _ = return []
> toList 1 (x,_) = return [x]
> toList n (x,r) = do
>   rest <- r
>   xs <- toList (n-1) rest
>   return (x:xs)

Antoine



More information about the Haskell-Cafe mailing list