95 lines
1.3 KiB
Plaintext
95 lines
1.3 KiB
Plaintext
;; declare lambda
|
|
($ a (-> (x) (* x 2)))
|
|
(assert= 14 (a 7))
|
|
|
|
;; high order function
|
|
($ b (-> (x y) (x (x y))))
|
|
(assert= 12 (b a 3))
|
|
|
|
;; calling function literal
|
|
(assert= 7 ( (-> (x y) (+ x y 1)) 2 4 ))
|
|
|
|
;; syntaxic sugar for function declaration
|
|
($ (c n) (* 2 n))
|
|
(assert= 18 (c 9))
|
|
|
|
;; inner variable
|
|
($ (d x) ($ y 3) (+ x y))
|
|
(assert= 7 (d 4))
|
|
|
|
;; recursivity
|
|
($ (e n)
|
|
(if (eq? 0 n) 1
|
|
(* n (e (- n 1)))))
|
|
|
|
(assert= 120 (e 5))
|
|
|
|
;; self keyword
|
|
(assert= 720 ((-> (x)
|
|
(if (eq? x 0)
|
|
1
|
|
(* x (self (- x 1))))) 6))
|
|
|
|
;; inner functions
|
|
($ (f n)
|
|
($ (inner n) (+ n 1))
|
|
(inner (inner n)))
|
|
|
|
(assert= 3 (f 1))
|
|
|
|
;; return function
|
|
($ (g)
|
|
($ (inner n) (* 7 n))
|
|
inner)
|
|
|
|
(assert= 42 ( (g) 6 ))
|
|
|
|
;; return lambda
|
|
($ (h)
|
|
(-> (n)
|
|
(* 3 (- n 2))))
|
|
|
|
(assert= 15 ( (h) 7 ))
|
|
|
|
;; closure
|
|
($ (i init)
|
|
($ counter (- init 1))
|
|
(-> ()
|
|
(set! counter (+ counter 1))
|
|
counter))
|
|
|
|
($ j (i 0))
|
|
($ k (i 16))
|
|
|
|
(assert= 0 (j))
|
|
(assert= 16 (k))
|
|
(assert= 17 (k))
|
|
(assert= 1 (j))
|
|
(assert= 2 (j))
|
|
(assert= 18 (k))
|
|
(assert= 19 (k))
|
|
(assert= 3 (j))
|
|
(assert= 4 (j))
|
|
(assert= 5 (j))
|
|
(assert= 20 (k))
|
|
|
|
($ m 1)
|
|
($ (n)
|
|
($ counter 0)
|
|
(-> ()
|
|
(set! counter (* 2 m))
|
|
(set! m (+ 1 m))
|
|
counter))
|
|
|
|
($ o (n))
|
|
($ p (n))
|
|
|
|
(assert= 2 (o))
|
|
(assert= 4 (o))
|
|
(assert= 6 (o))
|
|
|
|
(assert= 2 (p))
|
|
(assert= 4 (p))
|
|
(assert= 6 (p))
|
|
|
|
(assert= 1 m) |