summaryrefslogtreecommitdiff
path: root/scheme/aps/list-utils.sls
blob: 95b351f4f292962a69e17b8c7b6fce7ec94e02ce (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!r6rs
(library (aps list-utils)
(export range enumerate zip transpose distinct? let+ perm list-of-aux list-for
        remove-dupl append-unique fold flatten list-of normalize symbol-table)
(import (rnrs) (sweet-macros) (aps cut) (for (aps lang) expand))

;;; macros

;;LET+
(def-syntax let+
  (syntax-match ()
    (sub (let+ expr)
         #'expr)
    (sub (let+ (() lst) expr)
         #'(if (null? lst) expr
               (apply error 'let+ "Too many elements" lst)))
    (sub (let+ ((arg1 arg2 ... . rest) lst) expr)
         #'(let ((ls lst))
             (if (null? ls)
                 (apply error 'let+ "Missing arguments" '(arg1 arg2 ...))
                 (let+ (arg1 (car ls))
                   (let+ ((arg2 ... . rest) (cdr ls)) expr)))))
    (sub (let+ (name value) expr)
         #'(let ((name value)) expr)
         (identifier? #'name)
         (syntax-violation 'let+ "Argument is not an identifier" #'name))
    (sub (let+ (name value) (n v) ... expr)
         #'(let+ (name value) (let+ (n v) ... expr)))
    ))
;;END

;;LIST-OF

(def-syntax list-of-aux
  (syntax-match (in is)
  
    (sub (list-of-aux expr acc)
     #'(cons expr acc))
  
    (sub (list-of-aux expr acc (var in lst) rest ...)
     #'(let loop ((ls lst))
         (if (null? ls) acc
             (let+ (var (car ls))
               (list-of-aux expr (loop (cdr ls)) rest ...)))))
   
    (sub (list-of-aux expr acc (var is exp) rest ...)
     #'(let+ (var exp) (list-of-aux expr acc rest ...)))
  
    (sub (list-of-aux expr acc pred? rest ...)
     #'(if pred? (list-of-aux expr acc rest ...) acc))
  ))

(def-syntax (list-of expr rest ...)
  #'(list-of-aux expr '() rest ...))

;;END

(def-syntax (list-for decl ... expr)
  #'(list-of-aux expr '() decl ...))

;;SYMBOL-TABLE
(def-syntax (symbol-table arg ...)
  (: with-syntax
     ((name value) ...)
     (map (syntax-match ()
            (sub n #'(n n) (identifier? #'n))
            (sub (n v) #'(n v) (identifier? #'n)))
          #'(arg ...))
     #'(letrec ((name value) ...)
         (list (list 'name name) ...))))
;;END

;;; utilities

;;RANGE
(define range 
  (case-lambda 
    ((n) (range 0 n 1))
    ((n0 n)
     (range n0 n 1))
    ((n0 n s)
     (begin
       (assert (and (for-all number? (list n0 n s)) (not (zero? s))))
       (let ((cmp (if (> s 0) >= <=)))
         (let loop ((i n0) (acc '()))
           (if (cmp i n) (reverse acc)
               (loop (+ i s) (cons i acc)))))))))
;;END

;;ZIP
(define (zip . lists)
  (assert (for-all list? lists))
  (apply map list lists))
;;END

;;TRANSPOSE
 (define (transpose llist)
   (if (and (list? llist) (for-all list? llist))
       (apply map list llist)
       (error 'transpose "not a list of lists" llist)))
;;END

;;ENUMERATE
(define (enumerate lst)
  (zip (range (length lst)) lst))
;;END

;;DISTINCT?
;; check if the elements of a list are distinct according to eq?
(define (distinct? eq? items)
  (if (null? items) #t ; no items
      (let+ ((first . rest) items)
        (cond
         ((null? rest) #t); single item
         ((exists (cut eq? first <>) rest) #f); duplicate
         (else (distinct? eq? rest)); look at the sublist
         ))))
;;END

;;REMOVE-DUPL
;; ex: (remove-dupl = '(1 2 3 1 5 2 4)) => (1 2 3 5 4)
(define (remove-dupl eq? lst)
  (reverse
   (fold-left
    (lambda (acc el)
      (if (exists (cut eq? <> el) acc); duplicate
          acc
          (cons el acc)))
    '() lst)))
;;END

;;APPEND-UNIQUE
;; ex: (append-unique = '(1 2 3) '(1 5 2 4)) => (1 2 3 5 4)
(define (append-unique eq? . lists)
  (remove-dupl eq? (apply append lists)))
;;END

;;FOLD
(def-syntax fold
  (syntax-match (left right in)
     (sub (fold left (acc seed) (x in lst) (x* in lst*) ... new-acc)
           (: with-syntax (a a* ...) (generate-temporaries #'(x x* ...))
              #'(fold-left
                 (lambda (acc a a* ...)
                   (let+ ((x x* ...) (list a a* ...)) new-acc))
                 seed lst lst* ...)))
     (sub (fold right (acc seed) (x in lst) (x* in lst*) ... new-acc)
           (: with-syntax (a a* ...) (generate-temporaries #'(x x* ...))
              #'(fold-right
                 (lambda (a a* ... acc)
                   (let+ ((x x* ...) (list a a* ...)) new-acc))
                 seed lst lst* ...)))
     ))
;;END

;;FLATTEN
(define (flatten lst)
  (fold right (a '()) (x in lst)
        (if (list? x) (append (flatten x) a) (cons x a))))
;;END

;;PERM
;; compute the permutations of a list of distinct elements
(define (perm eq? lst)
  (cond
   ((null? lst) '()); empty list
   ((null? (cdr lst)) (list lst)); single element list
   (else; multi-element list 
    (list-of (cons el ls)
             (el in lst)
             (ls in (perm eq? (remp (cut eq? el <>) lst)))))))
;;END

;;NORMALIZE
(define (normalize ls)
  (map (syntax-match ()
         (sub n #'(n n) (identifier? #'n))
         (sub (n v) #'(n v) (identifier? #'n)))
       ls))
;;END
)