valeri/test/function.vli

52 lines
933 B
Common Lisp

;; -*- mode: lisp; -*-
(fn fact (n)
(if (<= n 0)
1
(* n (fact (- n 1)))))
(assert (= (fact 12) 479001600))
(let ((square (fn (x) (* x x))))
(assert (= (square 4) 16))
)
;; Closures should work at least across one scope
(let ((x 42))
(assert (= ((fn (y) (+ x y) 1)
43)))
)
;; Closures should work across multiple scopes
(let ((x 42))
(assert (=
(((fn (y)
(fn () (+ x y)))
1))
43))
)
;; Top-level functions should be able to capture the "let" scope
;; to a closure
(let ((x 42))
(fn foo() x)
)
(assert (= (foo) 42))
;; Top-level functions should be able to access top-level variables
(def four 4)
(fn add-four (x) (+ x four))
(assert (= (add-four 5) 9))
;; Passing lisp functions to native functions as parameters
;; (map is implemented in C++)
(assert (= (map '(1 2 3) (fn square (x) (* x x)))
'(1 4 9)))