-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.lisp
196 lines (161 loc) · 4.93 KB
/
utils.lisp
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
;;; utils.lisp For stuff that doesn't fit anywhere else
(in-package :dialogues)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Iteration
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro until (condition &body body)
`(do nil (,condition) ,@body))
(defmacro while (condition &body body)
`(do nil ((not ,condition)) ,@body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Lists
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun every-pair (pred lst-1 lst-2)
"With LST-1 = (a-1 a-2 ...) and LST-2 = (b-1 b-2 ...), and PRED a
binary predicate, determine whether PRED applies
to (a-1,b-1), (a-2,b-2), ... . If LST-1 and LST-2 do not have the
same length, return NIL."
(if lst-1
(when lst-2
(when (funcall pred (first lst-1) (first lst-2))
(every-pair pred (rest lst-1) (rest lst-2))))
(null lst-2)))
(defun first-n (n lst)
(loop :for i :from 1 :upto n :for elt :in lst :collect elt))
(defun map-initial-pairs (lst-1 lst-2 fn)
(cond ((null lst-1)
(values 1 lst-2))
((null lst-2)
(values 0 lst-1))
(t
(funcall fn (car lst-1) (car lst-2))
(map-initial-pairs (cdr lst-1) (cdr lst-2) fn))))
(defun length-at-most (lst n)
(cond ((minusp n)
t)
((zerop n)
(null lst))
(t
(length-at-most (cdr lst) (1- n)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Numbers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun same-parity (m n)
(or (and (evenp m) (evenp n))
(and (oddp m) (oddp n))))
(defun pairs-summing-to (n)
"All pairs (A . B) of natural numbers summing to N."
(loop :for a :from 0 :upto n :collect (cons a (- n a))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Input and output
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun comma-separated-list (lst)
(format nil "~{~a~^, ~}" lst))
(defun read-symbol (&rest symbols)
(let (response)
(if symbols
(until (and response
(symbolp response)
(member response symbols))
(setf response (read t nil nil)))
(until (and response
(symbolp response))
(setf response (read t nil nil))))
response))
(defmacro read-yes-or-no ()
`(read-symbol 'y 'yes 'n 'no))
(defmacro with-yes-or-no (&key yes no)
`(ecase (read-yes-or-no)
(y ,@yes)
(yes ,@yes)
(n ,@no)
(no ,@no)))
(defmacro yes-or-no-go (question prompt yes-tag no-tag)
`(progn
(msg "~A" ,question)
(format t "~A" ,prompt)
(ecase (read-yes-or-no)
(y (go ,yes-tag))
(yes (go ,yes-tag))
(n (go ,no-tag))
(no (go ,no-tag)))))
(defun read-symbol-different-from (&rest symbols)
(let (response)
(until (and response
(not (member response symbols)))
(setf response (read t nil nil)))
response))
(defun read-number-in-interval (a b)
(let (response)
(until (and (numberp response)
(<= a response)
(<= response b))
(setq response (read t nil nil)))
response))
(defun read-non-negative-number-at-most (n)
(read-number-in-interval 0 (1- n)))
(defun read-number-in-interval-or-symbol (m n &rest symbols)
(let (response)
(until (or (and (numberp response)
(<= m response)
(<= response n))
(and (symbolp response)
(member response symbols)))
(setf response (read t nil nil)))
response))
(defun read-natural-number ()
(let (response)
(until (and (integerp response)
(<= 0 response))
(setf response (read t nil nil)))
response))
(defun read-positive-integer ()
(let (response)
(until (and (integerp response)
(> 0 response))
(setf response (read t nil nil)))
response))
(defun msg (format-string &rest args)
(apply #'format t (concatenate 'string format-string "~%") args))
(defmacro with-simple-prompt ((prompt) question &body body)
`(progn
(msg ,question)
(format t "~A" ,prompt)
,@body))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Strings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun empty-string? (str)
(when (stringp str)
(string= str "")))
(defun stringify (x)
(format nil "~a" x))
(defun contains-whitespace? (str)
(find-if #'(lambda (x)
(or (char= x #\Newline)
(char= x #\Tab)
(char= x #\Space)))
str))
(defun symbolify (str &optional (package *package*))
(intern (string-upcase str) package))
(defun symbolify-here (str)
(symbolify str (find-package :dialogues)))
(defun concat-strings (&rest strings)
(funcall #'concatenate
'string
strings))
(defun lex< (str-1 str-2)
(cond ((string= str-1 "")
(not (string= str-2 "")))
((string= str-2 "")
nil)
(t (let ((char-1 (char str-1 0))
(char-2 (char str-2 0)))
(cond ((char< char-1 char-2)
t)
((char= char-1 char-2)
(lex< (subseq str-1 1)
(subseq str-2 1)))
(t
nil))))))
;;; utils.lisp ends here