;;;; The define-script macro ;;;; (define abort #f) (let/cc grab-abort (set! abort grab-abort)) (define-syntax define-script (syntax-rules () [(define-script (script-name arg ...) body) (define (script-name arg ...) (abort body))])) ;; the form input function (define (request-read promptstr) (begin (printf promptstr) (read))) ;; And as a script (define-script (request-read-script promptstr action) (begin (printf promptstr) (action (read)))) ;;;;; the original voter program ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; age-page-nonweb : -> void ;; displays ability to vote based on user's age (define (age-page-nonweb) (let [(age (request-read "Enter your age: "))] (cond [(>= age 18) (printf "Don't forget to vote!")] [else (printf "You'll be able to vote in ~a years" (- 18 age))]))) (define-script (age-page2) (request-read-script "Enter your age: " (lambda (hole) (let [(age hole)] (cond [(>= age 18) (printf "Don't forget to vote!")] [else (printf "You'll be able to vote in ~a years" (- 18 age))]))))) ;;;;;;;; the adder revisited ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (clean-adder) (printf "sum: ~a~n" (+ (request-read "Enter the first number: ") (request-read "Enter the second number: ")))) (define (clean-adder2) (request-read-script "Enter the first number: " (lambda (hole1) (request-read-script "Enter the second number: " (lambda (hole2) (printf "sum: ~a~n" (+ hole1 hole2))))))) ;;;;;;;; the tip calculator ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (add-tip bill) (+ bill (* bill (/ (request-read "Enter tip percentage: ") 100)))) (define (tip-adder) (add-tip (request-read "Enter bill amount: "))) (define (add-tip2 bill) (request-read-script "Enter tip percentage: " (lambda (hole) (+ bill (* bill (/ hole 100)))))) (define (tip-adder2) (request-read-script "Enter bill amount: " add-tip2)) ;;;;;;;;; the fight instigator ;;;;;;;;;;;;;;;;;;;;;;;;;; (begin (request-read (format "In how many games should the ~a win the series? " (request-read "Who should win the World Series? "))) (printf "Dream on!~n")) (request-read-script "Who should win the World Series? " (lambda (hole1) (request-read-script (format "In how many games should the ~a win the series? " hole1) (lambda (hole2) (begin hole2 (printf "Dream on!~n"))))))