Skip to content

A 'better' (less strict) Data.List.Base.tails? #2267

Closed
@jamesmckinna

Description

@jamesmckinna

Working on #2258 suggests the following:

tails : List A  List (List A)
{- compared to the original
tails []       = [] ∷ []
tails (x ∷ xs) = (x ∷ xs) ∷ tails xs
-}
tails xs = xs ∷ go xs
  where
  go : List A  List (List A)
  go [] = []
  go (_ ∷ xs) = xs ∷ go xs

which has the advantage that tails xs observably produces a non-empty list (moreover, one whose head is the argument xs), without having to do a subsidiary pattern-match on xs.

Similar remarks apply to eg inits and scanl, and as with moving scanr to Data.List.NonEmpty, perhaps these functions should, too.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions