;; To run this code, first open interpreter.scm in DrRacket and type (start), ;; then type (load-quietly "examples.txt") at the ==> interpreter prompt. ;; ;;---------------------------------------------------------------------------- ;; Examples of BlooP programs (define procedure "square" (n) (block 0 begin (output <= (n * n)))) (define procedure "cube" (n) (block 0 begin (output <= (n * n)) (output <= (output * n)))) (define procedure "two-to-the-three-to-the" (n) (block 0 begin ((cell 0) <= 1) (loop n times (block 1 begin ((cell 0) <= (3 * (cell 0))))) ((cell 1) <= 1) (loop (cell 0) times (block 2 begin ((cell 1) <= (2 * (cell 1))))) (output <= (cell 1)))) (define procedure "factorial" (n) (block 0 begin (output <= 1) ((cell 0) <= 1) (loop n times (block 1 begin (output <= (output * (cell 0))) ((cell 0) <= ((cell 0) + 1)))))) (define procedure "even?" (n) (block 0 begin (if ((remainder n 2) = 0) then (output <= yes)))) (define procedure "odd?" (n) (block 0 begin (if ((remainder n 2) = 1) then (output <= yes)))) (define procedure "prime?" (n) (block 0 begin (if (n < 2) then (quit block 0)) ((cell 0) <= 2) (loop at most (minus n 2) times (block 1 begin (if ((remainder n (cell 0)) = 0) then (quit block 0)) ((cell 0) <= ((cell 0) + 1)))) (output <= yes))) (define procedure "goldbach?" (n) (block 0 begin (if (not (even? n)) then (quit block 0)) ((cell 0) <= 2) (loop at most n times (block 1 begin (print "testing" (cell 0) "and" (minus n (cell 0))) (if ((prime? (cell 0)) and (prime? (minus n (cell 0)))) then (block 2 begin (output <= yes) (quit block 0))) ((cell 0) <= ((cell 0) + 1))))))