Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
It's a function using its single argument to create a new function, only to apply it to that same argument. You could rewrite it using do-notation as such:
I guess the tricky part is realizing that
head
will get applied to the same value thatxs
represents, similar to how in theIO
monad, you always write to the same enivironment you read from, and thanks to monads, this all happens implicitly. I generally think of monads as computations that can be composed/sequenced within a specific environment that they can interface with, without explicit references, a kind of meta-argument given at the start of the computation, in this case just a simpleString
.As for the
(. tail) . (:) . toUpper
part, it might be helpful to think of function composition as any other binary operator. Composition is defined as:So once it has an argument, it'll first get applied to function right of
.
, and its result gets applied to the function left of it. For simplicity, let's definecons = (:)
and rewrite the partially applied(. tail)
to(\f -> f . tail)
, then we can work out the following:So we have a function that capitalizes a
Char
, and "conses" it to the tail of aString
, and because of(head >>=)
, theChar
will materialize from the sameString
tail
will get applied to, giving us an overly complicated yet succint way to capitalize the first letter of a word.I'm not sure why I actually wrote it like that though, if I were to redo it, I'd use:
Which does the exact same thing, but for different reasons and without monads.
What means $n?
can you explain the
(head >>= (. tail) . (:) . toUpper)
part?