---
tags: cs1101s
---
# CS1101S Studio S4 (T05J)
## Tree Recursion
### Question 1
Write a function `pascal` that computes elements of Pascal’s triangle by means of a recursive process, i.e. the function should take two arguments, row (counted from the top, starting with 0), and position (counted from the left, starting with 0), and return the element for the specified row and position. Note that only one element must be returned and NOT the
entire row. E.g. calling the method with row = 2 and position = 1 should return 2.
Likewise calling the method with row = 4 and position = 2 should return 6.
```javascript!
//Write your answer here
1) function pascal(row, index){
return index > row
? false
: index === 1 || index === row
? 1
: pascal(row - 1, index - 1)
+ pascal(row - 1, index);
}
James
function pascal_triangle(row, position) {
return position > row
? false
: position === row || position === 0
? 1
: pascal_triangle(row-1, position-1)+pascal_triangle(row-1, position);
}
pascal_triangle(4,3);
function pascal(r,p) {
return p === 0 || p === p
? 1
: pascal(r-1,p-1) + pascal(r-1,p);
}
```
Similar to the tree for fib(5) in section 1.2.2 and the tree for cc(11, 5) in the solution for
exercise 1.14, draw the tree illustrating the process generated by pascal(4, 3) to compute
the value in Pascal’s triangle in row 4 and position 3. Does your function pascal give rise
to a recursive or an iterative process?
Recursive process
//Tree to be drawn here
## In Class Stuff
1. Consider compose(math_sqrt, math_log), which is a function of type Number-Transformation.
What is the result of compose(math_sqrt, math_log)(math_E)?
```javascript!
//Write your answer here
Number -> Number
math_sqrt(math_log(math_E))
math_sqrt(1)
The result is 1
```
What is the result of compose(math_log, math_sqrt)(math_E * math_E)?
```javascript!
//Write your answer here
Number -> Number
math_log(math_sqrt(math_E * math_E))
math_log(math_E)
The result is 1
```
2. What is the result of evaluating thrice(math_sqrt)(256);?
```javascript=
sqrt(sqrt(sqrt(256)))
sqrt(sqrt(16))
sqrt(4)
The result is 2
Number -> Number
```
3. For what value of n will thrice(thrice)(f)(0) return the same value as repeated(f,
n)(x)? In other words: What is the result of the following program?
`thrice(thrice)(x => x + 1)(0);`
```javascript!
//Write your answer here
thrice(thrice)(x => x + 1)(0)
(f(f(f(x))))(f(f(f(x))))(x => x + 1)(0)
```
4. See if you can now predict what will happen when the following statements are evaluated.
Briefly explain what goes on in each case.
Note: Function square and add1 are defined as follows:
const square = x => x * x;
const add1 = x => x + 1;
`(a) ((thrice(thrice))(add1))(6);`
```javascript!
27 + 6
33
```
`(b) ((thrice(thrice))(x => x))(compose);`
```javascript!
//Write your answer here
thrice(thrice) = thrice(thrice(thrice(f)))
f(f(f(f(f(f(f(f(f(f(f...(f(compose))))))))))
compose;
```
`(c) ((thrice(thrice))(square))(1);`
```javascript!
//Write your answer here
1
```
`(d) ((thrice(thrice))(square))(2);`
```javascript!
//Write your answer here
(2^2)^2.... for 27 times
number outside range(too huge)
```