-
Notifications
You must be signed in to change notification settings - Fork 0
/
beyond-basics.lisp
102 lines (84 loc) · 2.29 KB
/
beyond-basics.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
(define-condition foo () ()
(:report (lambda (condition stream)
(princ "Stop FOOing around, numbskull!" stream))))
;; The clean, functional part
(defun add-widget (database widget)
(cons widget database))
;; The dirty, nonfunctional part
(defparameter *database* nil)
(defun main-loop ()
(loop
(princ "Please enter the name of a new widget: ")
(setf *database* (add-widget *database* (read)))
(format t "The database contains the following: ~a~%" *database*)))
(defparameter *foo* (let ((x 5))
(lambda ()
x)))
(let ((line-number 0))
(defun my-print (x)
(print line-number)
(print x)
(incf line-number)
nil))
(defun my-length (list)
(if list
(1+ (my-length (cdr list)))
0))
(defun my-length (list)
(labels ((f (list accumulator)
(if list
(f (cdr list) (1+ accumulator))
accumulator)))
(f list 0)))
(compile 'my-length)
(defparameter *biglist* (loop for i below 100000 collect 'x))
(defun add (a b)
(let ((x (+ a b)))
(format t "The sum is ~a" x)
x))
(defmacro let1 (variable value &body body)
`(let ((,variable ,value))
,@body))
(defun add (a b)
(let1 x (+ a b)
(format t "The sum is ~a" x)
x))
;;Warning! Still contains a bug!
(defmacro split (val yes no)
`(let1 x ,val
(if x
(let ((head (car x))
(tail (cdr x)))
,yes)
,no)))
;; This function is finally safe to use.
(defmacro split (value yes no)
(let1 g (gensym)
`(let1 ,g ,value
(if ,g
(let ((head (car ,g))
(tail (cdr ,g)))
,yes)
,no))))
(defun my-length (list)
(labels ((f (list accumulator)
(split list
(f tail (1+ accumulator))
accumulator)))
(f list 0)))
(defmacro recurse (variables &body body)
(let1 p (pairs variables)
`(labels ((self ,(mapcar #'car p)
,@body))
(self ,@(mapcar #'cdr p)))))
(defun my-length (list)
(recurse (list list
accumulator 0)
(split list
(self tail (1+ accumulator))
accumulator)))
(defun my-length (list)
(reduce (lambda (x i)
(1+ x))
list
:initial-value 0))