-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd-lib.lisp
54 lines (43 loc) · 1.35 KB
/
std-lib.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
(in-package :pc)
;; Macros
(define-client-macro labels (forms &rest body)
(let ((param-names (mapcar #'first forms))
(param-forms (mapcar (lambda (x) (cons 'lambda x))
(mapcar #'cdr forms))))
`(rap (lambda ,param-names ,@body) ,@param-forms)))
(define-client-macro let (forms &rest body)
(let ((param-names (mapcar #'first forms))
(param-forms (mapcar (lambda (x) (cons 'progn x))
(mapcar #'cdr forms))))
`((lambda ,param-names ,@body) ,@param-forms)))
(define-client-macro let* (forms &rest body)
(if (null (cdr forms))
`(let ,forms ,@body)
`(let (,(car forms)) (let* ,(cdr forms) ,@body))))
(define-client-macro null (val)
`(if (atom ,val)
1
0))
(define-client-constant nil 0)
(define-client-macro funcall (func &rest args)
`(,func ,@args))
(define-client-macro function (func)
func)
;; REVERSE
;; ================================
(defun reverse-inner (lst acc)
(if (null lst)
acc
(reverse-inner (cdr lst)
(cons (car lst)
acc))))
(defun reverse (lst)
(reverse-inner lst nil))
;; ===============================
;; LENGTH
(defun length (lst)
(labels ((%inner (lst acc)
(if (null lst)
acc
(%inner (cdr lst) (+ acc 1)))))
(%inner lst 0)))