Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I wrote the following 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 xs
share|improve this question

1 Answer 1

up vote 4 down vote accepted

Looks good to me. The only thing I would change is

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'
share|improve this answer
    
Thank you for your continued help on most/all of my recent questions. –  Kevin Meredith 15 hours ago
    
@KevinMeredith my pleasure. I thoroughly enjoyed the previous one (Streams) in particular :) –  mjolka 15 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.