1
0
Fork 0
lesson-lisp/tests/syntax.scm

99 lines
3.4 KiB
Scheme
Raw Permalink Normal View History

2023-05-07 10:11:11 -04:00
;;; begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(assert-equal '() (begin))
(assert-equal 123 (begin 123))
(assert-equal 456 (begin 123 456))
(assert-equal 789 (begin 123 456 789))
2023-05-06 15:13:35 -04:00
2023-05-08 11:16:52 -04:00
;;; cond ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(assert-equal '() (cond))
(assert-equal '() (cond (#false 123)))
(assert-equal 123 (cond (#true 123)))
(assert-equal '() (cond (#false 123) (#false 456)))
(assert-equal 123 (cond (#true 123) (#true 456)))
(assert-equal 456 (cond (#false 123) (#true 456)))
(assert-equal 123 (cond (#true 123) (#true 456) (else 789)))
(assert-equal 456 (cond (#false 123) (#true 456) (else 789)))
(assert-equal 789 (cond (#false 123) (#false 456) (else 789)))
2023-05-07 10:11:11 -04:00
;;; define ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(assert-equal
'(123 579)
(begin
(define x 123)
(define y (+ x 456))
(list x y)))
(assert-equal
'(123 456)
(begin
(define x 123)
(define old x)
(define x 456)
(list old x)))
2023-05-07 12:23:35 -04:00
(assert-equal
5
(begin
(define (sum a b) (+ a b))
(define (inc a) (+ a 1))
(sum (inc 1) (inc 2))))
2023-05-08 08:05:25 -04:00
(assert-equal
11
(begin
(define (make-inc const-num) (lambda (var-num) (+ var-num const-num)))
(define inc1 (make-inc 1))
(define inc10 (make-inc 10))
(inc1 (inc10 0))))
2023-05-07 09:28:45 -04:00
2023-05-07 10:11:11 -04:00
;;; if ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(assert-equal 123 (if #true 123 456))
(assert-equal 123 (if "foo" 123 456))
(assert-equal 456 (if #false 123 456))
2023-05-06 15:13:35 -04:00
2023-05-07 11:29:35 -04:00
;;; lambda ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(assert-true (procedure? (lambda () 123)))
(assert-true (procedure? (lambda (x) (+ x x))))
(assert-true (procedure? (lambda (x y) (+ x y))))
(assert-true (procedure? (lambda (x y z) (+ x y z))))
(assert-equal 123 ((lambda () 123)))
(assert-equal 3 ((lambda (x) (+ 1 x)) 2))
(assert-equal 6 ((lambda (x y) (+ 1 x y)) 2 3))
(assert-equal 10 ((lambda (x y z) (+ 1 x y z)) 2 3 4))
2023-05-07 12:05:44 -04:00
(assert-equal
5
(begin
(define sum (lambda (a b) (+ a b)))
(define inc (lambda (a) (+ a 1)))
(sum (inc 1) (inc 2))))
(assert-equal
5
(let ([sum (lambda (a b) (+ a b))]
[inc (lambda (a) (+ a 1))])
(sum (inc 1) (inc 2))))
2023-05-07 11:29:35 -04:00
2023-05-07 11:13:16 -04:00
;;; let ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(assert-equal 'foo (let () 'foo))
(assert-equal '(1) (let { [a 1] } (list a)))
(assert-equal '(1 2) (let { [a 1] [b 2] } (list a b)))
(assert-equal '(1 3) (let { [a 1] [b (+ a 2)] } (list a b)))
(assert-equal '(1 2 3) (let { [a 1] [b 2] [c 3] } (list a b c)))
(assert-equal '(1 3 6) (let { [a 1] [b (+ a 2)] [c (+ b 3)] } (list a b c)))
2023-05-07 10:11:11 -04:00
;;; quote ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(assert-equal '+ (quote +))
(assert-equal '+ '+)
(assert-equal '() (quote ()))
(assert-equal '() '())
(assert-equal #true (quote #true))
(assert-equal #true '#true)
(assert-equal #false (quote #false))
(assert-equal #false '#false)
;(assert-equal #\n (quote #\n))
;(assert-equal #\n '#\n)
(assert-equal 'foo (quote foo))
(assert-equal 'foo 'foo)
(assert-equal "foo" (quote "foo"))
(assert-equal "foo" '"foo")
(assert-equal 123 (quote 123))
(assert-equal 123 '123)
(assert-equal '(cons 123 456) (quote (cons 123 456)))
(assert-equal '(cons 123 456) '(cons 123 456))