(define fib
(lambda (n)
(cond ((= n 0) 0)
((= n 1) 1)
(else
(+ (fib (- n 2))
(fib (- n 1)))))))
> (fib 6)
8
>
(define add
(lambda (s)
(if (pair? s)
(+ (add (car s))
(add (cdr s)))
(if (null? s)
0
s))))
> (add '(((1 2) 3 4) ((5))))
15
>
(define swap
(lambda (x y s)
(if (pair? s)
(cons (swap x y (car s))
(swap x y (cdr s)))
(cond ((eq? s x) y)
((eq? s y) x)
(else
s)))))
> (swap 'foo 'bar '((foo) bar))
((bar) foo)
>
(define copy
(lambda (s)
(if (pair? s)
(cons (copy (car s))
(copy (cdr s)))
s)))
> (copy '((1) ((2)) 3))
((1) ((2)) 3)
>
(define biggest
(lambda (s)
(if (pair? s)
(max (biggest (car s))
(biggest (cdr s)))
(if (null? s)
0
s))))
> (biggest '((1 2 3) (4 5)))
5
>
(define list-add
(lambda (ls)
(cond ((null? ls) 0)
((pair? (car ls))
(+ (list-add (car ls))
(list-add (cdr ls))))
(else
(+ (car ls)
(list-add (cdr ls)))))))
;; Compare this with swap.
(define list-swap
(lambda (x y ls)
(cond ((null? ls) '())
((pair? (car ls))
(cons (list-swap x y (car ls))
(list-swap x y (cdr ls))))
((eq? (car ls) x)
(cons y (list-swap x y (cdr ls))))
((eq? (car ls) y)
(cons x (list-swap x y (cdr ls))))
(else
(cons (car ls)
(list-swap x y (cdr ls)))))))
(define list-biggest
(lambda (ls)
(cond ((null? ls) 0)
((pair? (car ls))
(max (list-biggest (car ls))
(list-biggest (cdr ls))))
(else
(max (car ls)
(list-biggest (cdr ls)))))))
(define reverse-all
(lambda (ls)
(cond ((null? ls) '())
((pair? (car ls))
(append (reverse-all (cdr ls))
(list (reverse-all (car ls)))))
(else
(append (reverse-all (cdr ls))
(list (car ls)))))))