Programming languages: Practical 2 – Erlang
These problems are to assess your understanding of the use of the Erlang programming language.
Getting started
• Read and sign the honor code section of the code submission sheet.
• Use the command below to download a starting set of code. It contains implementations of map, reduce, filter,
and listlength.
wget http://www.cs.wfu.edu/~turketwh/231/pra2/pra2.erl
Solution constraints
• Unless otherwise specified, as much as possible make use of patterns and higher-order functions where appropriate. Part of your grade will depend on your ability to determine when/how the use of patterns and higher-order functions is appropriate.
• Do not use any additional Erlang functions other than the ones provided to you in the file you download in the Getting started section above.
Problem Set
1. Write a function isSorted that takes as a parameter a list. The function should return true if the items in the list are already sorted in descending order and should return false otherwise. Note, you are not asked to sort the list, just check that it is sorted. You can assume that an empty list is sorted.
2. Write a function named listDifferences that takes two lists as parameters. Compute the number of locations in the lists where the values differ. You can assume that the shorter list, of length M, overlays the first M values of the longer list, when doing the analysis. As an example listDifferences([1,2,4][1,3,4,8,10]) should return the value 3, based off the following:
List1: [1,2,4]
List2: [1,3,4,8,10]
Diffs: [0,1,0,1,1] –> 3
3. Write a function increasePrices that takes a list of 2-item lists, where each 2-item lists consists of an id number and a price. Your function should increase the price of each item in the list by 5%. As an example of what the input lists should look like, one example input is [[2,100],[5,120]]. This represents a list with information on two products – product 2 with price of 100 and product 5 with price of 120. After the function completes, the list should look like [[2,105],[5,126]]. The function should work on a list representing any number of products
(with any number of 2-item lists inside of it).
4. Consider the sequence of values defined by the equation an = 3an−1 − an−2. Let 1 and 3 be the 0th and 1st values in the sequence, corresponding to a0 and a1 respectively. Write a function named recseq which takes one input N, which is an integer value >= 0, and which returns the Nth value in the sequence.
1
5. Write a function checkSalePrices that takes a list of 2-item lists, where each 2-item lists consists of an id number and a price. Your function should return the list of 2-item lists where the price minus 12% of the price would be less than 110. As an example of what the input lists should look like, one example input is [[1,120],[6,150]]. This represents a list with information on two products – product 1 with price of 120 and product 6 with price of 150. After the function completes, the list should look like [[1,120]]. The function should work on a list representing any number of products (with any number of 2-item lists inside of it).
6. Consider the two Erlang functions written below. Rewrite the functions, making use of local variables (and NOT using patterns) to minimize the number of times the hd and tl functions are called in each function.
funcB(X,L) -> if
L == [] -> [X];
X < hd(L) -> [X|L];
true -> [hd(L)|funcB(X,tl(L))]
end.
funcA(L) ->
if L == [] -> [];
tl(L) == [] -> [hd(L)];
true -> funcB(hd(L),funcA(tl(L))) end.
2