Use lisp Part 3 Defining Functions Next try defining a few s
Use lisp Part 3: Defining Functions Next, try defining a few sample functions. This is Scheme code: (define (square x) (* x x)) (define (cube x) (* x x x)) (define (fib x) (cond ((or (= 0 x)(= 1 x)) 1) (#T (+ (fib (- x 1)) (fib (- x 2)))))) Or some Lisp equivalents: (defun square (x) (* x x)) (defun cube (x) (* x x x)) (defun fib (n) (cond ((= n 0) 0) ((= n 1) 1) (T (+ (fib (- n 1)) (fib (- n 2)))) ) ) Show the results of defining these functions, and the results of executing these functions with some sample input data. Question: What does each function do? Part 4: Combining Functions In these languages, you can use a function within a function. Show an expression and output result for the square of the cube of 2. How about the fib of the square of 3? Or the square of 4 divided by 5? Part 5: List functions CAR, CDR, and CONS are keywords for manipulating lists of data. Explain the output of: (car \'(1 2 3 4)) What about this: (cdr \'(1 2 3 4)) CAR and CDR take lists apart. CONS builds them. What is this doing? Show it!: (cons 1 (cons 2 (cons 3 \'(4)))) MAP (Scheme) or MAPCAR (Lisp) applies a function to a list of elements and returns another list with the result. Show the results of using map/mapcar, with the arguments being: one of the functions you defined above, and the literal list \'(0 1 2 3 4 5 6 7 8) (note: the Scheme syntax is clearer on this one. In Lisp, you need (mapcar #\'<function> <list>) in scheme it\'s just (map <function> <list>)
Solution
The program prints the nth number of Fibonacci series.
Example for n = 1, result is 0; for n = 5 result is 3
4.
5.
