Approximate overview
Events
Other good things
Silence.
(func-1 9 '(1 8 2))
--> (if (null? '(1 8 2))
(list 9)
(cons (car '(1 8 2)) (func-1 9 (cdr '(1 8 2)))))
--> (if #f
(list 9)
(cons (car '(1 8 2)) (func-1 9 (cdr '(1 8 2)))))
--> (cons (car '(1 8 2)) (func-1 9 (cdr '(1 8 2))))
--> (cons 1 (func-1 9 (cdr '(1 8 2))))
--> (cons 1 (func-1 9 (cdr '(1 8 2))))
--> (cons 1 (func-1 9 '(8 2)))
--> (cons 1 (if (null? '(8 2))
(list 9)
(cons (car '(8 2)) (func-1 9 (cdr '(8 2)))))
--> (cons 1 (if #f
(list 9)
(cons (car '(8 2)) (func-1 9 (cdr '(8 2)))))
--> (cons 1 (cons (car '(8 2)) (func-1 9 (cdr '(8 2))))
--> (cons 1 (cons 8 (func-1 9 (cdr '(8 2))))
--> (cons 1 (cons 8 (func-1 9 '(2))))
--> (cons 1 (cons 8 (if (null? '(2))
(list 9)
(cons (car '(2)) (func-1 9 (cdr '(2)))))
--> (cons 1 (cons 8 (if #f
(list 9)
(cons (car '(2)) (func-1 9 (cdr '(2)))))
--> (cons 1 (cons 8 (cons (car '(2)) (func-1 9 (cdr '(2))))))
--> (cons 1 (cons 8 (cons 2 (func-1 9 (cdr '(2))))))
--> (cons 1 (cons 8 (cons 2 (func-1 9 '()))))
--> (cons 1 (cons 8 (cons 2 (if (null? '())
(list 9)
(cons (car '()) (func-1 9 (cdr '()))))))
--> (cons 1 (cons 8 (cons 2 (if #t
(list 9)
(cons (car '()) (func-1 9 (cdr '()))))))
--> (cons 1 (cons 8 (cons 2 (list 9))))
--> (cons 1 (cons 8 '(2 9)))
--> (cons 1 '(8 2 9))
--> '(1 8 2 9)
What does the procedure do?
It puts a value (
x) at the end of a list (l)
What does it do in the base case?
When you add
xto the end of the empty list, you get the list'(x)
What does it do in the recursive case?
Removes the first element from
l(with(cdr l)), addsxto the remainder (with the recursive call), and then puts the first element back at the front (with(cons (car l) ...)).
Does that achieve the goal?
It seems to.
Why?
In general, we recurse through to the end of the list, building up stuff to cons on the front. Then we do all of the delayed
conscalls, to reconstruct the rest of the list.
func-1 might be called add-to-end.func-2 might be called append.Great quotations