CS 227 lecture -*- Outline -*-
context: review skills for the course,
note that programming is relating one piece of data to another
(give examples of such functions, draw pictures)
background: students need to see procedures as instructions
set up in advance that deal with parameters that
they don't see. (Blind robots)
* Procedures (Section 2.2)
Seen enough primitives, now start putting primitives
together and naming them (Combination and Abstraction)
** why?
*** want a (better) name for some important algorithm or idea
(define first
(lambda (ls) (car ls)))
(define rest
(lambda (ls) (cdr ls)))
*** get tired of typing the same kind of expression over and over again
(cons 'bush '())
(cons 'shrub '())
(cons 'tree '())
(cons (cons 'forest '()) '())
This is what computers are good for,
they can carry out a task over and over without complaint
draw a picture of black box for this and for cons inside it.
(define list1
(lambda (item)
(cons item '())))
** examples
; discuss the problem to be solved, identify the parameter,
; enter these, and play with them
(define add1
(lambda (n)
(+ n 1)))
(define eq-it?
(lambda (thing)
(eq? 'it thing)))
(define roulette-wheel
(lambda ()
(random 37)))
(define average
(lambda (a b)
(/ (+ a b) 2.0)))
** What is a procedure?
the following is a bit repititious
*** values
draw picture of black boxes for cons, list1, eq-it?, roulette-wheel
**** use
Show how to hook up boxes:
with inputs: (cons 'ho '())
(list1 'bush)
(average 22 33)
**** connection
with other boxes
(cons (+ 1 2) '())
(list1 (list1 (+ 1 2)))
*** algorithms
Recall that:
a description of how to compute the result is called an algorithm.
compare to receipe for spaghetti
We describe algorithms in Scheme code
e.g. list1's algoritm is to use cons
The thing that is the value of this code is a procedure (Object)
the representation of our code in Scheme
*** Scheme notation for algorithms
Scheme rep for this black box
(lambda (params ...) expr)
have to name the wires, those are the formal parameters.
starts with lambda, name formal prarmeters in list,
then give expression
The expression in the lambda is the algorithm (insides of box).
**** procedures can be named
using define just like (define x 3).
**** formal parameters are just placeholders
(lambda (x) x) = (lambda (y) y)
type these examples in
also show values of +, cons, car
**** use
***** prefix notation
in Math, write f(x,y), g(x)
x & y are arguments
in Scheme, write (f x y), (g x)
e.g., (cons 1 '()) or (eq? 'hi 'mom)
operator, operands
Give example invocations
how the arguments hook up to parameters
Can they summarize? How is it related to the machine diagrams?
(do this in small groups)
Symbolically.... the beta rule
-----------
APPLYING PROCEDURES
(define list1
(lambda (item)
(cons item '())))
(list1 'bit)
=
((lambda (item) (cons item '())) 'bit)
=
(cons 'bit '())
==>
(bit)
(list1 (+ 200 27))
=
((lambda (item) (cons item '()))
(+ 200 27))
=
((lambda (item) (cons item '())) 227)
=
(cons 227 '())
==>
(227)
----------
explain the substitution model (the beta rule)
(Note to keep in mind but not necessarily explain to students:
when you substitute the def of a procedure, you have
to watch out for capturing. In general, you have
to rename as in the following.)
(define two 2)
(define add2
(lambda (x) (+ x two)))
(define add4
(lambda (two) (add2 (add2 two))))
(add4 3)
=
((lambda (two) (add2 (add2 two))) 3)
not=
((lambda (two) (add2 ((lambda (x) (+ x two)) two))) 3)
The beta rule only applies to values, not expressions,
in Scheme.
show application to literals, expressions,
and to variables,
show application to a variable that
the students don't know the value of (unknown, do (load "unknown.ss"))
this is what procedures are good for
*** types
**** can group boxes by type
we write down the shape of the box with the type notation
give concrete examples of types, classify them
----------------------
FUNCTIONS
(PROCEDURE VALUES)
def: let S, T, and U be types of values.
A function of type (-> (S T) U) is
a rule that assigns to each x:S and y:T
a result of type U.
----------------------
**** types
---------------------
PROCEDURE OBJECTS
def: let S, T, and U be types of objects.
A procedure of type (-> (S T) U) is
an expression of the form
(lambda (x y) e)
where x and y are names,
and e is an expression
such that if x:S and y:T, then e:U.
EXAMPLES
type (-> (number) number)
(lambda (n) (+ n 1))
(lambda (m) (/ m 2))
(lambda (x) (* x x))
type (-> (T) (list T))
(lambda (item) (cons item '()))
(lambda (s) (cons s (cons 'y '())))
----------------------
explain the examples on slide
define formal parameters, body
*** Helping Procedures
-------------------
HELPING PROCEDURES
Problem: group words on a menu
(chicken soup ice cream)
==> ((chicken soup) (ice cream))
(lunch meat cole slaw)
==> ((lunch meat) (cole slaw))
Call the procedure "regroup":
type: (-> ((list symbol))
(list (list symbol))))
(regroup '(chicken soup ice cream))
==> ((chicken soup) (ice cream))
(regroup '(lunch meat cole slaw))
==> ((lunch meat) (cole slaw))
-------------------
draw picture of the regroup machine
have them group for 30 seconds to get as many questions as they can
about what a solution to this problem would look like
Could write procedure to regroup lists of 4 into
lists of 2 lists of two
Have them try to solve this in groups.
get volunteers to put theirs on the board
and explain
To solve this I'd first form a plan (alg. receipe) in English
Have them do this in groups
discuss some plans before showing this:
------------------
ALGORITHMS
1. get first 2 items in list
get the last two in a list
put the two lists together
or
2. get first 2 items in a list
get the 2nd cdr of the list
put these together
-------------------
draw pictures of these as machines
How to translate this into code?
Have them start in groups (2 min)
is anyone making progress?
try to translate the pieces
------------------
TRANSLATING INTO SCHEME
(first-group lst)
= the first 2 items of lst in a list
(last-group lst)
= the last 2 items of lst in a list
(cons lst1 (cons lst2 '()))
= put lst1 together with lst2
first-group: (-> ((list symbol))
(list symbol))
last-group: (-> ((list symbol))
(list symbol))
------------------
Note the increase of precision,
use of symbolic names for the arguments
Now experiment on the computer to get the right translations.
write them down in lambda notation.
(The following is a heavy-duty formal explanation.)
------------------
TRANSLATION
(regroup menu)
=
put (first-group menu)
together with (last-group menu)
=
(cons (first-group menu)
(cons (last-group menu) '()))
=
((lambda (menu)
(cons (first-group menu)
(cons (last-group menu) '())))
menu)
(define regroup
(lambda (menu)
(cons (first-group menu)
(cons (last-group menu) '()))))
------------------
Now we *know* this is right,
testing ensures we haven't made typographical errors
Now need to write first-group and last-group
------------------
FIRST-GROUP
(first-group '(x y z w))
==> (x y)
(define first-group
(lambda (menu)
(cons (car menu)
(cons (cadr menu)
'()))))
;; similarly
(define last-group
(lambda (lst)
(cddr lst)))
-------------------
-----------------------
DO HELPING PROCEDURES HELP?
(define reform ; from p. 22
(lambda (list-of-4)
(cons
(cons (car list-of-4)
(cons (cadr list-of-4)
'()))
(cons (cddr list-of-4) '()))))
------------------------
make comparisons
no significant efficiency difference
Could regroup be improved?
it makes 2 lists of 2, writing the same code twice...
------------
IMPROVED VERSION
(define list2
; TYPE: (-> (T T) (list T))
(lambda (x y)
(cons x (cons y '()))))
(define first-group2
(lambda (menu)
(list2 (car menu)
(cadr menu))))
(define regroup2
(lambda (menu)
(list2
(first-group2 menu)
(last-group menu))))
------------
** the list primitive (p. 37)
Scheme has a primitive that generalizes both list1 and list2
---------------
the procedure LIST
(list) ==> ()
(list 'a) ==> (a)
(list 'a 'b 'c) ==> (a b c)
(list 1 2 (+ 2 1) 4) ==> (1 2 3 4)
'(a b c) = (list 'a 'b 'c)
'(1 (2)) = (list 1 (list 2))
(define first-group3
(lambda (menu)
(list (car menu)
(cadr menu))))
(define regroup3
(lambda (menu)
(list
(first-group3 menu)
(last-group menu))))
---------------
this is more handy than cons in many situations
like in building (1 (2))
** group exercise on the homework
pick a homework problem handed in (HW2, problem4, Fall 93)
form groups of 3 (or 2 or 4)
- introduce yourself
- describe how you started working on this (your plan)
- say what didn't work and what worked
- explain what your final solution, if any, looked like
- if you have more than 2 solutions, pick the clearest and least clear
get these on the board