Answers for "max i haskell"

1

haskell max function

-- fold the list:

maximum' :: Ord a => [a] -> a
maximum' = foldr1 (x y ->if x >= y then x else y)

--For the recursive version (no double checking):

maximum'' :: Ord a => [a] -> a
maximum'' [x]       = x
maximum'' (x:x':xs) = maximum' ((if x >= x' then x else x'):xs)

-- If you want wards:

maximum'' :: Ord a => [a] -> a
maximum'' [x]       = x
maximum'' (x:x':xs) | x >= x'   = maximum' (x:xs)
maximum'' (x:x':xs) | otherwise = maximum' (x':xs)
Posted by: Guest on June-12-2021

Browse Popular Code Answers by Language