-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol.lisp
34 lines (25 loc) · 881 Bytes
/
sol.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
;;; Self-organizing lists
(defun greater-cdr (x y)
"Compare the second element of two pairs."
(> (cdr x) (cdr y)))
(defun assoc-update (x ys)
"If x is in ys, update its count and return it. Otherwise, return nil."
(let ((z (assoc x ys)))
(if z
(progn
(incf (cdr z))
(car z))
nil)))
(defun reorder (ys)
"A sorted copy of ys in descending order of second element."
(stable-sort (copy-seq ys) #'greater-cdr))
(ql:quickload 'alexandria)
(defparameter *ys* (mapcar #'(lambda (x) (cons x 0)) (alexandria:iota 10)))
*ys*
(loop repeat 100 do (let ((x (random 20)))
(format t "~A?~8T~A~%" x (if
(assoc-update x *ys*)
t
nil))))
(setf *ys* (reorder *ys*))
*ys*