-
Notifications
You must be signed in to change notification settings - Fork 8
/
corelib.lisp
117 lines (91 loc) · 2.78 KB
/
corelib.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
; This is a very basic library
; for a rather basic LISP
; interpreter.
;
; by Kristian Lein-Mathisen
; 2011, built on Andru
; Luvisi's lisp
; interpreter.
;
;
; GPL License
'=====================
'|..loading-corelib..|
'=====================
; use better naming (according
; to this article I read, you
; don't wanna use cdr)
(define rest cdr)
(define list-length
(lambda (L)
(if L
(+ 1 (list-length (cdr L)))
0)))
; I am looking forward to get this working:
(define rest-test ; that's not rest as in rest services ... but I hope
; you already knew that
(lambda (a &rest b)
(begin
(print 'a 'is a)
(print 'rest 'is &rest)
(print 'b 'is b))))
(define not
(lambda (x)
(if x
nil
t)))
(define factorial
(lambda (N)
(if (= N 1)
1
(* N (factorial (- N 1))))))
; remainder (% operator)
; returns 0 if x is divisible
; by d. the implementation is
; tricky: we count on the
; "rounding-off" at division and
; subtrack from the original
(define rem
(lambda (x d)
(- x (* (/ x d) d))))
(define is-even
(lambda (x)
(if (= 0 (rem x 2))
t
nil)))
(define is-odd
(lambda (x)
(if (is-even x)
nil
t)))
; does y divide x?
(define is-divisible
(lambda (x y)
(if (= y 1)
nil
(if (>= y x)
nil
(if (= 0 (rem x y))
t
nil)))))
(define is-prime
(lambda (x)
(if (is-even x)
nil
(is-prime-rec x 1))))
; assumes x is odd
(define is-prime-rec
(lambda (x y)
(if (is-divisible x y)
nil
(if (>= y x)
t
(is-prime-rec x (+ 2 y))))))
(define is-prime-on-list
(lambda (x)
(if (not x)
nil
(begin
(print 'prime-check (car x) (is-prime (car x) 1))
(is-prime-on-list (rest x))))))
(is-prime-on-list '(1 2 3 4 5 6 7 8 9 10 11 12 13 103))