snippetMinor
Create `suffixes` Function on List
Viewed 0 times
listfunctioncreatesuffixes
Problem
I wrote the following
suffixes [1,2,3,4] == [[1,2,3,4], [2,3,4], [3,4], [4], []]
Please critique my implementation.
suffixes function. Given a list, it returns the list + all sub-lists.suffixes [1,2,3,4] == [[1,2,3,4], [2,3,4], [3,4], [4], []]
Please critique my implementation.
suffixes :: [a] -> [[a]]
suffixes [] = [] : []
suffixes xxs@(_:xs) = xxs : suffixes xsSolution
Looks good to me. The only thing I would change is
to
as it's a bit more readable.
When re-inventing functions, it can be instructive to look up their definition using Hoogle. Following the links, we find this definition in
suffixes [] = [] : []to
suffixes [] = [[]]as it's a bit more readable.
When re-inventing functions, it can be instructive to look up their definition using Hoogle. Following the links, we find this definition in
Data.List:tails :: [a] -> [[a]]
tails xs = xs : case xs of
[] -> []
_ : xs' -> tails xs'Code Snippets
suffixes [] = [] : []suffixes [] = [[]]tails :: [a] -> [[a]]
tails xs = xs : case xs of
[] -> []
_ : xs' -> tails xs'Context
StackExchange Code Review Q#66952, answer score: 6
Revisions (0)
No revisions yet.