Last class

Call by value
	Copying.

Inefficient for arrays
Cannot do swap

Implementing Arrays:

(letarray (u[3] v[2])
  (begin
    (:= u[0] 5)
    (:= u[1] 6)
    (:= u[2] 7)
    (:= v[0] 3)
    (:= v[1] 8)
    (let ([p  (proc (x)
		(begin
                  (:= x[1] 7)
		  (:= x v)
		  (:= x[1] 9)))])
	(p u))))

Fig 6.1.6 on page 185


Indirect model:

x and u are two seperate cells pointing to the same
set of consecutive cells containing 5 6 7

Copying v to x makes x point to v.

--------

Direct model: 

x and u are TWO SETS of consecutive
cells containing 5 6 7 

Copying v to x causes first two elements of x to
be overwritten with contents of v



What happens with call by value?

(let ([p (proc (x) (:= x 5))])
 (let ([a 3]
       [b 4])
   (begin
     (p a)
     (p b)
     (+ a b))))

Result:??

Assignment restricted to scope of assigned
variable (x)


What happens with call by reference

(let ([p (proc (x) (:= x 5))])
 (let ([a 3]
       [b 4])
   (begin
     (p a)
     (p b)
     (+ a b))))

Result:??

CallbyReference:

We pass the bindings of the argument to the
procedure not the expressed value.

You can write swap.



Consider:

==> (define c 3)
==> (define p (proc (x) (:= x 5)))
==> (begin
     (p (add1 c))
     c)

??

Does the value of c change?
In most langages:  no. In such languages
WHEN THE operand IS NOT A VARIABLE OR DATA STRUCT
the value of the operand is placed in a new
location and assignments tothis location by
the called procedure have no effect that is
visible to the caller.

In such cases, 
WHEN THE operand IS NOT A VARIABLE OR DATA STRUCT
callbyvalue and callbyreference behave the same
way. 



With call by reference what does the following
expression return?

(let ([b 3]
      [p (proc (x y)
           (begin
             (:= x 4)
             y))])
 (p b b))

??

This is called ALIASING.
Aliasing makes it very difficult to understand
programs. Unpleasant surprise:


X  has written a neat swapper that doesn't need a
temporary variable but assumes that its args are
integers. 

==>
(define swap2
  (proc (x y)
    (begin 
      (:= x (+ x y))
      (:= y (- x y))
      (:= x (- x y)))))

==> (define b 1)
==> (define c 2)
==> (swap2 b c)
==> b
??
==> c
??

Isn't this a neat swapper?

Well, what happens if I type:

==> (swap2 b b)

==> b
??

X goes back to the drawing board

Call by reference:

3 cases:
If the operand is a 
1. variable   ==> pass the binding (cell)
2. array ref  ==> pass the binding (cell)
3. otherwise  ==> eval the expression then copy
into new cell as in callbyvalue.



Call-by-value-result

Callbyreference but more efficient.  Procedure
deals with LOCAL COPY (in a local cell) of
parameter then JUST BEFORE RETURNING copies value
to caller's cell.

If procedure references the parameter often then
this is more efficient. But less efficient
procedure call and return

Does not suffer from aliasing problems:

==> (define b 1)
==> (define c 2)
==> (swap2 b c)
==> b
??
==> c
??

Well, what happens if I type:

==> (swap2 b b)

==> b
??



Call by Result

passing info back to caller ONLY
Variation on CallbyValueResult when local copy is
uninitialized. 

------

Call by Value
Call by Value Result
Call by Result

are known collectively as   Call-by-Copy

------

Call by Name

Delays evaluation of operands
until used



Call by Need

Delays evaluation of operands until FIRST use 



Review:

Scheme: 1/5 of Final

Static Properties of Variables
	Free and Bound
	Scope and Lexical Address
1/5 of Final


Reduction Strategies:
	Alpha and Beta Conversions
1/5 of Final

Interpreters
2/5 of Final

	


    

Sushil Louis
Last modified: Fri Dec 10 15:55:51 PST 1999