98 lines
3.4 KiB
Scheme
98 lines
3.4 KiB
Scheme
;;; begin ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
(assert-equal '() (begin))
|
|
(assert-equal 123 (begin 123))
|
|
(assert-equal 456 (begin 123 456))
|
|
(assert-equal 789 (begin 123 456 789))
|
|
|
|
;;; 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)))
|
|
|
|
;;; 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)))
|
|
(assert-equal
|
|
5
|
|
(begin
|
|
(define (sum a b) (+ a b))
|
|
(define (inc a) (+ a 1))
|
|
(sum (inc 1) (inc 2))))
|
|
(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))))
|
|
|
|
;;; if ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
(assert-equal 123 (if #true 123 456))
|
|
(assert-equal 123 (if "foo" 123 456))
|
|
(assert-equal 456 (if #false 123 456))
|
|
|
|
;;; 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))
|
|
(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))))
|
|
|
|
;;; 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)))
|
|
|
|
;;; 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))
|