purescript/purescript-lists

scanl does not terminate with infinite list

sloosch opened this issue · 0 comments

Hi,

the default scanl is implemented with the Traversable instance of the Lazy List and does not terminate when having an infinite list.

import Data.List.Lazy as List

-- Will not terminate even though "Lazy" may indicates the opposite
foo :: Maybe Int
foo = List.head $ scanl (+) 0 $ List.cycle $ List.fromFoldable [1, 2, 3]

Should the Lazy List have a specialized scanl ?
E.g.

scanl :: forall a b. (b -> a -> b) -> b -> List a -> List b
scanl f init list = List $ defer \_ -> case List.step list of
  List.Nil -> List.Nil
  List.Cons head tail -> 
    let val = f init head in 
    List.Cons val (scanl f val tail)