learning-the-little-schemer

Some of the solutions to The Little Schemer.
git clone https://kaka.farm/~git/learning-the-little-schemer
Log | Files | Refs

stuff.scm (25991B)


      1 (define (dp x . l)
      2   (cond
      3    ((null? l) (format #t "~A\n" x))
      4    (else
      5     (format #t "~A " x)
      6     (apply dp l))))
      7 
      8 (dp '(a b c)
      9     '(d e f))
     10 
     11 (define (atom? x)
     12   ;; Originally:
     13   ; (and (not (pair? x)) (not (null? x)))
     14   (not (list? x))) 
     15 
     16 (define (lat? l)
     17   (cond
     18    ((null? l) #t)
     19    ((atom? (car l)) (lat? (cdr l)))
     20    (else #f)))
     21 
     22 (define (member? a lat)
     23   (cond
     24    ((null? lat) #f)
     25    ((eq? a (car lat)) #t)
     26    (else (member? a
     27 		  (cdr lat)))))
     28 
     29 (define (member? a lat)
     30   (cond
     31    ((null? lat) #f)
     32    ((equal? a (car lat)) #t)
     33    (else (member? a
     34 		  (cdr lat)))))
     35 
     36 (define (rember a lat)
     37   (cond
     38    ((null? lat) '())
     39    ((eq? a (car lat)) (cdr lat))
     40    (else (cons (car lat)
     41 	       (rember a
     42 		       (cdr lat))))))
     43 
     44 (rember 'a '(b a c d a e))
     45 (rember 'and '(bacon lettuce and tomato))
     46 (cons 'bacon (cons 'lettuce '(tomato)))
     47 
     48 (null? 1)
     49 
     50 (define (firsts l)
     51   (cond
     52    ((null? l) '())
     53    (else (cons (car (car l))
     54 	       (firsts (cdr l))))))
     55 
     56 (firsts '((1 2)
     57 	  (3 4)
     58 	  (5 6 7)))
     59 
     60 (define (insertR new old lat)
     61   (cond
     62    ((null? lat) '())
     63    ((eq? old (car lat)) (cons old (cons new
     64 					(cdr lat))))
     65    (else (cons (car lat)
     66 	       (insertR new old (cdr lat))))))
     67 
     68 (cond
     69  ((not (equal? (insertR 'e 'd '(a b c d f g d h))
     70 	       '(a b c d e f g d h)))
     71   (display "insertR is baaaaad\n")))
     72 
     73 (define (insertL new old lat)
     74   (cond
     75    ((null? lat) '())
     76    ((eq? old (car lat)) (cons new
     77 			      lat) ;; better than (cons new (cons old (cdr lat)))
     78     )
     79    (else (cons (car lat)
     80 	       (insertL new
     81 			old
     82 			(cdr lat))))))
     83 
     84 (cond
     85  ((not (equal? (insertL 'e
     86 			'd
     87 			'(a b c d f g d h))
     88 	       '(a b c e d f g d h)))
     89   (display "insertL is baaaaad\n")))
     90 
     91 (define (subst new old lat)
     92   (cond
     93    ((null? lat) '())
     94    ((eq? old (car lat)) (cons new
     95 			      (cdr lat)))
     96    (else (cons (car lat)
     97 	       (subst new
     98 		      old
     99 		      (cdr lat))))))
    100 
    101 (define (subst2 new o1 o2 lat)
    102   (cond
    103    ((null? lat) '())
    104    ((or (eq? o1
    105 	      (car lat))
    106 	(eq? o2
    107 	     (car lat)))
    108     (cons new
    109 	  (cdr lat)))
    110    (else (cons (car lat)
    111 	       (subst2 new
    112 		       o1
    113 		       o2
    114 		       (cdr lat))))))
    115 
    116 (cond
    117  ((not (equal? (subst2 'vanilla
    118 		       'chocolate
    119 		       'banana
    120 		       '(banana ice cream with chocolate topping))
    121 	       '(vanilla ice cream with chocolate topping)))
    122   (display "subst2 is baaaad\n")))
    123 
    124 (define (multirember a lat)
    125   (cond
    126    ((null? lat) '())
    127    ((eq? a
    128 	 (car lat)) (multirember a
    129 				 (cdr lat)))
    130    (else (cons (car lat)
    131 	       (multirember a
    132 			    (cdr lat))))))
    133 
    134 (cond
    135  ((not (equal? (multirember 'cup
    136 			    '(coffee
    137 			      cup
    138 			      tea
    139 			      cup
    140 			      and
    141 			      hick
    142 			      cup))
    143 	       '(coffee tea and hick)))
    144   (display "multirember is baaad\n")))
    145 
    146 (define (multiinsertR new old lat)
    147   (cond
    148    ((null? lat) '())
    149    ((eq? old
    150 	 (car lat)) (cons old
    151 			  (cons new
    152 				(multiinsertR new
    153 					      old
    154 					      (cdr lat)))))
    155    (else (cons (car lat)
    156 	       (multiinsertR new
    157 			     old
    158 			     (cdr lat))))))
    159 
    160 (cond
    161  ((not (equal? (multiinsertR 'fried
    162 			     'fish
    163 			     '(chips
    164 			       and
    165 			       fish
    166 			       or
    167 			       fish
    168 			       and
    169 			       fried))))
    170   (display "multiinsertR is baaaaaad!!\n")))
    171 
    172 (define (multiinsertL new old lat)
    173   (cond
    174    ((null? lat) '())
    175    ((eq? old (car lat)) (cons new (cons old (multiinsertL new old (cdr lat)))))
    176    (else (cons (car lat) (multiinsertL new old (cdr lat))))))
    177 
    178 (multiinsertL 'a 'b '(b c b c b))
    179 (multiinsertL 'a 'b '(1 2 3))
    180 
    181 (define (multisubst new old lat)
    182   (cond
    183    ((null? lat) '())
    184    ((eq? old (car lat)) (cons new (multisubst new old (cdr lat))))
    185    (else (cons (car lat) (multisubst new old (cdr lat))))))
    186 
    187 (multisubst 'a 'b '(b a c b a f b a))
    188 
    189 (define (add1 n) (1+ n))
    190 
    191 (define (sub1 n) (1- n))
    192 
    193 (define (o+ n m)
    194   (cond
    195    ((zero? m) n)
    196    (else (o+ (add1 n) (sub1 m)))))
    197 
    198 (o+ 5 3)
    199 (o+ 3 5)
    200 
    201 (o+ 46 12)
    202 
    203 (define (o- n m)
    204   (cond
    205    ((zero? m) n)
    206    (else (o- (sub1 n) (sub1 m)))))
    207 
    208 (o- 5 3)
    209 (o- 3 5)
    210 
    211 (define (tup? l)
    212   (cond
    213    ((null? l) #t)
    214    ((number? (car l)) (tup? (cdr l)))
    215    (else #f)))
    216 
    217 (tup? '(1 2 3))
    218 (tup? '(1 2 a))
    219 (tup? '())
    220 
    221 (define (addtup tup)
    222   (cond
    223    ((null? tup) 0)
    224    (else (o+ (car tup) (addtup (cdr tup))))))
    225 
    226 (= (addtup '(3 5 2 8)) 18)
    227 (= (addtup '(15 6 7 12 3)) 43)
    228 
    229 (define (o* n m)
    230   (cond
    231    ((zero? m) 0)
    232    (else (o+ n (o* n (sub1 m))))))
    233 
    234 (o* 3 5)
    235 (o* 5 3)
    236 
    237 (define (tup+ tup1 tup2)
    238   (cond
    239    ((and (null? tup1)
    240 	 (null? tup2))
    241     '())
    242    ((null? tup1) tup2)
    243    ((null? tup2) tup1)
    244    (else (cons (o+ (car tup1)
    245 		   (car tup2))
    246 	       (tup+ (cdr tup1)
    247 		     (cdr tup2))))))
    248 
    249 (tup+ '(3 6 9 11 4)
    250       '(8 5 2 0 7))
    251 (tup+ '(3 6 9 11 4)
    252       '(8 5 2 0))
    253 (tup+ '(3 6 9 11)
    254       '(8 5 2 0 7))
    255 
    256 (define (o> n m)
    257   (cond
    258    ((zero? n) #f)
    259    ((zero? m) #t)
    260    (else (o> (sub1 n)
    261 	     (sub1 m)))))
    262 
    263 (o> 3 5)
    264 (o> 5 3)
    265 (o> 3 3)
    266 
    267 (define (o< n m)
    268   (cond
    269    ((zero? m) #f)
    270    ((zero? n) #t)
    271    (else (o< (sub1 n) (sub1 m)))))
    272 
    273 (o< 3 5)
    274 (o< 5 3)
    275 (o< 3 3)
    276 
    277 (define (o= n m)
    278   (not (or (o< n m) (o> n m))))
    279 
    280 (o= 3 5)
    281 (o= 5 3)
    282 (o= 3 3)
    283 
    284 (define (o^ n m)
    285   (cond
    286    ((zero? m) 1)
    287    (else (o* n (o^ n (sub1 m))))))
    288 
    289 (o= (o^ 3 5) 243)
    290 (o= (o^ 5 3) 125)
    291 
    292 (define (odiv n m)
    293   (cond
    294    ((o< n m) 0)
    295    (else (add1 (odiv (o- n m) m)))))
    296 
    297 (odiv 10 2)
    298 (odiv 3 5)
    299 (odiv 5 3)
    300 
    301 (define (olength lat)
    302   (cond
    303    ((null? lat) 0)
    304    (else (add1 (olength (cdr lat))))))
    305 
    306 (olength '(1 2 3))
    307 (olength '())
    308 
    309 (define (pick n lat)
    310   (cond
    311    ;; turns out we don't define over empty lists, so this is out:
    312    ;; ((null? lat) '())
    313    ;;
    314    ;; and we start counting at 1:
    315    ((zero? (sub1 n)) (car lat))
    316    (else (pick (sub1 n) (cdr lat)))))
    317 
    318 (pick 1 '(1 2 3))
    319 
    320 (define (rempick n lat)
    321   (cond
    322    ((zero? (sub1 n)) (cdr lat))
    323    (else (cons (car lat) (rempick (sub1 n) (cdr lat))))))
    324 
    325 (rempick 1 '(a b c))
    326 (rempick 2 '(a b c))
    327 (rempick 3 '(a b c))
    328 
    329 (define (no-nums lat)
    330   (cond
    331    ((null? lat) '())
    332    ((number? (car lat)) (no-nums (cdr lat)))
    333    (else (cons (car lat) (no-nums (cdr lat))))))
    334 
    335 (equal? (no-nums '(1 2 3 a b c 4 5 6 d e f 7 8 9))
    336 	'(a b c d e f))
    337 
    338 (define (all-nums lat)
    339   (cond
    340    ((null? lat) '())
    341    ((number? (car lat)) (cons (car lat) (all-nums (cdr lat))))
    342    (else (all-nums (cdr lat)))))
    343 
    344 (equal? (all-nums '(1 2 3 a b c 4 5 6 d e f 7 8 9))
    345 	'(1 2 3 4 5 6 7 8 9))
    346 
    347 (define (eqan? a1 a2)
    348   (cond
    349    ((and (number? a1) (number? a2)) (o= a1 a2))
    350    ;; you forgot the case of mixed types, which is very very false (not sure if it makes any sense to include):
    351    ((or (number? a1) (number? a2)) #f)
    352    (else (eq? a1 a2))))
    353 
    354 (eqan? 1 2)
    355 (eqan? 2 2)
    356 (eqan? 'a 'b)
    357 (eqan? 'a 'a)
    358 (eqan? 1 'a)
    359 
    360 (define (occur a lat)
    361   (cond
    362    ((null? lat) 0)
    363    ((eqan? a (car lat)) (add1 (occur a (cdr lat))))
    364    (else (occur a (cdr lat)))))
    365 
    366 (occur 'a '(a b c d a))
    367 (occur 'a '(a b c d a 2))
    368 (occur 2 '(a b c d a 2))
    369 
    370 (define (one? n)
    371   (cond
    372    ((zero? n) #f)
    373    ((zero? (sub1 n)) #t)
    374    (else #f)))
    375 
    376 (define (one? n)
    377   (cond
    378    ((zero? n) #f)
    379    (else (zero? (sub1 n)))))
    380 
    381 (one? 0)
    382 (one? 1)
    383 (one? 2)
    384 
    385 (define (rempick n lat)
    386   (cond
    387    ((one? n) (cdr lat))
    388    (else (cons (car lat)
    389 	       (rempick (sub1 n)
    390 			(cdr lat))))))
    391 
    392 (rempick 1 '(a b c))
    393 (rempick 2 '(a b c))
    394 (rempick 3 '(a b c))
    395 
    396 (define (rember* a l)
    397   (cond
    398    ((null? l) '())
    399    ((atom? (car l))
    400     (cond
    401      ((eqan? a (car l)) (rember* a (cdr l)))
    402      (else (cons (car l)
    403 		 (rember* a
    404 			  (cdr l))))))
    405    (else (cons (rember* a (car l))
    406 	       (rember* a (cdr l))))))
    407 
    408 (equal? (rember* 'cup '((coffee) cup ((tea) cup) (and (hick)) cup))
    409 	'((coffee) ((tea)) (and (hick))))
    410 
    411 (define (insertR* new old l)
    412   (cond
    413    ((null? l) '())
    414    ((atom? (car l))
    415     (cond
    416      ((eqan? old (car l))
    417       (cons old
    418 	    (cons new
    419 		  (insertR* new
    420 			    old
    421 			    (cdr l)))))
    422      (else (cons (car l)
    423 		 (insertR* new
    424 			   old
    425 			   (cdr l))))))
    426    (else (cons (insertR* new old (car l))
    427 	       (insertR* new old (cdr l))))))
    428 
    429 (equal? (insertR* 'roast 'chuck '((how much (wood))
    430 				  could
    431 				  ((a (wood) chuck))
    432 				  (((chuck)))
    433 				  (if (a) ((wood chuck)))
    434 				  could chuck wood))
    435 	'((how much (wood))
    436 	  could
    437 	  ((a (wood) chuck roast))
    438 	  (((chuck roast)))
    439 	  (if (a) ((wood chuck roast)))
    440 	  could chuck roast wood))
    441 
    442 (define (insertR* new old l)
    443   (cond
    444    ((null? l) '())
    445    ((atom? (car l))
    446     (cond
    447      ((eqan? old (car l))
    448       (cons old
    449 	    (cons new
    450 		  (insertR* new
    451 			    old
    452 			    (cdr l)))))
    453      (else (cons (car l)
    454 		 (insertR* new
    455 			   old
    456 			   (cdr l))))))
    457    (else (cons (insertR* new old (car l))
    458 	       (insertR* new old (cdr l))))))
    459 
    460 (define (occur* a l)
    461   (cond
    462    ((null? l) 0)
    463    ((atom? (car l))
    464     (cond
    465      ((eqan? a (car l)) (add1 (occur* a (cdr l))))
    466      (else (occur* a (cdr l)))))
    467    (else (o+ (occur* a (car l))
    468 	     (occur* a (cdr l))))))
    469 
    470 (occur* 'banana '((banana)
    471 		  (split ((((banana ice)))
    472 			  (cream (banana))
    473 			  sherbet))
    474 		  (banana)
    475 		  (bread)
    476 		  (banana brandy)))
    477 
    478 (define (subst* new old l)
    479   (cond
    480    ((null? l) '())
    481    ((atom? (car l))
    482     (cond
    483      ((eqan? old (car l)) (cons new
    484 				(subst* new
    485 					old
    486 					(cdr l))))
    487      (else (cons (car l)
    488 		 (subst* new old (cdr l))))))
    489    (else (cons (subst* new old (car l))
    490 	       (subst* new old (cdr l))))))
    491 
    492 (equal? (subst* 'orange 'banana '((banana)
    493 				  (split ((((banana ice)))
    494 					  (cream (banana))
    495 					  sherbet))
    496 				  (banana)
    497 				  (bread)
    498 				  (banana brandy)))
    499 	'((orange)
    500 	  (split ((((orange ice)))
    501 		  (cream (orange))
    502 		  sherbet))
    503 	  (orange)
    504 	  (bread)
    505 	  (orange brandy)))
    506 
    507 (define (insertL* new old l)
    508   (cond
    509    ((null? l) '())
    510    ((atom? (car l))
    511     (cond
    512      ((eqan? old (car l)) (cons new
    513 				(cons old
    514 				      (insertL* new
    515 						old
    516 						(cdr l)))))
    517      (else (cons (car l)
    518 		 (insertL* new
    519 			   old
    520 			   (cdr l))))))
    521    (else (cons (insertL* new old (car l))
    522 	       (insertL* new old (cdr l))))))
    523 
    524 (equal? (insertL* 'pecker 'chuck '((how much (wood))
    525 				   could
    526 				   ((a (wood) chuck))
    527 				   (((chuck)))
    528 				   (if (a) ((wood chuck)))
    529 				   could chuck wood))
    530 	'((how much (wood))
    531 	  could
    532 	  ((a (wood) pecker chuck))
    533 	  (((pecker chuck)))
    534 	  (if (a) ((wood pecker chuck)))
    535 	  could pecker chuck wood))
    536 
    537 (define (member* a l)
    538   (cond
    539    ((null? l) #f)
    540    ((atom? (car l))
    541     (cond
    542      ((eqan? a (car l)) #t)
    543      (else (member* a (cdr l)))))
    544    (else (or (member* a (car l))
    545 	     (member* a (cdr l))))))
    546 
    547 (member* 'chips '((potato) (chips ((with) fish) (chips))))
    548 (member* 'chips '((potato) (((with) fish) ())))
    549 
    550 (define (leftmost l)
    551   (cond
    552    ((atom? (car l)) (car l))
    553    (else (leftmost (car l)))))
    554 
    555 (leftmost '((potato) (chips ((with) fish) (chips))))
    556 (leftmost '(((hot) (tuna (and))) cheese))
    557 ;; (leftmost '(((() four)) 17 (seventeen))) ;; "No answer."
    558 ;; (leftmost '()) ;; "No answer."
    559 ;; "works on non-empty lists that don't contain empty lists."
    560 
    561 (define (eqlist? l1 l2)
    562   (cond
    563    ;; All are empty:
    564    ((and (null? l1)
    565 	 (null? l2))
    566     #t)
    567    ;; One of the lists is empty the other is not:
    568    ((or (null? l1)
    569 	(null? l2))
    570     #f)
    571    ;; All firsts are atoms:
    572    ((and (atom? (car l1))
    573 	 (atom? (car l2)))
    574     (and (eqan? (car l1) (car l2))
    575 	 (eqlist? (cdr l1)
    576 		  (cdr l2))))
    577    ;; One of the firsts is empty, the other is not:
    578    ((or (atom? (car l1))
    579 	(atom? (car l2)))
    580     #f)
    581    ;; All firsts are lists:
    582    (else
    583     (and (eqlist? (car l1) (car l2))
    584 	 (eqlist? (cdr l1) (cdr l2))))))
    585 
    586 (dp "eqlist? empty lists:" (eqlist? '() '()))
    587 (dp "eqlist? flat lists:"  (eqlist? '(strawberry ice cream) '(strawberry ice cream)))
    588 (dp (eqlist? '(strawberry ice cream) '(strawberry cream ice)))
    589 (dp (eqlist? '(banana ((split))) '((banana) (split))))
    590 
    591 (define (oequal? s1 s2)
    592   (cond
    593    ((and (atom? s1) (atom? s2))
    594     (eqan? s1 s2))
    595    ((or (atom? s1) (atom? s2))
    596     #f)
    597    (else (eqlist? s1 s2))))
    598 
    599 (define (eqlist? l1 l2)
    600   (cond
    601    ((and (null? l1)
    602 	 (null? l2))
    603     #t)
    604    ((or (null? l1)
    605 	(null? l2))
    606     #f)
    607    (else (and (oequal? (car l1) (car l2))
    608 	      (oequal? (cdr l1) (cdr l2))))))
    609 
    610 (dp "eqlist? empty lists:" (eqlist? '() '()))
    611 (dp "eqlist? flat lists:"  (eqlist? '(strawberry ice cream) '(strawberry ice cream)))
    612 (dp (eqlist? '(strawberry ice cream) '(strawberry cream ice)))
    613 (dp (eqlist? '(banana ((split))) '((banana) (split))))
    614 
    615 (define (rember s l)
    616     (cond
    617      ((null? l) '())
    618      ((atom? (car l))
    619       (cond 
    620        ((oequal? s (car l)) (cdr l))
    621        (else (cons (car l) (rember s (cdr l))))))))
    622 
    623 (define (numbered? aexp)
    624   (cond
    625    ((atom? aexp) (number? aexp))
    626    ((= 3 (length aexp))
    627     (and (numbered? (car aexp))
    628 	 (numbered? (car (cdr (cdr aexp))))
    629 	 (or (equal? '+ (car (cdr aexp)))
    630 	     (equal? '- (car (cdr aexp)))
    631 	     (equal? '* (car (cdr aexp)))
    632 	     (equal? '/ (car (cdr aexp)))
    633 	     (equal? '^ (car (cdr aexp))))))
    634    (else #f)))
    635 
    636 (dp (numbered? 2))
    637 (dp (numbered? '(2 + 3)))
    638 (dp (numbered? '((2 * 3) + (10 / 2))))
    639 (dp (numbered? '((2 * 3) + (10 = 2))))
    640 
    641 (define (arg-a nexp) (car nexp))
    642 (define (arg-b nexp) (caddr nexp))
    643 (define (op nexp) (cadr nexp))
    644 
    645 (define (value nexp)
    646   (cond
    647    ((number? nexp) nexp)
    648    ((equal? '+ (op nexp)) (+ (value (arg-a nexp))
    649 			     (value (arg-b nexp))))
    650    ((equal? '- (op nexp)) (- (value (arg-a nexp))
    651 			     (value (arg-b nexp))))
    652    ((equal? '* (op nexp)) (* (value (arg-a nexp))
    653 			     (value (arg-b nexp))))
    654    ((equal? '/ (op nexp)) (/ (value (arg-a nexp))
    655 			     (value (arg-b nexp))))
    656    (else (expt (value (arg-a nexp))
    657 	       (value (arg-b nexp))))))
    658 
    659 (dp (value '((2 * 3) + (10 / 2))))
    660 
    661 (define (sero? n)
    662   (null? n))
    663 
    664 (define (edd1 n)
    665   (cons '() n))
    666 
    667 (define (zub1 n)
    668   (cdr n))
    669 
    670 (define (plas n m)
    671   (cond
    672    ((sero? m) n)
    673    (else (plas (edd1 n) (zub1 m)))))
    674 
    675 (dp (lat? (list (edd1 '())
    676 		(edd1 (edd1 '()))
    677 		(edd1 (edd1 (edd1 '()))))))
    678 
    679 (define (set? lat)
    680   (cond
    681    ((null? lat) #t)
    682    ((= 0 (occur (car lat)
    683 		(cdr lat)))
    684     (set? (cdr lat)))
    685    (else #f)))
    686 
    687 ;; Theirs' just check existence, not a count.
    688 (define (set? lat)
    689   (cond
    690    ((null? lat) #t)
    691    ((member? (car lat)
    692 	     (cdr lat))
    693     #f)
    694    (else (set? (cdr lat)))))
    695 
    696 (dp (set? '(1 2 3)))
    697 (dp (set? '(1 2 3 2 5)))
    698 (dp (set? '(apple 1 2 3 4 5)))
    699 
    700 (define (makeseth lat)
    701   (cond
    702    ((null? lat) '())
    703    ((member? (car lat) (cdr lat))
    704     (cons (car lat)
    705 	  (makeseth (rember (car lat)
    706 			    (cdr lat)))))
    707    (else (cons (car lat)
    708 	       (makeseth (cdr lat))))))
    709 
    710 (dp (makeseth '(apple 1 2 3 2 apple apple 5)))
    711 
    712 (define (makeseth lat)
    713   (cond
    714    ((null? lat) '())
    715    ((member? (car lat) (cdr lat))
    716     (cons (car lat)
    717 	  (makeseth (multirember (car lat)
    718 				 (cdr lat)))))
    719    (else (cons (car lat)
    720 	       (makeseth (cdr lat))))))
    721 
    722 (dp (makeseth '(apple 1 2 3 2 apple apple 5)))
    723 
    724 (define (subset? set1 set2)
    725   (cond
    726    ((null? set2) (null? set1))
    727    ((null? set1) #t)
    728    ((member? (car set1) set2)
    729     (subset? (cdr set1) set2))
    730    (else #f)))
    731 
    732 (define (subset? set1 set2)
    733   (cond
    734    ((null? set1) #t)
    735    ((member? (car set1) set2)
    736     (subset? (cdr set1) set2))
    737    (else #f)))
    738 
    739 (define (subset? set1 set2)
    740   (cond
    741    ((null? set1) #t)
    742    ((and (member? (car set1) set2)
    743 	 (subset? (cdr set1) set2)))
    744    (else #f)))
    745 
    746 (define (subset? set1 set2)
    747   (cond
    748    ((null? set1) #t)
    749    (else (and (member? (car set1) set2)
    750 	      (subset? (cdr set1) set2)))))
    751 
    752 (dp (subset? '() '()))
    753 (dp (subset? '(1) '()))
    754 (dp (subset? '() '(1)))
    755 (dp (subset? '(1) '(1)))
    756 
    757 (define (eqset? set1 set2)
    758   (and (subset? set1 set2)
    759        (subset? set2 set1)))
    760 
    761 (newline)
    762 
    763 (dp (eqset? '() '()))
    764 (dp (eqset? '(1) '()))
    765 (dp (eqset? '() '(1)))
    766 (dp (eqset? '(1) '(1)))
    767 
    768 (define (intersect? set1 set2)
    769   (cond
    770    ((null? set1) #f)
    771    ((member? (car set1) set2) #t)
    772    (else (intersect? (cdr set1) set2))))
    773 
    774 (define (intersect? set1 set2)
    775   (cond
    776    ((null? set1) #f)
    777    (else (or (member? (car set1) set2)
    778 	     (intersect? (cdr set1) set2)))))
    779 
    780 (define (intersect set1 set2)
    781   (cond
    782    ((null? set1) '())
    783    ((member? (car set1) set2)
    784     (cons (car set1)
    785 	  (intersect (cdr set1) set2)))
    786    (else (intersect (cdr set1) set2))))
    787 
    788 (newline)
    789 
    790 (dp (intersect '(1 2 3) '(3 4 5)))
    791 
    792 (define (union set1 set2)
    793   (cond
    794    ((null? set1) set2)
    795    ((member? (car set1) set2)
    796     (union (cdr set1) set2))
    797    (else (cons (car set1)
    798 	       (union (cdr set1) set2)))))
    799 
    800 (dp (union '(1 2 3) '(3 4 5)))
    801 
    802 (define (set-difference set1 set2)
    803   (cond
    804    ((null? set1) '())
    805    ((member? (car set1) set2)
    806     (set-difference (cdr set1) set2))
    807    (else (cons (car set1)
    808 	       (set-difference (cdr set1) set2)))))
    809 
    810 (define (intersect-all l-set)
    811   (cond
    812    ((null? l-set) '())
    813    ((null? (cdr l-set)) (car l-set))
    814    (else (intersect (car l-set)
    815 		    (intersect-all (cdr l-set))))))
    816 
    817 (dp (intersect-all '((1 2 3)
    818 		     (3 4 5)
    819 		     (3 5 6 7))))
    820 (dp (intersect-all '((poop pee 23)
    821 		     (poop pup 23)
    822 		     (poop pork 23))))
    823 
    824 (define (a-pair? l)
    825   (cond
    826    ((null? l) #f)
    827    ((null? (cdr l)) #f)
    828    ((null? (cdr (cdr l))) #t)
    829    (else #f)))
    830 
    831 ;; Adding an atom case because you cannot cdr a non-cons atom.
    832 (define (a-pair? l)
    833   (cond
    834    ((atom? l) #f)
    835    ((null? l) #f)
    836    ((null? (cdr l)) #f)
    837    ((null? (cdr (cdr l))) #t)
    838    (else #f)))
    839 
    840 (dp (a-pair? '()))
    841 (dp (a-pair? '(())))
    842 (dp (a-pair? '(() ())))
    843 (dp (a-pair? '(() () ())))
    844 (dp (a-pair? 3))
    845 
    846 (define (first p)
    847   (car p))
    848 
    849 (define (second p)
    850   (car (cdr p)))
    851 
    852 (define (build s1 s2)
    853   (cons s1 (cons s2 '())))
    854 
    855 (define (third l)
    856   (car (cdr (cdr p))))
    857 
    858 (define (seconds l)
    859   (cond
    860    ((null? l) '())
    861    (else (cons (second (car l))
    862 	       (seconds (cdr l))))))
    863 
    864 (define (rel? l)
    865   (cond
    866    ((null? l) #t)
    867    ((member? (car l) (cdr l)) #f)
    868    ((a-pair? (car l)) (rel? (cdr l)))
    869    (else #f)))
    870 
    871 (dp "rel?")
    872 
    873 (dp (rel? '()))
    874 (dp (rel? '((1 2) (1 2))))
    875 (dp (rel? '((1 2) (1 3))))
    876 
    877 (define (fun? rel)
    878   (cond
    879    ((null? rel) #t)
    880    ((member? (second (car rel))
    881 	     (seconds (cdr rel)))
    882     #f)
    883    (else (fun? (cdr rel)))))
    884 
    885 (dp "relations and functions:")
    886 
    887 (for-each (lambda (l) (dp (list "fun?" l) '= (fun? l)))
    888 	  '(((4 3) (4 2) (7 6) (6 2) (3 4))
    889 	    ((1 2) (3 4))
    890 	    ((1 2) (3 2))))
    891 
    892 (define (fun? rel)
    893   ;; No repeats in the domain of a function.
    894   (set? (firsts rel)))
    895 
    896 (define (revrel rel)
    897   (cond
    898    ((null? rel) '())
    899    (else (cons (build (second (car rel))
    900 		      (first (car rel)))
    901 	       (revrel (cdr rel))))))
    902 
    903 (for-each (lambda (rel) (dp (list "revrel" rel) '= (revrel rel)))
    904 	  '(((8 a) (pumpkin pie) (got sick))))
    905 
    906 (define (revpair pair)
    907   (build (second pair)
    908 	 (first pair)))
    909 
    910 (define (revrel rel)
    911   (cond
    912    ((null? rel) '())
    913    (else (cons (revpair (car rel))
    914 	       (revrel (cdr rel))))))
    915 
    916 (for-each (lambda (rel) (dp (list "revrel" rel) '= (revrel rel)))
    917 	  '(((8 a) (pumpkin pie) (got sick))))
    918 
    919 (define (fullfun fun)
    920   (cond
    921    ((null? fun) #t)
    922    (else (set? (seconds fun)))))
    923 
    924 (define (fullfun fun)
    925   (set? (seconds fun)))
    926 
    927 (define (rember-f test? a l)
    928   (cond
    929    ((null? l) '())
    930    ((test? a (car l)) (cdr l))
    931    (else (cons (car l)
    932 	       (rember-f test?
    933 			 a
    934 			 (cdr l))))))
    935 
    936 (dp (rember-f equal? 'a '(1 2 a 3)))
    937 
    938 (define (rember-f test?)
    939   (lambda (a l)
    940     (cond
    941      ((null? l) '())
    942      ((test? a (car l)) (cdr l))
    943      (else (cons (car l)
    944 		 ((rember-f test?)
    945 		  a
    946 		  (cdr l)))))))
    947 
    948 (dp ((rember-f equal?) 'a '(1 2 a 3)))
    949 
    950 (define (insertL-f test?)
    951   (lambda (new old lat)
    952     (cond
    953      ((null? lat) '())
    954      ((test? old (car lat))
    955       (cons new
    956 	    lat) ;; better than (cons new (cons old (cdr lat)))
    957       )
    958      (else (cons (car lat)
    959 		 ((insertL test?)
    960 		  new
    961 		  old
    962 		  (cdr lat)))))))
    963 
    964 (define (insertR test?)
    965   (lambda (new old lat)
    966     (cond
    967      ((null? lat) '())
    968      ((test? old (car lat))
    969       (cons old (cons new
    970 		      (cdr lat))))
    971      (else (cons (car lat)
    972 		 ((insertR-f test?)
    973 		  new
    974 		  old
    975 		  (cdr lat)))))))
    976 
    977 (define (insert-g-f test?)
    978   (lambda (direction)
    979     (lambda (new old lat)
    980       (cond
    981        ((null? lat) '())
    982        ((test? old (car lat))
    983 	(cond
    984 	 ((equal? 'left direction)
    985 	  (cons new lat))
    986 	 ((equal? 'right direction)
    987 	  (cons old (cons new
    988 			  (cdr lat))))))
    989        (else (cons (car lat)
    990 		   (((insert-g-f test?) direction)
    991 		    new
    992 		    old
    993 		    (cdr lat))))))))
    994 
    995 (define (seqL new old l)
    996   (cons new (cons old l)))
    997 
    998 (define (seqR new old l)
    999   (cons old (cons new l)))
   1000 
   1001 (define (insert-g-f test?)
   1002   (lambda (seqg)
   1003     (lambda (new old lat)
   1004       (cond
   1005        ((null? lat) '())
   1006        ((test? old (car lat))
   1007 	(seqg new old (cdr lat)))
   1008        (else (cons (car lat)
   1009 		   (((insert-g-f test?) seqg)
   1010 		    new
   1011 		    old
   1012 		    (cdr lat))))))))
   1013 
   1014 (dp (((insert-g-f equal?) seqL) 1 2 '(1 2 3)))
   1015 (dp (((insert-g-f equal?) seqR) 1 2 '(1 2 3)))
   1016 
   1017 (dp "subst seqS:")
   1018 
   1019 (define (seqS new old l)
   1020   (cons new l))
   1021 
   1022 (define (subst new old l)
   1023   ;; XXX: I don't get it. Why won't it work?
   1024   ;; Oh! Now I get it. I did not call the function.
   1025   ;; It just returns the last expression. ~_~
   1026   ((insert-g-f equal?) seqS) new old l)
   1027 
   1028 (dp "Bad version:")
   1029 
   1030 (dp (((insert-g-f equal?) seqS) 2 1 '(1 2 3)))
   1031 (dp (subst 2 1 '(1 2 3)))
   1032 
   1033 (define (subst new old l)
   1034   ;; XXX: This works:
   1035   (((insert-g-f equal?) seqS) new old l))
   1036 
   1037 (dp "Fixed version:")
   1038 
   1039 (dp (((insert-g-f equal?) seqS) 2 1 '(1 2 3)))
   1040 (dp (subst 2 1 '(1 2 3)))
   1041 
   1042 (define subst
   1043   ((insert-g-f equal?) seqS))
   1044 
   1045 (dp "Book version:")
   1046 
   1047 (dp (((insert-g-f equal?) seqS) 2 1 '(1 2 3)))
   1048 (dp (subst 2 1 '(1 2 3)))
   1049 
   1050 (define (seqrem new old l) l)
   1051 
   1052 (define (rember a l)
   1053   (((insert-g-f equal?) seqrem) #f a l))
   1054 
   1055 (dp (rember 2 '(1 2 3)))
   1056 
   1057 (define (atom-to-function atom)
   1058   (cond
   1059    ((equal? atom '+) +)
   1060    ((equal? atom '*) *)
   1061    (else expt)))
   1062 
   1063 (define (value nexp)
   1064   (cond
   1065    ((atom? nexp) nexp)
   1066    (else ((atom-to-function (cadr nexp))
   1067 	  (value (car nexp))
   1068 	  (value (caddr nexp))))))
   1069 
   1070 (dp (value '((2 ^ 3) + (5 * 7))))
   1071 
   1072 (define (multirember-f test?)
   1073   (lambda (a lat)
   1074     (cond
   1075      ((null? lat) '())
   1076      ((test? a (car lat))
   1077       ((multirember-f test?) a (cdr lat)))
   1078      (else (cons (car lat)
   1079 		 ((multirember-f test?) a (cdr lat)))))))
   1080 
   1081 (dp ((multirember-f equal?) 2 '(1 2 3 2)))
   1082 
   1083 (define multirember-eq (multirember-f eq?))
   1084 
   1085 (dp (multirember-eq 2 '(1 2 3 2)))
   1086 
   1087 (define (multiremberT tester? lat)
   1088   (cond
   1089    ((null? lat) '())
   1090    ((tester? (car lat))
   1091     (multiremberT tester?
   1092 		  (cdr lat)))
   1093    (else (cons (car lat)
   1094 	       (multiremberT tester?
   1095 			     (cdr lat))))))
   1096 
   1097 (dp (multiremberT (lambda (x) (equal? 2 x)) '(1 2 3 2)))
   1098 
   1099 (define (multirember-and-co a lat col)
   1100   (cond
   1101    ((null? lat)
   1102     (col '() '()))
   1103    ((equal? a (car lat))
   1104     (multirember-and-co a
   1105 			(cdr lat)
   1106 			(lambda (newlat seen)
   1107 			  (col newlat
   1108 			       (cons (car lat)
   1109 				     seen)))))
   1110    (else
   1111     (multirember-and-co a
   1112 			(cdr lat)
   1113 			(lambda (newlat seen)
   1114 			  (col (cons (car lat) newlat)
   1115 			       seen))))))
   1116 
   1117 (dp (multirember-and-co 'tuna '(strawberries tuna and swordfish) (lambda (x y) (list x))))
   1118 
   1119 (define (multiinsertLR new oldL oldR lat)
   1120   (cond
   1121    ((null? lat) '())
   1122    ((equal? oldL (car lat))
   1123     (cons new
   1124 	  (cons oldL
   1125 		(multiinsertLR new
   1126 			       oldL
   1127 			       oldR
   1128 			       (cdr lat)))))
   1129    ((equal? oldR (car lat))
   1130     (cons oldR
   1131 	  (cons new
   1132 		(multiinsertLR new
   1133 			       oldL
   1134 			       oldR
   1135 			       (cdr lat)))))
   1136    (else (cons (car lat)
   1137 	       (multiinsertLR new
   1138 			      oldL
   1139 			      oldR
   1140 			      (cdr lat))))))
   1141 
   1142 (define (multiinsertLR-and-co new oldL oldR lat col)
   1143   (cond
   1144    ((null? lat)
   1145     (col '() '()))
   1146    ((equal? oldL (car lat))
   1147     (cons new
   1148 	  (cons oldL
   1149 		(multiinsertLR new
   1150 			       oldL
   1151 			       oldR
   1152 			       (cdr lat)
   1153 			       (lambda (newlat seenL seenR)
   1154 				 (col newlat
   1155 				      (cons (car lat)
   1156 					    seenL)
   1157 				      seenR))))))
   1158    ((equal? oldR (car lat))
   1159     (cons oldR
   1160 	  (cons new
   1161 		(multiinsertLR new
   1162 			       oldL
   1163 			       oldR
   1164 			       (cdr lat)
   1165 			       (lambda (newlat seenL seenR)
   1166 				 (col newlat
   1167 				      seenL
   1168 				      (cons (car lat)
   1169 					    seenR)))))))
   1170    (else (cons (car lat)
   1171 	       (multiinsertLR new
   1172 			      oldL
   1173 			      oldR
   1174 			      (cdr lat)
   1175 			      (lambda (newlat seenL seenR)
   1176 				(col (cons (car lat) newlat)
   1177 				     seenL
   1178 				     seenR)))))))
   1179 
   1180 (define (multiinsert-and-co new oldL oldR lat col)
   1181   (cond
   1182    ((null? lat) (col '() 0 0))
   1183    ((equal? oldL (car lat))
   1184     (multiinsert-and-co new
   1185 			oldL
   1186 			oldR
   1187 			(cdr lat)
   1188 			(lambda (newlat l-count r-count)
   1189 			  (col (cons new
   1190 				     (cons oldL newlat))
   1191 			       (1+ l-count)
   1192 			       r-count))))
   1193    ((equal? oldR (car lat))
   1194     (multiinsert-and-co new
   1195 			oldL
   1196 			oldR
   1197 			(cdr lat)
   1198 			(lambda (newlat l-count r-count)
   1199 			  (col (cons oldR
   1200 				     (cons new newlat))
   1201 			       l-count
   1202 			       (1+ r-count)))))
   1203    (else
   1204     (multiinsert-and-co new
   1205 			oldL
   1206 			oldR
   1207 			(cdr lat)
   1208 			(lambda (newlat l-count r-count)
   1209 			  (col (cons (car lat)
   1210 				     newlat)
   1211 			       l-count
   1212 			       r-count))))))
   1213 
   1214 (dp (multiinsert-and-co
   1215      'n
   1216      'l
   1217      'r
   1218      '(a a a l a a a r a a a a r a a a a r)
   1219      (lambda (a b c)
   1220        (list a b c))))
   1221 
   1222 (define (evens-only* l)
   1223   (cond
   1224    ((null? l) '())
   1225    ((atom? (car l))
   1226     (cond
   1227      ((even? (car l))
   1228       (cons (car l) (evens-only* (cdr l))))
   1229      (else
   1230       (evens-only* (cdr l)))))
   1231    (else (cons (evens-only* (car l))
   1232 	       (evens-only* (cdr l))))))
   1233 
   1234 (dp (evens-only* '(1 2 3 4 (2 3 4 5) (5 5 23 2 1))))
   1235 
   1236 ;; Wrap your bloody head around this!
   1237 (define (evens-only-and-co* l col)
   1238   (cond
   1239    ((null? l)
   1240     (col '() 1 0))
   1241    ((atom? (car l))
   1242     (cond
   1243      ((even? (car l))
   1244       (evens-only-and-co* (cdr l)
   1245 			  (lambda (new-evens evens-multiplication odds-sum)
   1246 			    (col (cons (car l)
   1247 				       new-evens)
   1248 				 (* (car l) evens-multiplication)
   1249 				 odds-sum))))
   1250      (else
   1251       (evens-only-and-co* (cdr l)
   1252 			  (lambda (new-evens evens-multiplication odds-sum)
   1253 			    (col new-evens
   1254 				 evens-multiplication
   1255 				 (+ (car l) odds-sum)))))))
   1256    (else
   1257     (evens-only-and-co* (car l)
   1258 			(lambda (new-evens-car evens-multiplication-car odds-sum-car)
   1259 			  (evens-only-and-co* (cdr l)
   1260 					      (lambda (new-evens-cdr evens-multiplication-cdr odds-sum-cdr)
   1261 						(col (cons new-evens-car
   1262 							   new-evens-cdr)
   1263 						     (* evens-multiplication-car
   1264 							evens-multiplication-cdr)
   1265 						     (+ odds-sum-car
   1266 							odds-sum-cdr)))))))))
   1267 
   1268 (dp (evens-only-and-co* '((9 1 2 8) 3 10 ((9 9) 7 6) 2)
   1269 			(lambda (a b c) (cons b (cons c a)))))