-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsort.scm
85 lines (72 loc) · 1.8 KB
/
sort.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(import (basics.scm core/let.scm))
; merge-sort
; first at compile-time
(macro ct_merge (a b)
(cond (((= () a) b)
((= () b) a)
((> (car a) (car b))
(cons (car b)
(ct_merge a (cdr b))))
(true (cons (car a)
(ct_merge (cdr a) b))))))
;(ct_merge (1 3 5) (2 3 6)) -> (1 2 3 3 5 6)
(macro ct_setup (l)
(cond (((= l ()) ())
(true (cons (cons (car l) ())
(ct_setup (cdr l)))))))
;(tree (ct_setup (5 5)))
(macro ct_sort2 (l)
(cond (((= (cdr l) ()) (car l))
(true
(ct_sort2
(reverse
(cons (ct_merge (car l)
(car (cdr l)))
(reverse (cdr (cdr l))))))))))
(macro ct_sort (l)
(ct_sort2
(ct_setup l)))
;;;;;; next at run-time
(define (rt_merge f a b)
(cond (((= nil a) b)
((= nil b) a)
;((> (car a) (car b))
((not (execute f ((car a) (car b))))
(cons (car b)
(recurse f a (cdr b))))
(true (cons (car a)
(recurse f (cdr a) b))))))
;(rt_merge (tree (2 4 6)) (tree (3 5 6)))
(define (rt_sort l f)
(let ((cdrl (cdr l))
(carl (car l))
(cond (((= nil cdrl) carl)
(true
(recurse
(reverse
(cons
(rt_merge f
carl
(car cdrl))
(reverse (cdr cdrl))))
f))))))
(define (rt_sort_old l f)
(cond (((= nil (cdr l)) (car l))
(true
(recurse
(reverse
(cons
(rt_merge f
(car l)
(car (cdr l)))
(reverse (cdr (cdr l)))))
f)))))
;(execute (@ rt_sort) ((tree ((5)(2)(6)(1)(3)))))
(define (rt_setup l)
(cond (((= nil l) nil)
(true (cons (cons (car l) nil)
(recurse (cdr l)))))))
;(rt_setup ((tree (4 5 6))))
(macro sort (l g)
(rt_sort (rt_setup l) g))
;(sort (tree (1 2 3)))