1. ; three homework problems
2.
4. (let loop ((xs xs) (zs (list)))
5. (cond ((null? xs) (reverse (cons x zs)))
6. ((= x (car xs)) (append (reverse zs) xs))
7. ((< x (car xs)) (append (reverse (cons x zs)) xs))
8. (else (loop (cdr xs) (cons (car xs) zs))))))
9.
10. (define s (list))
11. (set! s (adjoin-set 1 s))
12. (set! s (adjoin-set 2 s))
13. (set! s (adjoin-set 3 s))
14. (set! s (adjoin-set 4 s))
15. (set! s (adjoin-set 5 s))
16. (set! s (adjoin-set 3 s))
17. (display s) (newline)
18.
19. (define (list-index x xs)
20. (let loop ((xs xs) (idx 0))
21. (if (null? xs) -1
22. (if (= (car xs) x) idx
23. (loop (cdr xs) (+ idx 1))))))
24.
25. (display (list-index 3 '(0 1 2 3 4))) (newline)
26. (display (list-index 7 '(0 1 2 3 4))) (newline)
27.
28. (define (update-count k kc-pairs)
29. (let loop ((kc-pairs kc-pairs) (zs (list)))
30. (cond ((null? kc-pairs) (cons (cons k 1) zs))
31. ((equal? (caar kc-pairs) k)
32. (cons (cons (caar kc-pairs) (+ (cdar kc-pairs) 1))
33. (append (cdr kc-pairs) zs)))
34. (else (loop (cdr kc-pairs) (cons (car kc-pairs) zs))))))
35.
36. (define kc-pairs (list))
37. (set! kc-pairs (update-count 3 kc-pairs))
38. (set! kc-pairs (update-count 1 kc-pairs))
39. (set! kc-pairs (update-count 3 kc-pairs))
40. (set! kc-pairs (update-count 3 kc-pairs))
41. (set! kc-pairs (update-count 2 kc-pairs))
42. (display kc-pairs) (newline)
Success #stdin #stdout 0.01s 50288KB
stdin
Standard input is empty
stdout
(1 2 3 4 5)
3
-1
((2 . 1) (1 . 1) (3 . 3))