Saturday, 4 June 2016

Haskell: Write a function that computes number of elements in list


listUtil.hs
lengthOfList :: [a] -> Integer
lengthOfList [] = 0
lengthOfList (x:xs) = 1 + lengthOfList xs

*Main> :load listUtil.hs
[1 of 1] Compiling Main             ( listUtil.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> lengthOfList []
0
*Main> 
*Main> lengthOfList [2, 3, 5, 7]
4
*Main> 
*Main> lengthOfList ["Hello", "How", "are", "you"]
4
*Main> 


Previous                                                 Next                                                 Home

No comments:

Post a Comment