Functional Programming with Haskell.
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org/ | sh
sudo apt-get install haskell-platform is outdated
I have created a github with all the examinations that were uploaded and have done my best to create solutions for them https://github.com/ludde127/EDAN40-LTH.
First 10 lectures are common and one last lecture is only for the bigger course. In the advanced course we don't get any labs and have the two courses have different exams (of course).
We get two/three not too tough programming assignments (15, 10, 6 hrs) and you get tests to use.
Haskell is a niche functional programming language. Haskell as its name suggests consists of functions.s.
Functions
Let A and B be arbitrary set, any subset of A * B will be called a relation from A to B. A relation R subset of A x B is a function if and only if x is in A for each y1 & y2 in B and each (x, y1) is in R and (x, y2) is in R => y1 = y2.
That's to say that a function may only have one output for each x.
f 0 = 1
f n = n * f (n-1)
We avoid assignments and want no side effects. We avoid side effects as we don't always know what will happen and functions with side effects are thus not real function.
Functional programming = ordinary programming - assignments / side effects
It provides good support for:
sum1 [] = 0
sum1 (x:xs) = x + (sum1 xs)
Note 1: recursion is intimately connected to computability
Note 2: (x:xs) - a very important idiom in FP/Haskell.
ackumulate f i [] = i
ackumulate f i (x:xs) = f x (ackumulate f i xs)
sum2 = ackumulate (+) 0
product2 = ackumulate (*) 1
anyTrue2 = ackumulate (||) True
allTrue2 = ackumulate (&&) True
Compute primes with Eratosthenes sieve:
sieve (n:ns) = n : sieve [x | x <- ns, x 'mod' n > 0]
take 10 xs takes the first 10 items.
primes = sieve [2..]
where
sieve (n:ns) =
n: sieve [x|x<-ns, x 'mod' n > 0]
What does g x = map ($x) do?
The code snippet g x = map ($x) defines a function named g that takes an argument x.
The map function in Haskell applies a given function to every element of a list and returns a new list with the results. In this case, the function being mapped is ($x). The ($) operator is known as "function application" and is used to apply a function to an argument. In this context, ($x) represents a partially applied function where $ is the function and x is the argument.
So, the function g takes a list as input and applies the partially applied function ($x) to each element of the list using map. As a result, it returns a new list where each element of the original list is replaced by the result of applying x to that element.
Here's an example to illustrate how g works:
g 3 [(*2), (+1), (^2)]
[6, 4, 9]
In this example, we call g with the argument 3 and a list of functions [(*2), (+1), (^2)]. The function g applies 3 to each function in the list using ($x), resulting in a new list [6, 4, 9]. The original list [(*2), (+1), (^2)] contains three functions, and each function is applied to 3 to produce the corresponding element in the resulting list.