Monday 6 June 2016

Haskell: Get tail of the list (or) string

Return the elements from list except first element.


listUtil.hs
getTail :: [a] -> Maybe [a]
getTail [] = Nothing
getTail (x:xs) = Just xs

Prelude> :load listutil.hs
[1 of 1] Compiling Main             ( listutil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> getTail []
Nothing
*Main> getTail [1, 2, 3]
Just [2,3]
*Main> getTail [[1, 2, 3], [2, 3], [4, 5], []]
Just [[2,3],[4,5],[]]
*Main> getTail "Hare Ram"
Just "are Ram"
*Main> 

Previous                                                 Next                                                 Home

No comments:

Post a Comment