Now that you’ve learned a bit about the basics of Haskell’s syntax and how to write some applications, try working through these exercises. You should be able to complete all of the following exercises using the material from this chapter, but if you find yourself stuck on an exercise, try reading ahead to the next chapter and coming back to the exercise after you’ve gotten a bit more practice thinking about how to implement programs in Haskell.
Factorials
The factorial function is a simple function that you can define recursively. You can compute the factorial of a number, n , by multiplying all of the numbers up to n :
| | factorial 5 = 5 * 4 * 3 * 2 * 1 = 120 |
Try implementing your own factorial function. You can test your implementation in ghci and compare its output to the example:
| | λ factorial 1 |
| | 1 |
| | λ factorial 3 |
| | 6 |
| | λ factorial 5 |
| | 120 |
| | λ factorial 10 |
| | 3628800 |
| | λ factorial 25 |
| | 15511210043330985984000000 |
The Fibonacci Sequence
The Fibonacci sequence is a sequence of numbers that can be defined recursively. The first 10 numbers of the Fibonacci sequences are: 0,1,1,2,3,5,8,13,21,34. You can calculate any given Fibonacci number, n , by adding up the two previous Fibonacci numbers.
Write a function that will compute the nth Fibonacci number for any given number, n. You can test your implementation in ghci and compare it to the example:
| | λ fibonacci 0 |
| | 0 |
| | λ fibonacci 1 |
| | 1 |
| | λ fibonacci 5 |
| | 5 |
| | λ fibonacci 10 |
| | 55 |
| | λ fibonacci 25 |
| | 75025 |
Manual Currying
You’ve learned how Haskell functions work by taking a single argument. One way to write a function that takes multiple arguments is to pass in a tuple of arguments. For example, consider this addition function:
| | uncurriedAddition nums = |
| | let |
| | a = fst nums |
| | b = snd nums |
| | in a + b |
Haskell’s standard library includes two functions, curry and uncurry , that make it easy for you to convert between functions that take two arguments and functions that take a tuple. The curry function transforms a function like our uncurriedAddition function and turns it into one that takes two separate arguments:
| | λ addition = curry uncurriedAddition |
| | λ addOne = addition 1 |
| | λ addTwo = addition 2 |
| | λ addOne 1 |
| | 2 |
| | λ addOne 2 |
| | 3 |
| | λ addOne 3 |
| | 4 |
| | λ addTwo 1 |
| | 3 |
| | λ addTwo 2 |
| | 4 |
| | λ addTwo 3 |
| | 5 |
Similarly, the uncurry function takes a regular function with two arguments and converts it into a function that accepts a tuple. For example, using uncurry we could have rewritten uncurredAddition like this:
| | uncurriedAddition = uncurry (+) |
Using what you’ve learned in this chapter, try implementing your own version of curry and uncurry .
Since the standard library already has functions named curry and uncurry , you should select different names for your implementations. After you’ve written your versions, compare the behavior to the standard library implementations to ensure that your versions behave the same way.
Footnotes
[1]
Copyright © 2023, The Pragmatic Bookshelf.