-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.scm
68 lines (54 loc) · 1.58 KB
/
utilities.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
;;;; File: "utilities.scm", Time-stamp: <2006-05-08 16:04:37 feeley>
;;;; Copyright (C) 2004-2009 by Marc Feeley and Vincent St-Amour
;;;; All Rights Reserved.
(define keep
(lambda (keep? lst)
(cond ((null? lst) '())
((keep? (car lst)) (cons (car lst) (keep keep? (cdr lst))))
(else (keep keep? (cdr lst))))))
(define take
(lambda (n lst)
(if (> n 0)
(cons (car lst) (take (- n 1) (cdr lst)))
'())))
(define drop
(lambda (n lst)
(if (> n 0)
(drop (- n 1) (cdr lst))
lst)))
(define repeat
(lambda (n x)
(if (> n 0)
(cons x (repeat (- n 1) x))
'())))
(define pos-in-list
(lambda (x lst)
(let loop ((lst lst) (i 0))
(cond ((not (pair? lst)) #f)
((eq? (car lst) x) i)
(else (loop (cdr lst) (+ i 1)))))))
(define every
(lambda (pred? lst)
(or (null? lst)
(and (pred? (car lst))
(every pred? (cdr lst))))))
(define (sort-list l <?)
(define (mergesort l)
(define (merge l1 l2)
(cond ((null? l1) l2)
((null? l2) l1)
(else
(let ((e1 (car l1)) (e2 (car l2)))
(if (<? e1 e2)
(cons e1 (merge (cdr l1) l2))
(cons e2 (merge l1 (cdr l2))))))))
(define (split l)
(if (or (null? l) (null? (cdr l)))
l
(cons (car l) (split (cddr l)))))
(if (or (null? l) (null? (cdr l)))
l
(let* ((l1 (mergesort (split l)))
(l2 (mergesort (split (cdr l)))))
(merge l1 l2))))
(mergesort l))