Se scrivo:foldr vs utilizzo foldr1 in Haskell
> let xs = [1,5,19,2,-3,5]
> foldr max 0 xs
19
> foldr1 max xs
19
E se scrivo (lo so, il valore iniziale è errato qui per una funzione di massima generica ...):
> let maximum' = foldr max 0
> maximum' xs
19
Ma se scrivo:
> let maximum2' = foldr1 max
> maximum2' xs
la risposta è:
<interactive>:61:11:
Couldn't match expected type `()' with actual type `Integer'
Expected type: [()]
Actual type: [Integer]
In the first argument of maximum2', namely `xs'
In the expression: maximum2' xs
Sono nuovo di Haskell. Che cosa sto facendo di sbagliato? (Impossibile decifrare il messaggio di errore ...) Come usare foldr1
con max
? Grazie.
EDIT (dopo aver accettato RISPOSTA):
solo per mostrare alcuni altri esempi di effetto delle regole inadempienti (la risposta spiega questi, anche):
Esempio 1:
> let max' = max
> :t max
max :: Ord a => a -> a -> a
> :t max'
max' ::() ->() ->()
Esempio 2:
> let plus = (+)
> :t (+)
(+) :: Num a => a -> a -> a
> :t plus
plus :: Integer -> Integer -> Integer
La temuta restrizione monomorfismo. – augustss
abbastanza divertente ': t foldr1 max' dà' Ord a => [a] -> a' e 'let maximum 'l = foldr1 max l' funziona come previsto – soulcheck
@augusts manca il collegamento a [restrizione monomorfismo] (http://www.haskell.org/haskellwiki/Monomorphism_restriction) – soulcheck