I am reading the book 'Programming in Scala' (The red book).
In the chapter about Monoids, I understand what a Monoid homomorphism is, for example: The String Monoid M
with concatenation and length
function f
preserves the monoid structure, and hence are homomorphic.
M.op(f(x), f(y)) == M.op(f(x) + f(y))
// "Lorem".length + "ipsum".length == ("Lorem" + "ipsum").length
Quoting the book (From memory, so correct me if I am wrong:
When this happens in both directions, it is named Monoid isomorphisim, that means that for monoids
M, N
, and functionsf, g
,f andThen g
andg andThen f
are theidentity
function. For example theString
Monoid andList[Char]
Monoid with concatenation are isomorphic.
But I can't see an actual example for seeing this, I can only think of f
as the length
function, but what happens with g
?
Note: I have seen this question: What are isomorphism and homomorphisms.
To see the isomorphism between
String
andList[Char]
we havetoList: String -> List[Char]
andmkString: List[Char] -> String
.length
is a homomorphism from the String monoid to the monoid of natural numbers with addition.A couple of examples of endo-homomorphism of the String monoid are
toUpperCase
andtoLowerCase
.For lists, we have a lot of homomorphisms, many of which are just versions of
fold
.