CS - PowerPoint PPT Presentation

1 / 64
About This Presentation
Title:

CS

Description:

CS. 1321. CS1321: Introduction to. Programming. Georgia Institute of Technology. College of Computing ... Spring Break & Exam 2! Spring Break is March 4th - March 8th ... – PowerPoint PPT presentation

Number of Views:85
Avg rating:3.0/5.0
Slides: 65
Provided by: ccGa
Category:
Tags: break | college | spring

less

Transcript and Presenter's Notes

Title: CS


1
1321
  • CS

2
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Lecture 16
  • February 28, 2002

3
Spring Break Exam 2!
  • Spring Break is March 4th - March 8th
  • Exam 2 is March 12, 13, 14 based on
    yourrecitation

4
Todays Menu
  • Your Readings
  • Intermezzo III
  • Similarities in Functions
  • Revising the way we think
  • Passing Values
  • Passing Functions
  • Changes to our design recipes

5
Intermezzo III and Onwards
6
However
The introduction of slick new programming
concepts that give you all sorts of shortcuts
does NOT mean you can ignore the lessons learned
from the previous lectures. In many cases,
youll find that the lessons learned will be even
more important.
7
Where we are in the book
  • The material we will be covering for the next two
    to three lectures will come from
  • Chapter 18 (Intermezzo III)
  • Chapters 19 through 22
  • Chapters 19 through 22 can be considered one
    giant chapter divided into four parts.
  • These sections of the book contain many excellent
    examples. You are highly encouraged to read
    ahead!

