Skip to content

Instantly share code, notes, and snippets.

@L7R7
Last active November 13, 2020 11:18
Show Gist options
  • Save L7R7/dbba84814fdc29e5e2d46196df8289c7 to your computer and use it in GitHub Desktop.
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
@Cmdv
Copy link

Cmdv commented Nov 12, 2020

Ended up asking and was told that the constraint (Append a) is needed because append a1 a2 uses it.

So basically in this scenario, we're taking the values out of the Just and just passing them on to append. So at from there we know that a1 + a2 are of the type class Append. πŸ˜…

@L7R7
Copy link
Author

L7R7 commented Nov 13, 2020

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

@Cmdv
Copy link

Cmdv commented Nov 13, 2020

Ah that's cool πŸ‘Œ I wish I could find papers readable, have always struggled with them!! πŸ˜‚

@L7R7
Copy link
Author

L7R7 commented Nov 13, 2020

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment