-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0.efma.scm
84 lines (72 loc) · 2.6 KB
/
0.efma.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
;;;; SICP 2.2.3
(define nil '())
(define (enumerate-interval low high)
(if (> low high)
nil
(cons low (enumerate-interval (+ low 1) high))))
(define (enumerate-tree tree)
(cond ((null? tree) nil)
((not (pair? tree)) (list tree))
(else (append (enumerate-tree (car tree))
(enumerate-tree (cdr tree))))))
(define (filter predicate sequence)
(cond ((null? sequence)
nil)
((predicate (car sequence))
(cons (car sequence)
(filter predicate
(cdr sequence))))
(else (filter predicate
(cdr sequence)))))
;; (define (map p sequence)
;; (accumulate (lambda (x y) (cons (p x) y))
;; nil
;; sequence))
;; fold-right starts from the end and calculates backwards --
;; This is due to applicative-order by which the inner-most expression is evaluted first
(define (accumulate op initial sequence) ; fold-right: (op s_1 ... (op s_2 (op s_n initial)) ... )
(if (null? sequence) initial
(op (car sequence) ; next: previous element
(accumulate op initial (cdr sequence))))) ; prev: next element
(define (flatmap proc seq)
(accumulate append
nil
(map proc seq)))
(define (remove item sequence)
(filter (lambda (x) (not (eq? x item)))
sequence))
(define (permutations s)
(if (null? s)
(list nil)
(flatmap (lambda (x)
(map (lambda (p) (cons x p))
(permutations (remove x s))))
s)))
(define (combinations s r)
(if (null? s) s
(if (= r 1) (map (lambda (x) (cons x nil)) s) ; Hack
(append
(map (lambda (x) (cons (car s) x))
(combinations (cdr s) (- r 1)))
(combinations (cdr s) r)))))
(define (product r s)
(if (null? s) r
(map (lambda (x) (product (cons x r) (cdr s)))
(car s))))
(define (product lists) ; https://stackoverflow.com/a/20591545
(accumulate (lambda (new acc)
(append-map (lambda (v)
(map (lambda (l)
(cons v l))
acc))
new))
'(())
lists))
(define (shuffle list) ; Fisher-Yates shuffle (https://en.wikipedia.org/wiki/Fisher–Yates_shuffle)
(define (iter shuffled original)
(if (null? original) shuffled
(let ((i (random (length original))))
(iter (cons (list-ref original i) shuffled)
(append (take original i)
(cdr (drop original i)))))))
(iter '() list))