8
Intermezzo III (local )
Chapter 18 introduces us to a new function
(local ) The function local creates within our
Scheme environment a small area where we can
define new variables, structures or even
functions. These new definitions exist and can
be used ONLY within this new area weve defined.
This is also known as restricting the scope of
our definitions.
9
The Concept of Scope
Global Scope Here, within this box, there exists
data and rules and definitions that are
universal to our problem. Anyone can view
these items, use them, and perhaps even change
them.
Local Scope 1 Now weve created a new area to
play in. We can create new pieces of data,
rules, and definitions that exists only within
the confines of this box. We can still access
all the information within the global scope as we
are still considered to be within the global
scope.
Local Scope 4
10
Confused?
Heres a more visual explanation of whats going
on
11
At each level there are different sets of rules
and data that are scoped to the area were
considering
12
The syntax of local
( local (
.
) to be evaluated)
13
The syntax of local
Here we can define numerous local definitions
that exist only within the local. When the local
finishes evaluating, these disappear.
( local (
.
) to be evaluated)
14
The syntax of local
local, like all functions weve seen in Scheme,
resolves to a value. In this case, it resolves
to the value returned from our expression.
( local (
.
) to be evaluated)
15
An example
When We Hit Execute
16
Whats happening here? Weve created a local
scope in which we define the variable x. The
local has to resolve to a value, so we decide to
return the value of x. Right after the local
finishes, we then try to evaluate x again.
17
Here, when we press Execute, Scheme evaluates
first the call to local and then x
18
Another Example
In this example, we define two items within
local the function my-func and the variable
my-var. As our code to be executed by the local,
we call the locally defined function passing it
the locally defined variable. After the local
finishes execution, we try to call my-func
19
Again, we see the evaluation of the local
expression succeeds admirably. However, the
call to my-func outside the local fails just as
miserably as it did before.
20
A Last Example
21
A Last Example
Here we create two internal helper functions for
the main function we are writing. As we will
see, these functions are only visible within
the main function.
22
So I bet by now you have some questions about the
stuff required when you use local Lets try to
address some of these questions
23
Do I have to have a complete Design Recipes for
every helper function that I write as a local
function?
No, you do not HAVE to. These functions are
invisible to a user, who may only be concerned
with making use of your main functions, not
knowing the internal details of how you calculate
a result. Design Recipes are still required for
your global functions.
Hooray! No more commenting all the time!
Not so! The value of comments within your code
has not been diminished by this new discovery.
So we will expect at the very least a Contract
and Purpose section for every Internal function.
24
An Example
25
When to not make a helper local!
Ok, so you still have to comment. But using
local can certainly cut down on the recipe
requirement Can you always make helper functions
local, rather than global? No!
There will be situations when you develop a
helper function that will be needed by several,
if not all, of the main functions youre
writing. You must not duplicate that exact same
helper function in every single main function
that needs to use it. If youre writing a
function that is called by more than one other
function, put that helper in the global space and
do a full Design Recipe!
26
The Real Deal
Using our new-found knowledge of local, re-write
the insertion-sort function we developed several
lectures ago. You should produce only ONE global
function.
27
We start as we normally would
Data Analysis and Definition A
list-of-numbers is either 1) the empty
list empty 2) (cons n lon) where n is a
number and lon is a
list-of-numbers Contract sort
list-of-numbers ? list-of-numbers Purpose
This function takes in a list of numbers and
performs an insertion-sort on the numbers
to put the numbers into ascending order.
28
Continuing
Examples (sort empty) should produce
empty (sort (list 1 2))
should produce (list 1 2)
(sort (list 3 4 2 1)) should produce
(list 1 2 3
4) Template (define (process-lon in-lon)
(cond ((empty? in-lon) )
(else (first in-lon)
(process-lon (rest in-lon))
29
The definition one possible solution
Lets say that we want to deviate as LITTLE as
possible from the way we originally wrote our
sorting function. As we only had one helper
function (namely insert), we could create the
following definition
30
(define (sort in-lon) (local ((define (insert
value in-lon) (cond (empty? in-lon)
(list value) else (cond (
value (first in-lon))
(cons (first in-lon)
(insert value
(rest in-lon)))
else (cons value
in-lon))))) (cond (empty? in-lon) empty
else (insert (first in-lon)
(sort (rest in-lon))))))
31
(define (sort in-lon) (local ((define (insert
value in-lon) (cond (empty? in-lon)
(list value) else (cond (
value (first in-lon))
(cons (first in-lon)
(insert value
(rest in-lon)))
else (cons value
in-lon))))) (cond (empty? in-lon) empty
else (insert (first in-lon)
(sort (rest in-lon))))))
This is the local definition of our insert
function
32
(define (sort in-lon) (local ((define (insert
value in-lon) (cond (empty? in-lon)
(list value) else (cond (
value (first in-lon))
(cons (first in-lon)
(insert value
(rest in-lon)))
else (cons value
in-lon))))) (cond (empty? in-lon) empty
else (insert (first in-lon)
(sort (rest in-lon))))))
This code at the bottom is the actual body of the
sort function.
33
Using this approach
Anyone who wants to use your code can ONLY call
the sort function, not the insert function.
34
We could have also written it
(define (sort in-lon) (local (define
(insert-sort in-lon) (cond (empty?
in-lon) empty else (insert
(first in-lon)
(insert-sort (rest in-lon)))))
(define (insert value in-lon)
(cond (empty? in-lon) (list value)
else (cond ( value (first in-lon))
(cons (first in-lon)
(insert value
(rest
in-lon))) else
(cons value
in-lon)))) (insert-sort
in-lon)))
35
We could have also written it
(define (sort in-lon) (local (define
(insert-sort in-lon) (cond (empty?
in-lon) empty else (insert
(first in-lon)
(insert-sort (rest in-lon)))))
(define (insert value in-lon)
(cond (empty? in-lon) (list value)
else (cond ( value (first in-lon))
(cons (first in-lon)
(insert value
(rest
in-lon))) else
(cons value
in-lon)))) (insert-sort
in-lon)))
Now we use two internal definitions to take over
the entire functionality of the program
36
They are Functionally Equivalent
37
Want to Know More? READ INTERMEZZO III!
Intermezzo III contains a large number of
examples and detailed explanations of the
particulars of local. It would be a brilliant
idea to read that material before attempting to
use local on your own.
38
Similarities in Functions
As we develop more and more functions, one of the
things we start to notice (besides how sore our
fingers become) is just how much many of our
functions resemble each other. Theres always
that one little difference that makes it a
different function, however. So what if we
could get rid of that one little difference?
39
A simple example
(define (contains-doll? in-los) (cond ((empty?
in-los) false) (else (cond
((symbol? (first in-los) doll) true) (else
(contains-doll? (rest in-los)))))))
(define (contains-car? in-los) (cond ((empty?
in-los) false) (else (cond
((symbol? (first in-los) car) true) (else
(contains-car? (rest in-los)))))))
40
A simple example
(define (contains-doll? in-los) (cond ((empty?
in-los) false) (else (cond
((symbol? (first in-los) doll) true) (else
(contains-doll? (rest in-los)))))))
WHERES THE DIFFERENCE?
(define (contains-car? in-los) (cond ((empty?
in-los) false) (else (cond
((symbol? (first in-los) car) true) (else
(contains-doll? (rest in-los)))))))
41
A simple example
(define (contains-doll? in-los) (cond ((empty?
in-los) false) (else (cond
((symbol? (first in-los) doll) true) (else
(contains-doll? (rest in-los)))))))
(define (contains-car? in-los) (cond ((empty?
in-los) false) (else (cond
((symbol? (first in-los) car) true) (else
(contains-doll? (rest in-los)))))))
42
Getting rid of the difference
(define (contains-symbol? in-los in-symbol)
(cond ((empty? in-los) false) (else
(cond ((symbol? (first in-los)
in-symbol)
true) (else (contains-symbol?
(rest
in-los)
in-symbol))))))
Here we make a generic searching function for a
symbol in a list of symbols. To satisfy the
problems that ask us to write contains-doll? and
contains-car?, we can do the following
43
Specific functions
(define (contains-doll? in-los)
(contains-symbol? in-los doll))
(define (contains-car? in-los)
(contains-symbol? in-los car))
(define (contains-bat? in-los)
(contains-symbol? in-los bat))
(define (contains-ice-cream? in-los)
(contains-symbol? in-los ice-cream))
44
A new class of problems
So that was pretty easy. All we had to do was
add an extra parameter and we created this
generic searching function for lists of
symbols. But what about lists of numbers? Or
lists of posns? Or lists of TAs? If we examine
the code for each
45
(define (contains-num? in-lon in-num) (cond
((empty? in-lon) false) (else
(cond (( (first in-lon) in-num) true)
(else
(contains-num? (rest in-lon)

in-num))))))
46
(define (contains-posn? in-lop in-posn) (cond
((empty? in-lop) false) (else
(cond ((and ( (posn-x (first in-lop))
(posn-x
in-posn))
( (posn-y (first in-lop))
(posn-y
in-posn)))
true)
(else (contains-posn? (rest in-lop)

in-posn))))))
47
a TA has a name, hours worked and a
salary (define (contains-TA? in-loTA in-TA)
(cond ((empty? in-loTA) false)
(else (cond ((and (symbol?
(TA-name (first
in-loTA))
(TA-name in-TA))
( (TA-hours (first
in-loTA)) (TA-hours
in-TA)) ( (TA-salary (first
in-loTA)) (TA-salary in-TA)))
true) (else (contains-TA?

(rest in-loTA) in-TA))))))
48
These functions are almost identical!
  • Upon inspection, the degree to which these
    function differ is tiny!
  • Checking symbols, use symbol?
  • Checking numbers, use OR
  • Checking structures, check every field in both
    structures. (Write a function that does
    that)
  • Do we see this only when we search for values?

49
How about these problem statements?
Write a function under-threshold that takes in a
number and a list of numbers and returns a list
of numbers in which every value in our list is
SMALLER than the inputted number.
Write a function over-threshold that takes in a
number and a list of numbers and returns a list
of numbers in which every value in our list is
LARGER than the inputted number.
50
(define (under-threshold in-lon thresh) (cond
((empty? in-lon) empty) (else (cond (((first in-lon) thresh)
(cons (first in-lon)
(under-threshold (rest
in-lon) thresh))) (else (under-threshold

(rest in-lon) thresh))))))
(define (over-threshold in-lon thresh) (cond
((empty? in-lon) empty) (else (cond ((
(first in-lon) thresh)
(cons (first in-lon)
(over-threshold (rest
in-lon) thresh))) (else (over-threshold
(rest in-lon) thresh))))))
51
(define (under-threshold in-lon thresh) (cond
((empty? in-lon) empty) (else (cond (((first in-lon) thresh)
(cons (first in-lon)
(under-threshold (rest
in-lon) thresh))) (else (under-threshold

(rest in-lon) thresh))))))
(define (over-threshold in-lon thresh) (cond
((empty? in-lon) empty) (else (cond ((
(first in-lon) thresh)
(cons (first in-lon)
(over-threshold (rest
in-lon) thresh))) (else (over-threshold
(rest in-lon) thresh))))))
52
Wouldnt it be neat
Wouldnt it be nifty if we could some how pass
FUNCTIONALITY as an argument just as easily as we
pass values? We could just pass a specific
equality test to each of our contains?
functions, or a numerical comparison test to our
higher-than and lower-than functions. We could
create truly GENERIC functions that fit a common
pattern and apply specific behavior based on our
whimsy!
53
Surprise!
Most modern programming languages (Or at least
those that call themselves useful as an
anonymous instructor put it) have the ability to
do just that. We can pass, as a parameter, the
body of our function to other functions and apply
that body to a list of arguments! In many
languages this is a bizarre and complex process
involving function pointers and special glue
functions that put our pieces together. In Scheme
it just couldnt be easier.
54
Everyones seen this
We all understand, at this point that Scheme will
interpret as an addition operation to be
performed on 1 and 2.
55
And this
Now were passing the symbol to the list
function along with the numbers 1 and 2. List
creates a list of those values for us.
56
But what about
What about something like
Whats going on here?
57
It gets even weirder!
This is the body of the function my-func. If we
look carefully at the function, we see that in
the place of our usual call to the addition
function, the subtraction function, the list
function, etc., we have the parameter operator
instead!
58
Scheme has a very simple set of rules. One of
those rules is Unless quoted out, treat
anything that follows a left parenthesis as a
call to a function.
59
So our definition states
This function takes in three parameters named
operator, op1, and op2. Operator is really a
function. We should apply the function stored
in operator to the values stored in op1 and op2.
60
So we can do
61
So we can do
Pass the function and the values 1 and 2 to
my-func
62
So we can do
Pass the function - and the values 1 and 2 to
my-func
63
So we can do
Pass the function and the values 1 and 2 to
my-func
64
Next Time
How to use this to generalize functions!
Write a Comment
User Comments (0)
About PowerShow.com