Skip to content

Instantly share code, notes, and snippets.

@jbclements
Last active May 18, 2020 18:23
Show Gist options
  • Save jbclements/7d415708b5c9ea06146bde41f3b8ea96 to your computer and use it in GitHub Desktop.
Save jbclements/7d415708b5c9ea06146bde41f3b8ea96 to your computer and use it in GitHub Desktop.
#lang racket
(module sim-AQSE3.5 racket
(provide
(rename-out [lambda lam])
#%module-begin
#%app
#%datum
+ - * / >= <= > <
if
box
unbox
set-box!
vars
begin
)
(define-syntax-rule (vars ([x e] ...) eb)
((lambda (x ...) eb) e ...))
)
;; put all of your code in the "foo" module, below.
;; 1) write a block of code that creates a box containing 22, updates the
;; box to be 44, and then returns the value in the box.
;; 2) write a function box-incr that accepts a box and an integer and updates
;; the box to contain the original value in the box plus the value of the
;; integer. What should it return? It doesn't matter. Return the number 17.
;; 3) using "vars" and the self-reference technique we described earlier,
;; define a "while" function that accepts itself and also a "guard" and
;; a "body" procedure. The while function should call the "guard" procedure,
;; and if it returns true, call the "body" procedure and then re-invoke the
;; "while" function. If the guard returns false, the "while" function exits
;; with the value 1235.
;; 4) use your "while" function to model this loop as it would be written in C:
#|
int sum = 0;
int n = 10;
while (n > 0) {
sum = sum + n;
n = n - 1;
}
|#
(module foo (submod ".." sim-AQSE3.5)
{+ 33 44}
)
(require 'foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment