aboutsummaryrefslogtreecommitdiff
path: root/tests/lambda.lisp
blob: 0a1ad1cf6dde8da1a6e1efa906f22f9b01ab9200 (plain)
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
(defun Y (f)
  (funcall
    (lambda (g) (funcall g g))
    (lambda (h)
       (funcall f (lambda args (apply (funcall h h) args))))))

(defun fibo-impl (self)
  (lambda (n)
    (if (<= n 2)
        1
        (+ (funcall self (- n 1)) (funcall self (- n 2))))))

(defvar fibo (Y #'fibo-impl))

(assert (= 55 (funcall fibo 10)))

(defun generate-counter (init)
  (let ((i init))
    (lambda ()
      (setq i (+ 1 i))
      i)))

(let ((c (generate-counter 0)))
  (assert (= 1 (funcall c)))
  (assert (= 2 (funcall c)))
  (assert (= 3 (funcall c))))

(let ((x 1)
      (fn nil))
  (setq fn
    (lambda (x) (setq x 2)))
  (funcall fn x)
  (assert (= x 1)))