Last active
August 29, 2015 14:14
-
-
Save ldub/8312572c8d0c5d42a504 to your computer and use it in GitHub Desktop.
A cool challenge in haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is an excersize in point-free Haskell. | |
To create a function that adds two numbers we could write: | |
λ> let f a b = a + b | |
Or alternatively | |
λ> let f = (+) | |
Now, how about a function that adds three numbers? | |
λ> let g = (+) . (+) --Doesn't work | |
This doesn't work because it doesn't match the type signature of the (.) operator | |
Let's create a new operator and call it 'dot' (operator taken from here: https://wiki.haskell.org/Pointfree the backstory behind how it got created is interesting) | |
λ> let dot = (.).(.) | |
λ> let g = (+) `dot` (+) | |
λ> g 1 2 3 | |
6 | |
Works as expected! | |
Now we can create a function that is point-free and adds any number of integers, simply by chaining the `dot` operator. | |
λ> let h = ((+) `dot` (+)) `dot` (+) | |
λ> h 1 2 3 4 | |
10 | |
The function h adds 4 integers. | |
Here's the challenge. Create a function called createAdderN that takes an argument, n, and returns a function that adds n integers. | |
(is this even possible? what would this function's type signature be? We could write createAdderN :: Int -> [Char] and pipe that function into System.Eval.Haskell but that isn't the goal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment