There are basically two ways to do this. The first approach slightly changes how the function works: in that case the base-case returns an empty list, and the recursive case yields t
instead of f
:
suffixes :: [a] -> [[a]]
suffixes [] = []
suffixes (_:t) = t : suffixes t
This works since t
is the tail of the list, so it is the list, but without the first element. We thus yield this element, and perform recursion on the tail t
as well, until we reach the empty list. Since this empty list was already "yielded" by the recursive case, we thus do not yield it in the basecase.
Or we can just keep the old suffixes
function (but rename it to for example go
), and let the outer function "pre-processes" the list by "popping" an element:
suffixes :: [a] -> [[a]]
suffixes [] = []
suffixes (_:xs) = go xs
where go [] = [] : []
go f@(_:t) = f: go t
Note that for an empty string, there are no proper suffixes, hence we return an empty list.
This then yield the expected results:
Prelude> suffixes ""
[]
Prelude> suffixes "a"
[""]
Prelude> suffixes "ab"
["b",""]
Prelude> suffixes "abc"
["bc","c",""]
Prelude> suffixes "123abc"
["23abc","3abc","abc","bc","c",""]
suffixes = tail . tails
usingtails
fromData.List
. – K. A. Buhr