;;; ======================================= ;;; Simple examples, not involving sheets, ;;; files, or graphics ;;; ======================================= ;;; The factorial function (bulk-define fact integer integer (lambda (x) (expect (>= x 0)) (cond ((<= x 1) 1) (#t (* x (fact (- x 1))))))) ;;; vector inputs (bulk-define vect-combine ((integer-point 2) (integer-point 2)) (integer-point 2) (lambda (x y) (* 2 (+ x y)))) ;;; multiple return values (bulk-define split integer (integer integer) (lambda (x) (values x (* 2 x)))) ;;; no inputs, returns random value (bulk-define random-output () real (lambda () (random-real 2.0 4.0))) ;;; simple example of a conditional (bulk-define cond-sqrt integer real (lambda (x) (if (> x 3) (sqrt x) x))) ;;; ================================================================ ;;; compiler handling of missing values ;;; ================================================================ ;;; user specifies nothing: tests for missing inputs are ;;; inserted automatically (bulk-define test (integer integer) integer (lambda (x y) (if (< x y) (+ x 1) (* y 2)))) ;;; Test explicitly inserted by user (redundant in this case, ;;; but useful for removing tests from inside loops in other ;;; examples). (bulk-define test (integer integer) integer (lambda (x y) (expect (not (missing? x))) (if (< x y) (+ x 1) (* y 2)))) ;;; Another way to explicitly structure tests at the user level (bulk-define test (integer integer) (integer integer) (lambda (x y) (if (or (missing? x) (missing? y)) (values (make-missing) (make-missing)) (values (+ x 1) (* y 2))))) ;;; ================================================================ ;;; more complex ones ;;; ================================================================ ;;; a loop which adds the squares of the integers ;;; between i and b (inclusive) (bulk-define loop (integer integer) integer (lambda (i b) (do ((j i (+ j 1)) (sum 0)) ((> j b) sum) (set! sum (+ sum (* j j)))))) ;;; Set an integer-grid at all positions without a scan ;;; From Dan's thesis. (bulk-define without-scan ((integer-grid 2 1) integer) unspecified (lambda (ig value) (do ((x 0 (+ x 1))) ((= x 640)) (do ((y 0 (+ y 1))) ((= y 480)) (sample-set! (nearest-sample ig x y) value)))))