[plt-scheme] htdp section 17.7

From: wooks . (wookiz at hotmail.com)
Date: Fri Jul 7 20:10:02 EDT 2006

Apologies if this is sent twice ...... can't remember whether I pressed send 
the first time.

Please check my data analysis and examples.

(define-struct add (left right))
(define-struct mul (left right))
(define-struct div (left right))
(define-struct sub (left right))
(define-struct funcDef (name parm expr))
(define-struct funcInv (name expr))

An expression is either

     a variable
     a number or
     a multiplication or
     an addition  or
     a function invocation


A multiplication is a list (* left right)
    left is an expression
    right is an expression

An addition is a list (+ left right)
    left is an expression
    right is an expression

A subtraction is a list (- left right)
    left is an expression
    right is an expression

A division is a list (/ left right)
    left is an expression
    right is an expression

A variable is a symbol

A funcInv (function invocation) is a structure (name expr) where
      name is a symbol
      expr is an expression

A funcDef (function defiinition) is a structure (name parm expr)

name is a symbol
parm is a symbol
expr is an expression

evaluate-with-one-def : expression funcDef -> number or error
Calculates and returns the value of the expression if it is a numeric 
expression that does not contain a function invocation.

If the expression contains a function invocation and the invoked function 
matches funcDef substitute the functions body in the expression and 
calculate and return its value.

If the expression invokes a function other than that specified in funcDef 
return error.

(evaluate-with-one-def (make-funcInv 'square 8) (make-funcDef 'square
                                                              (make-mul 'num 
'num))) = 64
(evaluate-with-one-def (make-funcInv 'square (make-add 3 4)) (make-funcDef 
(make-mul 'num 'num))) = 49

(evaluate-with-one-def 8   (make-funcDef 'square
                                          (make-mul 'num 'num))) = 8

(evaluate-with-one-def (make-mul 7 11)  (make-funcDef 'square
                                          (make-mul 'num 'num))) = 77

(evaluate-with-one-def (make-funcInv 'circle (make-add 3 4)) (make-funcDef 
(make-mul 'num 'num))) = ERROR

(evaluate-with-one-def (make-funcInv 'square (make-funcInv 'square 3))
                       (make-funcDef 'square 'num (make-mul 'num 'num))) = 

(evaluate-with-one-def (make-add 7 (make-funcInv 'square (make-funcInv 
'square 3)))
                       (make-funcDef 'square 'num (make-mul 'num 'num))) = 

(evaluate-with-one-def (make-funcInv 'circle (make-funcInv 'square 7))
                       (make-funcDef 'square 'num (make-mul 'num 'num))) = 

(evaluate-with-one-def (make-funcInv 'square (make-funcInv 'circle 7))
                       (make-funcDef 'square 'num (make-mul 'num 'num))) = 

----Original Message Follows----
From: Matthias Felleisen <matthias at ccs.neu.edu>
To: wooks . <wookiz at hotmail.com>
CC: plt-scheme at list.cs.brown.edu
Subject: Re: [plt-scheme] htdp section  17.7
Date: Thu, 6 Jul 2006 14:12:38 -0400

On Jul 5, 2006, at 7:11 PM, wooks . wrote:

>re 17.7.2 - Am I right to say that the data definitions for  parameter name 
>and expression have to allow for "nulls".

If you mean to say that a parameter list is possibly empty, the  answer is 
"in principle, yes" but you may assume that is non-empty if  that helps you. 
As you may have noticed, Beginner - Intermediate  don't allow empty 
parameter lists.

>I ask with particular reference to problems 3 and 5 which seem to  require 
>a null parameter and a null expression respectively.


>re 17.7.3 - Can I have an example of what is required as I don't  
>sufficiently "get" the problem as it is set.

1. Take any program you have written. That's an example of the kind  of 
information you're dealing with. Translate it into Scheme data.  Done. Or 
translate it into Java data if this makes it easier for you.

2. The contract for the function is

  evaluate-with-defs : Expression List-of-definitions -> Value

But of course this only makes sense after you have defined the sets  
(classes) Expression, List-of-definitions, Value.

I recommend Value = Number to make life easy.

2a. I recommend you think of the Expression argument as the thing you  type 
into the Interactions window; the List-of-definitions as the  function 
definitions that you typed into the Definitions Window (and  then you hit 
RUN); and the Value is what the Interaction Windows  delivers at the end.

3. This exercise separates those who have studied the design recipe  from 
those who haven't. Or those who will be great programmers from  those who 
will struggle.

Just do it. -- Matthias

P.S. Students who have never programmed before but follow the DR to  the dot 
tend to solve this problem in a couple of hours, max. They  are also usually 
girls but that's a coincidence.

>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.