Composing functions by concatenation
A lot of tasks are not really worthy of being implemented in completely custom code. Let's, for example, have a look on how a programmer might solve the task of finding out how many unique words a text contains with the programming language Haskell. The first line defines a function unique_words
and the second one demonstrates its use with an example string:
Wow, that is short! Without explaining Haskell syntax too much, let's see what the code does. It defines the function called unique_words
, which applies a series of functions to its input. It first maps all the characters from the input to lowercase with map toLower
. This way, words like FOO
and foo
can be regarded as the same word. Then, the words
function splits a sentence into individual words, as from "foo bar baz"
to ["foo", "bar", "baz"]
. Next step is sorting the new list of words. This way, a word sequence such as ["a", "b", "a"]
becomes ["a", "a", "b"]
. Now, the group
function takes over. It...