ich einen Datentyp haben, die Monoid Implementierung verwendet:Wie passt das Muster zusammen?
newtype First' a =
First' {getFirst' :: Optional a}
deriving (Eq, Show)
instance Arbitrary a => Arbitrary (First' a) where
arbitrary = do
x <- arbitrary
frequency [ (1, return (First'(Only x)))
, (1, return (First' Nada))]
instance Monoid (First' a) where
mempty = First' Nada
mappend (First' (Only x)) _ = First' (Only x)
mappend (First' Nada) (First' (Only x)) = First' (Only x)
mappend _ _ = First' Nada
Die letzte Implementierung von mappend
sagt, wenn es nicht Muster Spiel zwei vorherigen mappend
funktioniert, dann wäre der dritte genommen werden. Die letzte mappend
Funktion ist für mich nicht offensichtlich, dass ich First'
Datentyp übergeben muss. Es scheint mir, ich könnte jeden Datentyp wegen der Unterstreichung übergeben.
Wie funktioniert die Haskell wissen, wenn ich die Funktion als:
mappend (First' Nada) (First' Nada)
, dass das letzte Muster (mappend _ _)
abgestimmt ist?
Aus Neugier habe ich versucht:
*Main First Lib MonoidLaws> mappend 3 45
Aber Haskell Compiler beschwert.
<interactive>:14:1: error:
* Ambiguous type variable `a0' arising from a use of `print'
prevents the constraint `(Show a0)' from being solved.
Probable fix: use a type annotation to specify what `a0' should be.
These potential instances exist:
instance [safe] Show Args -- Defined in `Test.QuickCheck.Test'
instance [safe] Show Result -- Defined in `Test.QuickCheck.Test'
instance Show All -- Defined in `Data.Monoid'
...plus 35 others
...plus 22 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In a stmt of an interactive GHCi command: print it
Was war die Fehlermeldung? –
Diese Fehlermeldung ist ein Ablenkungsmanöver. – Zeta
'3' und' 45' werden in Ihrem 'mappend' überhaupt nicht gematcht, es wird zu einer' Num'. –