8.12
First Worksheet on Naming
Start by copying all of the given code into a single DrRacket buffer. Make sure that you include the final require that actually invokes the inner module, and make sure that when you run the code you see the results computed by the expressions in the my-mod1 module.
Then, solve each exercise by adding code immediately after the commented exercise statement.
#lang racket |
;; this code defines the sim-ZODE4 language as a module |
(module sim-ZODE4 racket |
(provide |
[rename-out (#%lam-app #%app) |
(my-if if)] |
else |
#%module-begin |
#%datum |
+ - * / = equal? <= => |
true false) |
(require (for-syntax syntax/parse)) |
(define-syntax (#%lam-app stx) |
(syntax-case stx (lamb :) |
[(_ lamb : args ... : body) |
#'(lambda (args ...) body)] |
[(_ e ...) |
#'(#%app e ...)])) |
(define-syntax (my-if stx) |
(syntax-case stx (:) |
[(_ : e1 : e2 : e3) |
#'(if e1 e2 e3)]))) |
;; this module uses the sim-ZODE4 language. That is, its |
;; contents are written *in* the sim-ZODE4 language. |
;; the only edits you should make are inside this module: |
(module my-mod1 (submod ".." sim-ZODE4) |
1234 |
4567 |
{+ 4 5} |
{if : true : 34 : 39} |
{{lamb : x y : {+ x y}} 4 3} |
;; exercise 0: write a function that adds one to a number. |
;; exercise 1: combining the definition and application forms, |
;; apply the function that adds one to a number to the number 17. |
;; thought exercise: does running this program "give a name to a value" anywhere? |
;; if so, what name and what value? |
;; exercise 2: write a function that accepts a function 'h' and applies |
;; it to 8. |
;; exercise 3: combining the definition and application forms, |
;; apply the function that applies its argument to 8 to the function |
;; that adds one to a number. |
;; thought exercise: does running this program "give a name to a value" |
;; anywhere? if so, what name(s) and what value(s)? |
;; exercise 4 (a bit harder): write a function that performs function composition: |
;; that is, it accepts functions named 'f' and 'g' and returns a new |
;; function of one argument that applies first 'g' and then 'f' to its |
;; argument |
;; exercise 5 (harder): Write a program that gives the name "compose" to |
;; the function defined in the previous exercise, gives the name "add1" to |
;; the function that adds one, and then gives the name "add2" to the composition |
;; of add1 and add1, and finally applies this add2 function to 99. |
) |
;; this code actually invokes the 'my-mod1 module, so that the |
;; code runs. |
(require 'my-mod1) |