Monday 6 June 2016

Haskell: Decompose a list into (head, tail)


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

*Main> :load listUtil.hs
[1 of 1] Compiling Main             ( listUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> decomposeList []
Nothing
*Main> 
*Main> decomposeList [1]
Just (1,[])
*Main> 
*Main> decomposeList [1, 2, 3, 4]
Just (1,[2,3,4])
*Main> 
*Main> decomposeList ""
Nothing
*Main> 
*Main> decomposeList "Hare Ram"
Just ('H',"are Ram")


Previous                                                 Next                                                 Home

No comments:

Post a Comment