Exercise 1.16
************************************************************
1. (up lst)
(define up
  (lambda (lst)
    (if (null? lst) '()
	(if (list? (car lst))
	    (append (car lst) (up (cdr lst)))
	    (cons (car lst) (up (cdr lst)))))))
(display (up '((1 2) (3 4))))
(newline)
(display (up '((x (y) z))))
(newline)

;; another solution

(define up
  (lambda (lst)
    (apply append
	   (map (lambda (x) (if (list? x) x (list x))) lst))))
(display (up '((1 2) (3 4))))
(newline)
(display (up '((x (y) z))))
(newline)

************************************************************
2. (swapper s1 s2 slist)
(define swapper
  (lambda (s1 s2 slist)
    (if (null? slist) '()
	(if (list? (car slist))
	    (cons (swapper s1 s2 (car slist))
		  (swapper s1 s2 (cdr slist)))
	    (if (eqv? (car slist) s1)
		(cons s2 (swapper s1 s2 (cdr slist)))
		(if (eqv? (car slist) s2)
		    (cons s1 (swapper s1 s2 (cdr slist)))
		    (cons (car slist) (swapper s1 s2 (cdr slist)))))))))
(display (swapper 'a 'd '(a b c d)))
(newline)
(display (swapper 'x 'y '((x) y (z (x)))))
(newline)
(display (swapper 'a 'd '(a d () c d)))
(newline)

************************************************************
3. (count-occurrence s slist)
(define count-occurrence
  (lambda (s slist)
    (cond 
     ((null? slist) 0)
     ((not (list? (car slist)))
      (cond
       ((eqv? (car slist) s) (+ 1 (count-occurrence s (cdr slist))))
       (else (count-occurrence s (cdr slist)))))
     (else (+ (count-occurrence s (car slist)) (count-occurrence s (cdr slist)))))))
(display (count-occurrence 'x '((f x) y (((x z) x)))))
(newline)
(display (count-occurrence 'x '((f x) y (((x z) () x)))))
(newline)
(display (count-occurrence 'w '((f x) y (((x z) x)))))
(newline)

************************************************************
4. (flatten slist)
(define flatten
  (lambda (slist)
    (cond 
     ((null? slist) '())
     ((not (list? (car slist)))
      (cons (car slist) (flatten (cdr slist))))
     (else (append (flatten (car slist)) (flatten (cdr slist)))))))
(display (flatten '(a b c)))
(newline)
(display (flatten '((a) () (b ()) (c))))
(newline)
(display (flatten '((a b) c (((d)) e))))
(newline)
(display (flatten '(a b (() (c)))))
(newline)

************************************************************
5. (merge lon1 lon2)
(define merge
  (lambda (lon1 lon2)
    (cond
     ((null? lon1) lon2)
     ((null? lon2) lon1)
     ((< (car lon1) (car lon2)) 
      (cons (car lon1) (merge (cdr lon1) lon2)))
     (else (cons (car lon2) (merge lon1 (cdr lon2)))))))
(display (merge '(35 62 81 90 91) '(3 83 85 90)))
(newline)

************************************************************
