Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why foldl' giving an error while foldl working fine in the same definition

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Shouldn’t foldl’ be just a more efficient version of foldl?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading