Some examples of functional programming and Scheme programming language in specific.
A binary tree in Scheme language can be defined as below (Note that no node can have only one child):
- (leaf integer-value)
- (node left-subtree right-subtree)
Write the function, sum, in a way that returns the sum of all leaf values given a binary tree as input.
Example test case:
Input:
(display(sum '(node (leaf 1) (node (leaf 2) (leaf 3)))))
Output:
6
In Scheme language, the 2D array, Am*n, is illustrated as below:
- ((a11 a12 ... a1n) (a21 a22 ... a2n) (am1 am2 ... amn))
Write a function to get two arrays as input and return their multiplication.
Example test case:
Input:
(display (multiply '(((1 2) (3 4)) ((5 6) (7 8)))))
Output:
((19 22) (43 50))
Write a function as depicted below to return the sum of rows and columns of a given array.
- (rowSum '((a11 a12 ... a1n) (a21 a22 ... a2n) (am1 am2 ... amn)) )
- (colSum '((a11 a12 ... a1n) (a21 a22 ... a2n) (am1 am2 ... amn)) )
Example test case:
Input:
(display (rowSum '((0 1 2 3 4)(5 6 7 8 9)(0 1 2 3 4)(5 6 7 8 9))))
Output:
(10 35 10 35)