-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathlist.scm
152 lines (129 loc) · 4.83 KB
/
list.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
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
#|
LambdaNative - a cross-platform Scheme framework
Copyright (c) 2009-2020, University of British Columbia
All rights reserved.
Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of the University of British Columbia nor
the names of its contributors may be used to endorse or
promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|#
;; list related extras
;; Check for empty lists - this one might be safer than using pair?
;; However, it might not be optimal from a performance perspective
;; see issue #405.
(define (list-notempty? lst) (and (list? lst) (not (null? lst))))
;; Make a list of length n, where each element is set to elem
(define (make-list n #!optional (elem 0))
(if (zero? n) '()
(cons elem (make-list (- n 1) elem))))
;; Similar to map in the sense that proc is applied to each element of lst, but
;; the result is #f if any application of proc produces #f, in which case proc is not applied to later elements of the lst; or
;; the result is #t
(define (list-andmap proc lst)
(if (null? lst)
#t
(and (proc (car lst)) (list-andmap proc (cdr lst)))))
;; mar 2010: changed implementation
;;(define (list-head l n)
;; (let ((len (length l)))
;; (if (>= n len) l
;; (reverse (list-tail (reverse l) (- len n))))))
(define (list-head l k)
(if (or (= k 0) (null? l)) '()
(cons (car l) (list-head (cdr l) (- k 1)))))
(define (list-keep lst cnd)
(let loop ((l lst))
(if (not (pair? l)) l
(let ((head (car l))(tail (cdr l)))
(if (cnd head)
(let ((new-tail (loop tail)))
(if (eq? tail new-tail) l (cons head new-tail))
)
(loop tail)
)
)
)
))
;; mapS behaves like map, except for stripping empty lists from the returned list
;; Usage: (maps FunctionToBeCalledForEachElement ListToWorkOn)
(define (maps fcn lst)
(list-keep (map fcn lst) (lambda (l) (not (and (list? l) (= (length l) 0))))))
;; do a continuous lookup
(define (list-interpolate lst x0)
(let* ((x (max (min x0 1.) 0.))
(n (- (length lst) 1))
(idx1 (macro-fix (floor (* x n))))
(idx2 (macro-fix (ceiling (* x n))))
(v1 (list-ref lst idx1))
(v2 (list-ref lst idx2)))
(+ v1 (* (- (* x n) idx1) (- v2 v1)))))
;; resample a list by linear interpolation
(define (list-resample lst n)
(let loop ((x 0.)(i 0)(nlst '()))
(if (= i n) nlst
(loop (+ x (/ 1. (- n 1.))) (+ i 1)
(append nlst (list
(list-interpolate lst x)))))))
(define (sublist lst start end)
(list-head (list-tail lst start) (- end start)))
(define (list-remove-duplicates lst)
(do ((a '() (if (member (car lst) a) a (cons (car lst) a)))
(lst lst (cdr lst)))
((null? lst) (reverse a))))
(define (list-delete-item lst item)
(cond
((fx= (length lst) 0) lst)
((equal? item (car lst)) (cdr lst))
(else (cons (car lst) (list-delete-item (cdr lst) item)))
)
)
(define (list-insert-item lst item)
(if (member item lst) lst
(append lst (list item))))
(define (list-set! lst pos item)
(cond ((or (< pos 0) (null? lst)) #f)
((= pos 0) (set-car! lst item))
(else (list-set! (cdr lst) (- pos 1) item))
)
)
(define (list-pos lst element)
(if (and (not (null? lst)) (member element lst))
(fx- (length lst) (length (member element lst)))
#f
))
;; New list filled with n elements containing s, s+1, s+2, ... s+n-1
(define (make-list-natural s n)
(if (zero? n)
'()
(cons s (make-list-natural (+ s 1) (- n 1)))
))
;; New list filled with n elements containing s, s+i, s+2*i, ... s+(n-1)*i
(define (make-list-increment s n i)
(if (zero? n)
'()
(cons s (make-list-increment (+ s i) (- n 1) i)))
)
;; eof