Here I am trying to reimplement a safe maximum using folds
import Data.ByteString (foldl')
maximum' :: Ord a => [a] -> Maybe a
maximum' = foldl (\ acc x -> max acc (Just x)) Nothing
maximum'' :: Ord a => [a] -> Maybe a
maximum'' = foldl' (\ acc x -> max acc (Just x)) Nothing
First function that uses foldl working correctly but second one gives this error:
• Couldn't match expected type ‘a’
with actual type ‘GHC.Word.Word8’
‘a’ is a rigid type variable bound by
the type signature for:
maximum'' :: forall a. Ord a => [a] -> Maybe a
at /home/mali/Projects/Deneme/HigherOrder.hs:60:1-36
• In the first argument of ‘Just’, namely ‘x’
In the second argument of ‘max’, namely ‘(Just x)’
In the expression: max acc (Just x)
Shouldn't foldl'
be just a more efficient version of foldl
?
You want the foldl'
from Prelude
(which works on lists), not Data.ByteString
(which works on bytestrings). You don't have to import anything since Prelude
is imported automatically.