-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.lisp
66 lines (55 loc) · 1.16 KB
/
stdlib.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
;;; the standard library for rd-lisp
(defun abs (n)
(if (< n 0)
(- 0 n)
n))
(defun foldl (fn init lst)
(if lst
(foldl fn
(fn init (car lst))
(cdr lst))
init))
(defun foldr (fn init lst)
(if lst
(fn (car lst)
(foldr fn init (cdr lst)))
init))
(defun reverse (lst)
(foldl (lambda (lst elem) (cons elem lst)) nil lst))
(defun map (fn lst)
(foldr (lambda (x rest)
(cons (fn x) rest))
nil
lst))
(defun filter (pred lst)
(foldr (lambda (x rest)
(if (pred x)
(cons x rest)
rest))
nil
lst))
(defun list (&rest items)
items)
(defun list* (&rest forms)
(foldr
(lambda (e acc)
(if (pair? e)
(if (null? acc)
(append e acc)
(cons e acc))
(cons e acc)))
nil
forms))
(defun caar (x) (car (car x)))
(defun cadr (x) (car (cdr x)))
(defun cddr (x) (cdr (cdr x)))
(defun append-two (lst1 lst2)
(if (null? lst1)
lst2
(cons (car lst1) (append-two (cdr lst1) lst2))))
(defun append (&rest lsts)
(foldr
(lambda (lst acc)
(append-two lst acc))
nil
lsts))