CS1321: Introduction to Programming - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

CS1321: Introduction to Programming

Description:

A new area to. code in. Local Scope 3. A new area to Play in. Local Scope 4. Confused? ... value of comments within your code has not been diminished by this ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 35
Provided by: ccGa
Category:

less

Transcript and Presenter's Notes

Title: CS1321: Introduction to Programming


1
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Module 18
  • Local Definitions and Scope

2
Intermezzo III and Onwards
3
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.
4
Where we are in the book
  • Intermezzo III
  • Chapters 19-22
  • Material for this module is within Intermezzo III
    (Ch. 18)
  • Next modules will cover chapters 19 through
    22.Chapters 19-22 can almost 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!

5
Intermezzo III (local )
  • Chapter 18 introduces 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.

6
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
7
Confused?
Heres a more visual explanation of whats going
on
8
At each level there are different sets of rules
and data that are scoped to the area were
considering
9
The syntax of local
( local (

)
)
10
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 (

)
)
11
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 the expression.
( local (

)
)
12
Contrast global scope
When We Hit Execute
13
With local scope
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.
14
When the code executes, Scheme evaluates first
the call to local and then x
15
Another Example
16
Another Example
Define two items within local 1) function
my-func and 2) variable my-var. The expression
for the local happens to call the locally defined
function, my-func, passing it the locally defined
variable, my-var. After the local finishes, we
attempt to call my-func
17
Again, we see the evaluation of the local
expression succeeds admirably. However, the
call to my-func outside the local fails miserably.
18
A Last Example
19
A Last Example
Two internal helper functions are created for the
main function being written. Notice the calls in
the locals body. These local functions are only
visible within the local. The main function
returns what the local returns.
20
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
21
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, and not
care about 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.
22
An Example
23
When to not make a helper local!
Ok, Design Recipes are still extremely important.
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!
24
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.
25
We start as we normally would
Data Analysis and Definition A
list-of-numbers is either 1) the empty
list, empty, or 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 producing a list
with the numbers in ascending order.
26
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)
27
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
28
Insertion sort with local helper, insert
(define (sort lon) (local ((define (insert value
lon) (cond (empty? lon) (list value)
else (cond (
value (first lon)) (cons
(first lon)
(insert value (rest lon)))
else (cons value
lon))))) (cond (empty? lon) empty
else (insert (first
lon) (sort (rest lon))))))
29
Insertion sort with local helper, insert
(define (sort lon) (local ((define (insert value
lon) (cond (empty? lon) (list value)
else (cond (
value (first lon)) (cons
(first lon)
(insert value (rest lon)))
else (cons value
lon))))) (cond (empty? lon) empty
else (insert (first
lon) (sort (rest lon))))))
This is the local definition of our insert
function
30
Insertion sort with local helper, insert
(define (sort lon) (local ((define (insert value
lon) (cond (empty? lon) (list value)
else (cond (
value (first lon)) (cons
(first lon)
(insert value (rest lon)))
else (cons value
lon))))) (cond (empty? lon) empty
else (insert (first
lon) (sort (rest lon))))))
The code at the bottom is the actual body of the
sort function.
31
Using this approach
Anyone who wants to use your code can ONLY call
the sort function, not the insert function.
32
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)))
33
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
34
Questions???
Write a Comment
User Comments (0)
About PowerShow.com