[plt-scheme] HtDP 17.7.1-4 - Diff between name-of-function and function-name
On Thu, Oct 2, 2008 at 12:23 PM, dave yrueta <dyrueta at gmail.com> wrote:
> Hi All --
>
> Two questions about exercises 17.4.*.
>
> The terms of these exercises suggests a significant difference between
> a function-name and name-of-function.
>
> Is a function-name a symbol and a name-of-function a variable, where
>
No, both are symbols. When you define a function, you give it a name
(a symbol). When you use a function, you have to look up its
definition by giving its name (also a symbol).
> A function-definition(fd) is a structure
> (make-fd f-name p-name f-body) where
> f-name is a symbol, p-name is a symbol and f-body is a scheme-
> expression, and;
>
> A name-of-function(fn) is the variable in the sentence
> (define fn fd)
> where define is a keyword, fn is a variable and fd is function-
> definition, and;
>
Here's the problem. You don't write the name-of-function definition above.
> A function-application (fa) is a structure, where
> (make-fa fn arg)
> where fn is a name-of-function and arg is a scheme-expression?
>
I actually hate this form of data/structure definition because it's
wordy and unwieldy. When I went through the training, Kathi used the
following form, which I hope will be the model for 2e. (Unless 2e uses
something like Typed Scheme, which would be even better. Declaring
types is a pain, but type checking is a lot of help for students.)
For 17.7.1, I'd do something like:
;; A function-appl is:
;; (make-function-appl symbol scheme-expr)
(define-struct function-appl (func-name argument))
Then for 17.7.2, I'd have:
;; A function-defn is:
;; (make-function-defn symbol symbol scheme-expr)
(define-struct function-defn (func-name param-name body))
I think these are completely equivalent to what you have above, except
that both func-names are symbols, not structures.
> If this is the case, then I have a follow-up question for exercise
> 17.7.4: what is the meaning of Step 2, which instructs "evaluate-with-
> defs" to "look up the definition of P is defs?"
>
> Example:,
> (define(f x) (+ 3 x) is translated to name-of-function
> (define f (make-fd 'f 'x (make-add 3 'x))), and;
>
> (define (h u) (f (* 2 u))) is translated to name-of-function
> (define h(make-fd 'h 'u(make-fa f (make-mul 2 'u))))
>
Ah. Here's the problem. You're mixing real Scheme with the fake
version of Scheme we're writing. (Maybe the next version should call
this language Skeem or something, just to be clear that we're writing
a language like Scheme in Scheme.)
Here's a translation table:
Scheme :: Skeem
(+ 3 4) :: (make-add 3 4)
(define (f x) (+ x 4)) :: (make-func-defn 'f 'x (make-add 'x 4))
(f 3) :: (make-function-appl 'f 3)
Using your examples above, I'd have:
(define(f x) (+ 3 x) :: (make-func-defn 'f 'x (make-add 3 'x)
(define (h u) (f (* 2 u))) :: (make-func-defn 'h 'u
(make-function-appl 'f (make-mul 2 'u)))
So this explains why you have to look up the function's body--you just
have the name (a symbol) when you're trying to do the application. You
have to find the function's definition in the list of definitions by
using the name, and then substitute correctly into its body.
Does that make sense?
Todd