Saturday 4 June 2016

Haskell: Return True if the list has only one element, else False.


CheckList.hs
isListHasOneEle :: [a] -> Bool
isListHasOneEle [] = False
isListHasOneEle (x:[]) = True
isListHasOneEle (x:xs) = False

Prelude> :load CheckList.hs 
[1 of 1] Compiling Main             ( CheckList.hs, interpreted )
Ok, modules loaded: Main.
*Main> 
*Main> isListHasOneEle []
False
*Main> isListHasOneEle [1]
True
*Main> isListHasOneEle [1, 2, 3]
False
*Main> isListHasOneEle [1..]
False
*Main> isListHasOneEle [[1, 2, 3]]
True
*Main> 


Previous                                                 Next                                                 Home

No comments:

Post a Comment