-
-
Save L7R7/dbba84814fdc29e5e2d46196df8289c7 to your computer and use it in GitHub Desktop.
| instance Append (List a) where | |
| append Empty as = as | |
| append as Empty = as | |
| append (Cons a as) bs = Cons a (append as bs) | |
| instance (Append a) => Append (Maybe a) where | |
| append (Just a1) (Just a2) = Just (append a1 a2) | |
| append ma Nothing = ma | |
| append Nothing ma = ma |
yes π It took me a while to really understand this, as it is quite abstract. It's even more mind blowing for tuples: We can write an instance for Append (or Semigroup, which is actually the same thing) for tuples if the types in the tuple have an Append instance as well.
I think the canonical example is the "word count" example based on monoids which is described in a paper of which I forgot the name π€ There they are using tuples to count words, lines, sentences etc. in one go by composing monoids together
Ah that's cool π I wish I could find papers readable, have always struggled with them!! π
Oh, for me it's the same! most of them are still too advanced for me, plus the sometimes more complex use of the english language makes it even harder for meπ but I keep going, slowly but surely!
Ended up asking and was told that the constraint
(Append a)is needed becauseappend a1 a2uses it.So basically in this scenario, we're taking the values out of the
Justand just passing them on toappend. So at from there we know thata1+a2are of the type classAppend. π