Skip to content

scanl does not terminate with infinite list #154

@sloosch

Description

@sloosch

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions