Why this implementation of maximum doesn't work in haskell: maximum' = foldr1 max

101 views Asked by At

This implementation works fine:

 λ > let maximum' xs = foldl1 max xs
 λ > maximum' [1..5]
 5

Because of currying we can rewrite function like foo a = bar b a as foo = bar b

Result of foldl1 max have expected type

 λ > :t foldl1 max
 foldl1 max :: Ord a => [a] -> a

But this doesn't work

 λ > let maximum' = foldl1 max
 λ > maximum' [1..5]

<interactive>:71:11:
    No instance for (Num ()) arising from the literal `1'
    Possible fix: add an instance declaration for (Num ())
    In the expression: 1
    In the first argument of maximum', namely `[1 .. 5]'
    In the expression: maximum' [1 .. 5]

This error isn't clear for me in this case. So, why this error occur? Is there a way to fix it and define functions like this without explicitly specifying last argument?

0

There are 0 answers