Monday, 6 June 2016

Haskell : Find minimum in list of numbers


ListUtil.hs
module ListUtil(getMinimum) where
    getMinimum :: [Integer] -> (Maybe Integer)
    getMinimum [] = Nothing
    getMinimum (x:xs) = Just (getMin x xs)

    getMin :: Integer -> [Integer] -> Integer
    getMin currentMax [] = currentMax
    getMin currentMax (x:xs)
      | (currentMax > x) = getMin x xs
      | otherwise = getMin currentMax xs

Prelude> :load ListUtil.hs
[1 of 1] Compiling ListUtil         ( ListUtil.hs, interpreted )
Ok, modules loaded: ListUtil.
*ListUtil> 
*ListUtil> getMinimum []
Nothing
*ListUtil> 
*ListUtil> getMinimum [43, 2, 3, 1, 0, 98, -9, 87]
Just (-9)
*ListUtil> 


Previous                                                 Next                                                 Home

No comments:

Post a Comment