Sunday 5 June 2016

Haskell: Get all the tails of a list


ListUtil.hs
getTails :: [a] -> [[a]]
getTails [] = []
getTails list@(x:xs) = (list) : getTails (xs)

*Main> :load listUtil.hs 
[1 of 1] Compiling Main             ( listUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> getTails "Hello"
["Hello","ello","llo","lo","o"]
*Main> 
*Main> getTails [2, 3, 5, 7]
[[2,3,5,7],[3,5,7],[5,7],[7]]
*Main> 
*Main> getTails [True, False, True]
[[True,False,True],[False,True],[True]]
*Main> 


Previous                                                 Next                                                 Home

No comments:

Post a Comment