Computer Science Canada [Haskell-tut] Follow-up: Combining Functions |
Author: | wtd [ Fri Jan 07, 2005 8:18 pm ] | ||||||||||||||
Post subject: | [Haskell-tut] Follow-up: Combining Functions | ||||||||||||||
In my introduction to Haskell I createded a simple function greeting, which takes a name as a string and formulates a greeting.
And then, I created a greet function which takes a name and prints the greeting for it.
The latter function I rewrote as:
Now, the argument "name" should appear quite redundant in that last example. It is. What if, instead, we could simply combine the two functions, "greeting" and "putStrLn". Haskell provides an easy mechanism for doing so.
Now, we can make a similar observation about the greeting function.
This is really two functions, since operators are just functions. Given the order of evaluation, this could be rewritten:
Now, since we can partially apply functions - give them one argument, and get back a function which takes another argument and gives us the rest - we can rewrite this as:
Just as in the original, the function takes a string, appends "!" to it, then prepends "Hello, " to that. |