Erlang Exercises
Simple Stuff
- Define a function that computes the fibonacci function. Make different implementations; a trivially recursive version, a tail recursive version and one that has better than linear complexity. What happens if you call the function with a negative number? Make sure nothing too bad happens if you do this.
- Define a function that returns the last three elements of a list.
- Define a function lastn(N, L) that returns the last N elements of a list.
- Define a function that given a non empty list of numbers returns a tuple {Min, Max} for the smallest and largest number in the list. Do at least two versions; one using a direct definition and one using a higher order function on lists.
Fruit Store
You are given the assignment to write software for a web shop for selling fruit. Fruit is sold either by weight (having a price/kg) or by unit (having a per unit price). Construct a record (or an abstract data type) for storing this information. A list of those records is then your pricing information. A shopping cart will list the amount of fruit a customer wants to buy. Again, construct a suitable representation for a shopping cart. It should allow several instances of the same fruit. Write a function that given a shopping cart and pricing information return the amount the customer should pay.
What limitations do you have on the different data types?
List Comprehension
- Write a list comprehension that given a shopping cart extracts all bananas weighing more than 0.4kg.
- Use list comprehension to write a function that removes all occurrences of an element from a list.
- Use the previous function to write a function that removes duplicates in a list, i.e., uniq([1,2,1,a,b,a]) = [1,2,a,b]. Order does not matter.
- You can represent sets by lists. Write set operations using list comprehension. Can you implement all operations?
Higher order functions
- Use functions to represent sets, i.e., when you create an empty set or do set operations, a function is returned. Can you implement all operations? (This is very much about thinking about a suitable function for representing a set; when that is down, the rest is rather easy)
Processes
- Implement a calculator as Erlang process. Message are simple arithmetic expressions which are evaluated and the result sent back to the process sending the message. Devise your own protocol for this. Every expression that has been evaluated should be kept and in response to a show message a list of expressions and the result should be returned.
- Can you make the process robust against errors, i.e., can it survive even if you feed it bad input? The state (previous expressions) should not be lost if it is fed with bad input. There are several ways to do this, e.g., by linking processes or catching any errors and acting upon them. Explore different solutions!
- Take some problem that can be parallelised and distribute the computation between several processes. Again, how can you react if any of the processes doing a part of the work encounters an error? Can you recover in any way? (Yes, this is a rather general question, so any answer is dependent on the problem you choose)
Macros
- Define a macro LET(Var, Expr, Body) to locally bind Var to the value of Expr inside Body. This was more or less given during the lecture.
- Common Lisp has a general LET ((name_1 expr_1) (name_2 expr_2) ..) body) construction allowing you to bind several names locally in parallel. Is it possible to define something similar in Erlang?