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).
(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)
6
|
-----------
| | |
3 5 8
/ \ |
11 9 ----------
| | | |
1 20 4 17
/ \
7 10
would be represented as the list:
'(6 (3 (11) (9)) (5) (8 (1) (20 (7) (10)) (4) (17)))An obvious way to solve the problem is to 1) write a function that performs a pre-order traversal of each tree and returns a list of the nodes in each tree, and then 2) compare the two lists. Here is some sample code that would accomplish these two tasks:
(define preorder
(lambda (L)
(cond
((null? L) L) ; base case that stops the recursion--returns a null list
; processing a list of sub-trees? If so append the nodes that
; result from a preorder search of the first subtree with the
; nodes that result from a preorder search of the remaining subtrees
((list? (car L)) (append (preorder (car L)) (preorder (cdr L))))
; else we're processing the root of the tree. add the root of the tree
; to the nodes that result from a preorder search of its subtrees
(else (cons (car L) (preorder (cdr L)))))))
(define same-nodes
(lambda (T1 T2)
(equal (preorder T1) (preorder T2))))
(preorder '(6 (5) (8 (1) (20 (7) (10)))))To show you what I want your trace execution to look like, here is a sample trace execution:
(preorder '(3 (11) (9)))
(cons 3 (preorder '((11) (9))))
(preorder '((11) (9)))
(append (preorder '(11)) (preorder '((9))))
; note that '((9)) is not a typo, it is the cdr of the list
(preorder '(11))
(cons 11 (preorder '())) ==> '(11)
; I do not need to see the trace execution of preorder on an empty list
(preorder '((9)))
(append (preorder '(9)) (preorder '()))
(preorder '(9))
(cons 9 (preorder '())) ==> '(9)
; result of (append (preorder '(9)) (preorder '()))
==> (append '(11) '(9))
==> '(11 9)
; result of (cons 3 (preorder '((11) (9))))
==> (cons 3 '(11 9))
==> '(3 11 9)
The trace execution shows which of the two branches of the conditional is
executed, then shows any non-trivial recursive executions of preorder
(trivial executions occur when preorder is called on an empty list), and
finally shows how the initial execution gets reduced to the final result
for that function call.
(fringe '(6 (3 (11) (9)) (5) (8 (1) (20 (7) (10)) (4) (17))))should return
'(11 9 5 1 7 10 4 17)Hint: I found it very helpful to write a recursive definition of fringe that use the map and fold functions from page 530 of the Scott text. 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.
(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 on page 276 of the book (example 6.84) as a template for using the force function.