In this assignment you cannot use any imperative constructs of scheme, such as set! or the iterative loop constructs. You also are not allowed to use higher level functions, such as length to get the length of the list. Instead you must use the basic built-in functions for manipulating a list--car, cdr, cons, append--and the built-in arithmetic operators to create your functions.
(swap 9 6)should return the cons pair (6 . 9).
(avg '(3 6 17 12 15))should return 10.6 (and not 53/5 which is what you'll get if you do not ensure that you have a floating point operand in one of the dividend or divisor). For efficiency reasons, please do not write two functions that separately compute the sum of the list and the length of the list (i.e., do not traverse the list twice). It is possible to write a solution to this question that traverses the list only once and simultaneously computes the sum and length of the list and then returns their quotient.
;; find minimum element in a list (define min (lambda (l) (cond ((null? l) '()) ((null? (cdr l)) (car l)) (#t (let ((a (car l)) (b (min (cdr l)))) (if (< b a) b a)))))))
(mergesort '(1 3 2 4 8 1 9 6 10)) ==> '(1 1 2 3 4 6 8 9 10)As long as you use the purely functional features of scheme (i.e., no imperative constructs), you may design your merge sort function any way you see fit. However, it helped me to define the following two helper functions, which I created and tested first, and then used them to build mergesort:
(merge '(2 4 6) '(1 3 5 9 10)) ==> '(1 2 3 4 5 6 9 10)
(filter (lambda(x) (< x 5)) '(3 9 5 8 2 4 7))should return (3 2 4). Your function should be named filter.
6 | ----------- | | | 3 5 8 / \ | 11 9 ---------- | | | | 1 20 4 17 / \ 7 10would be represented as the list:
'(6 (3 (11) (9)) (5) (8 (1) (20 (7) (10)) (4) (17)))Write a function named fringe that takes a list which represents a tree and returns a list of the leaves of the tree, in left-to-right order. For example,
(fringe '(6 (3 (11) (9)) (5) (8 (1) (20 (7) (10)) (4) (17))))should return
'(11 9 5 1 7 10 4 17)You must write a recursive definition of fringe that use the map and fold functions. Look at the Functional-Languges-Higher-Order-Functions lecture and the slides from that lecture if you're not sure what map and fold are. map is a Scheme built-in function and the Scheme implementation for fold can be found on the slides. I would use the map function to apply fringe to the list of sub-trees for a tree and then use fold to append the lists returned by each invocation of fringe into a single list.
(define fib (lambda (n) (letrec ((fib-helper (lambda (f1 f2 i) (if (= i n) f2 (fib-helper f2 (+ f1 f2) (+ i 1)))))) (fib-helper 0 1 0))))
(printFib fib 10) ==> 0 1 1 2 3 5 8 13 21 34My solution used the display function to print the numbers and the let construct to ensure that the display functions were executed in the correct order (i.e., the display function that prints a number was executed before the display function that printed a space). My let statement used an empty list for the name-value bindings. Note that you will need to use Scheme's force function. Again look at the natural number example as a template for using the force function. However, your print function will need to be more sophisticated than the simple tail function defined in that example.