-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathulisp-examples.lisp
64 lines (47 loc) · 1.17 KB
/
ulisp-examples.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
;
; uLisp Zero examples
;
; Interleave two lists
(defun int (one two)
(if (null one) nil
(cons (car one)
(cons (car two)
(int (cdr one) (cdr two))))))
; Last item of a list
(defun end (lst)
(if (null (cdr lst)) (car lst)
(end (cdr lst))))
; Append two lists
(defun app (one two)
(if (null (cdr one)) (cons (car one) two)
(cons (car one) (app (cdr one) two))))
; Reverse a list
(defun rev (lst)
(if (null (cdr lst)) lst
(app (rev (cdr lst)) (cons (car lst) nil))))
; Largest Common Subsequence
(defun lgr (a b)
(if (null b) a
(lgr (cdr a) (cdr b))))
(defun lon (a b)
(if (lgr a b) a b))
(defun lcs (a b)
(if (null a) nil
(if (null b) nil
(if (eq (car a) (car b))
(cons (car a) (lcs (cdr a) (cdr b)))
(lon (lcs a (cdr b)) (lcs (cdr a) b))))))
; Hofstadter Q sequence
(defun add (one two)
(if (null (cdr one)) (cons (car one) two)
(cons (car one) (add (cdr one) two))))
(defun sub (a b)
(if (null b) a
(sub (cdr a) (cdr b))))
(defun le2 (n)
(null (cdr (cdr n))))
(defun q (n)
(if (le2 n) '(x)
(add
(q (sub n (q (cdr n))))
(q (sub n (q (cdr (cdr n))))))))