-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex-1-17.scm
executable file
·67 lines (59 loc) · 1.75 KB
/
ex-1-17.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
;;; 1
;;; <binary-search-tree> ::= ()
;;; ::= (<number> <binary-search-tree> <binary-search-tree>)
;;;
;;; If first < n, left path, if first > n, right path, if first = n found,
;;; if first is null, return what we have.
;;; This only works if the number is contained in the tree,
;;; i.e. if the number does not exist, it returns all the
;;; steps of the unsuccessful search.
(define path-iter
(lambda (n bst lst)
(if (null? bst)
lst
(let ((first (car bst))
(rest (cdr bst)))
(if (= first n)
(reverse lst)
(if (< n first)
(path-iter n (car rest) (cons 'left lst))
(path-iter n (cadr rest) (cons 'right lst))))))))
(define path
(lambda (n bst)
(path-iter n bst '())))
;;; 2
;;; <lon> ::= () | (<num> . <lon>)
;; Insert n after the first element in lon (a sorted list of numbers)
;; less than or equal to n
(define insert
(lambda (n lon)
(if (null? lon)
(list n)
(if (<= n (car lon))
(cons n lon)
(cons (car lon) (insert n (cdr lon)))))))
(define sort
(lambda (lon)
(if (null? lon)
'()
(insert (car lon) (sort (cdr lon))))))
;; (sort2 pred lon)
;; Can we go to the end of the list and work backwards?
;; No, that leads back to the problem of trying to insert something
;; in the right place.
;; So, do the same thing as for the first sort - recursively sorting
;; the cdr guarantees that we first sort a list of length 1, then
;; length 2 and so on, so each successive application works on a sorted
;; list.
(define insert2
(lambda (n pred lon)
(if (null? lon)
(list n)
(if (not (pred n (car lon)))
(cons (car lon) (insert2 n pred (cdr lon)))
(cons n lon)))))
(define sort2
(lambda (pred lon)
(if (null? lon)
'()
(insert2 (car lon) pred (sort2 pred (cdr lon))))))