functional-programming

Enter the vast world of programming languages by following the tutorials provided here to learn about functional programming in Haskell.


Project maintained by jd-anabi Hosted on GitHub Pages — Theme by mattgraham

Back to main

If you have installed Haskell, we are ready to begin learning the basics. If not, please go here to install it.

Functions

One of the most fundamental aspects of Haskell is functions. Learning how to create, compose, and apply functions is essential to working with Haskell. Whenever you are write a function, it will usually follow the basic format (the … indicate that this code wouldn’t actually run; it is merely for demonstration purposes):

f :: at1 -> at2 -> ... -> atn -> rt  
f O1 O2 ... On = br  
f a1 a2 ... an = r 

where

Examples

Let’s try to write a Haskell function that computes the nth fibonacci number.
Firstly, let’s write the recurrence relation of the fibonacci sequence:
fib(0) = 0
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2)
This can be easily converted into Haskell like:

fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = (fib n-1) + (fib n-2)

As we can see, this is a nice connection between the mathematical reccurence relation and the recursive Haskell function.
If you are still not comfortable with this, see the recurence relations section for more help. If you are comfortable with these recurrence relations, we are ready to work with lists in Haskell.