Skip to content

Commit da2a359

Browse files
authored
Update README.md
1 parent 172e82b commit da2a359

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
1-
# functional-programming-principles-in-scala-assignment1
1+
# functional-programming-principles-in-scala-assignment1
2+
3+
Mechanics
4+
5+
Download the recfun.zip handout archive file and extract it somewhere on your machine.
6+
7+
This assignment counts towards your final grade. Please refer to the Grading Policy for more details.
8+
9+
Do not forget to submit your work using the submit task from SBT. Please refer to the example assignment for instructions.
10+
11+
Exercise 1: Pascal’s Triangle
12+
13+
The following pattern of numbers is called Pascal’s triangle.
14+
15+
16+

17+
1
18+
2
19+
3
20+
4
21+
5
22+
6
23+
1
24+
1 1
25+
1 2 1
26+
1 3 3 1
27+
1 4 6 4 1
28+
...
29+
The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. Write a function that computes the elements of Pascal’s triangle by means of a recursive process.
30+
31+
Do this exercise by implementing the pascal function in Main.scala, which takes a column c and a row r, counting from 0 and returns the number at that spot in the triangle. For example, pascal(0,2)=1,pascal(1,2)=2 and pascal(1,3)=3.
32+
33+
34+

35+
1
36+
def pascal(c: Int, r: Int): Int
37+
Exercise 2: Parentheses Balancing
38+
39+
Write a recursive function which verifies the balancing of parentheses in a string, which we represent as a List[Char] not a String. For example, the function should return true for the following strings:
40+
41+
(if (zero? x) max (/ 1 x))
42+
I told him (that it’s not (yet) done). (But he wasn’t listening)
43+
The function should return false for the following strings:
44+
45+
:-)
46+
())(
47+
The last example shows that it’s not enough to verify that a string contains the same number of opening and closing parentheses.
48+
49+
Do this exercise by implementing the balance function in Main.scala. Its signature is as follows:
50+
51+
52+

53+
1
54+
def balance(chars: List[Char]): Boolean
55+
There are three methods on List[Char] that are useful for this exercise:
56+
57+
chars.isEmpty: Boolean returns whether a list is empty
58+
chars.head: Char returns the first element of the list
59+
chars.tail: List[Char] returns the list without the first element
60+
Hint: you can define an inner function if you need to pass extra parameters to your function.
61+
62+
Testing: You can use the toList method to convert from a String to aList[Char]: e.g. "(just an) example".toList.
63+
64+
Exercise 3: Counting Change
65+
66+
Write a recursive function that counts how many different ways you can make change for an amount, given a list of coin denominations. For example, there are 3 ways to give change for 4 if you have coins with denomination 1 and 2: 1+1+1+1, 1+1+2, 2+2.
67+
68+
Do this exercise by implementing the countChange function inMain.scala. This function takes an amount to change, and a list of unique denominations for the coins. Its signature is as follows:
69+
70+
71+

72+
1
73+
def countChange(money: Int, coins: List[Int]): Int
74+
Once again, you can make use of functions isEmpty, head and tail on the list of integers coins.
75+
76+
Hint: Think of the degenerate cases. How many ways can you give change for 0 CHF(swiss money)? How many ways can you give change for >0 CHF, if you have no coins?

0 commit comments

Comments
 (0)