CS - PowerPoint PPT Presentation

1 / 120
About This Presentation
Title:

CS

Description:

... LAMINA LAURE LEDA LENS LIANA LIFE LIMBO LINDA LIS LISP LML LOGO LOOEE LOOKS LORE ... Brown. CMU. Indiana. MIT. Rice. Utah. Yale. Florida. Why Use Scheme? ... – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 121
Provided by: ccGa
Category:
Tags: brown | linda

less

Transcript and Presenter's Notes

Title: CS


1
1321
  • CS

2
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Lecture 1
  • Jan 8th, 2002
  • Spring Semester

3
Who I am
Who Jim Greenlee Email jkg_at_cc.gatech.edu
(use cs1321 in the subject line) Office Room
115, College of Computing. Course
http//www.cc.gatech.edu/classes/AY2002/cs1321 My
website http//swiki.cc.gatech.edu8080/cs2130/2
5 Office hours T, Th 930-1100 a.m.
4
Who I am
Who David Smith Email dmsmith_at_cc.gatech.edu
(use cs1321 in the subject line) Office Room
123, College of Computing. Course
http//www.cc.gatech.edu/classes/AY2002/cs1321 My
website http//www.cc.gatech.edu/fac/David.Smith
Office hours M, W 100-300 p.m.
5
Who I am
Who Monica Sweat Email sweat_at_cc.gatech.edu
(use cs1321 in the subject line) Office Room
120, College of Computing. Course
http//www.cc.gatech.edu/classes/AY2002/cs1321 My
website http//www.cc.gatech.edu/sweat Office
hours T, Th 300 400 pm (subject to change)
6
University Vocabulary
  • Lecture Typically led by Professor or Lecturer.
    Sometimes big.
  • Lab Formerly held in equipped laboratory. Now
    hands on labs are done on your own.
  • Recitation Typically led by Teaching Assistant
    (TA). Smaller group. Questions and answers.

7
Basic Schedule
Lecture Tues Thurs (1.5 hours each) You
MUST go to the section youve signed up for For
example, the homeworks might vary from section to
section. Recitation One night/week 600 p.m.
to 800 p.m.
8
Resources
  • Book DrScheme Website
  • How To Design Programs
  • Felleisen, Findler, Flatt, Krishnamurthi
  • http//www.htdp.org
  • Class Web Site
  • http//www.cc.gatech.edu/AY2002/CS1321
  • Lecture slides
  • Examples
  • On-line Lab Manual
  • And much more

9
Resources
  • Newsgroups required reading!
  • My Web Site (Jim Greenlee)
  • http//swiki.cc.gatech.edu8080/cs2130/25
  • Again, DrScheme Web Site
  • http//www.htdp.org

10
Resources
  • Newsgroups required reading!
  • My Web Site
  • http//www.cc.gatech.edu/fac/David.Smith
  • Lecture slides
  • Examples
  • And much more
  • Again, DrScheme Web Site
  • http//www.htdp.org

11
Resources
  • Newsgroups required reading!
  • My Web Site
  • http//www.cc.gatech.edu/sweat
  • Announcements occasionally
  • Personal stuff
  • And much more
  • Again, DrScheme Web Site
  • http//www.htdp.org

12
Course Text
How to Design Programs Matthias Felleisen
Robert Bruce Findler Matthew Flatt Shriram
Krishnamurthi ISBN 0-262-06218-6 Online copy
available at http//www.htdp.org
13
Help
  • Follow the following sequence of people
  • TA (or newsgroup)
  • Senior TA (STA) your TA's supervisor
  • Instructor/Program Manager

14
Graded items
  • Tests given during recitation, dates TBA
  • Final exam consult OSCAR for estimated date
    time, but subject to change!
  • Homework many. 5 day turnaround. Basically
    assigned Tuesday, due the next Monday.
  • Lab 6 total often simple.
  • Note Done on your own the name lab is a
    misnomer and is misleading.

15
Course Grading Breakdown
Evaluation (no curve) Test 33 Hw
20 Lab 15 Final 30 BB 2
16
Some Tips
  • For many students this will be your first
    exposure to Computer Science (unlike Math,
    Chemistry, etc.)

17
Tip 1
Dont wait until the last minute to get help
18
Tip 2
Hey, can I still pass if I can get enough partial
credit?
Bad things happen while learning a new skill.
You will probably crash and burn on some
programs. Start early give yourself time for
mistakes.
19
Tip 3
Dont be too ambitious with your course load.
You CANNOT slack off in this class, even for a
few days.
20
Tip 4
Watch out for the big picture. Dont forget
this is a programming course, not a Scheme
course. Its dangerous to hide from the
programming part of the course. You may be
crushed on the final.
21
Todays Menu
0. What is Computer Science? 1. What is an
Algorithm? 2. Why Study Computer Science? 3.
Which Computer Language? 4. Why Scheme? 5.
Course Mechanics 6. On to Scheme
22
0. What is Computer Science?
23
Many possible answers...
languages
hardware
algorithms
from different perspectives
24
Definitions
Computer science is the study of what computers
do.
25
Rather Dry Definition
Computer science, today more accurately a
separate discipline, considers a number of rather
mathematical topics. In addition to computability
questions arising from many problems in discrete
mathematics, and logic questions related to
recursion theory, one must consider scheduling
questions, stochastic models, and so on.
Computer Science is no more about computers
than astronomy is about telescopes.
26
Definitions
"Computer Science is, simply put, the study of
computation. The study of anything that lends
itself to a computational model is a study in
Computer Science."
27
Definitions
Computer science revolves around computational
processes, which are also called information
processes or simply processes. A process is a
dynamic succession of eventsa happening.
28
ProcessWhat?
?
So we define CS based on a study of
computational processes--- Whats a
process?
29
A Definition Through Indirection
Another way of describing a process is "a
procedure in execution." A procedure is a
collection of instructions, that results in some
useful behavior on behalf of the device that
executes the instruction. When expressed in a
symbolic language, the procedure is called a
computer program.
What's a procedure?
30
1. What is an Algorithm?
31
Algorithms
  • An algorithm is a precise method of solving a
    problem
  • A given problem may be solvable by a number of
    different algorithms
  • An algorithm will typically use levels of
    abstraction to make the solution clearer and
    implementation easier.

32
Once upon a time
  • Many years ago programmers realized that
    programming a complex task would be simpler if
    they could break the big job down into pieces

33
Once upon a time
  • This was the birth of modular programming where
    big programs get broken down into smaller pieces
    called modules, methods, subroutines, procedures
    or functions.

34
Abstraction
  • Breaking down a problem into components that
    finally just do one thing is called abstraction.

35
Algorithms Abstraction
  • A key feature of a well written algorithm will be
    proper use of abstraction
  • This will provide an algorithm that
  • is more understandable
  • is easier to implement and test
  • provides more reusable modules
  • will get a higher grade!

A
36
2. Why Study Computer Science?
37
Myth
Computer science is for techno geeks who want to
drive expensive luxury imports.
38
Imagine
Taking a class about bicycling And never getting
on a bike! So we propose to you, to spend some
cycles learning about computer programming Will
you be ready for the Tour de France?
39
The Inevitable...
If we propose to study CS, we need the
convenience of a symbolic language to express and
communicate our instructions. Hence, well use
a computer language to learn about processes and
procedures.
40
3. Which Computer Language?
41
Flavors
A ABC ABEL ABCL ACCENT ACTOR ADA ADL AGORA AKL
ALEF ALEPH ALF ALGOL ALLOY AMBER AMIGA E AML AMPL
appall AVAIL AWK B BABEL BASIC BAWK BeBOP BEFUNGE
BETA BEGL BERTRAND BIGWIG BISTRO BLISS BLUE
BRIAN BRILLIANT BSL C C C C C (GNU CECIL
CHARITY CHEETAH CHiLL CILK CLAIRE CLARITY CLEAN
CLIPS CLOS CLU COBOL COLD FUSION COMAL COMMON
LISP COOL COMPONENT PASCAL CORAL COQ CORBA COSY
CWEB DAD DARK BASIC DELPHI DEMETER DIALECT
DRAGOON DYLAN DYNACE E ECLIPSE Eiffel EGG ELF
ELASTIC C ELLIE EMERALD ENIAC ERLANG ESCHER
EuLISP EUPHORIA FRANZ LISP FMPL FORMS/3 FORTH
FORTRAN G GEL GENTLE GNU E GDEL GREEN GUILE
HASKELL HOPE HPCODE HUGO ICI ICON INFER ISWIM J
JADE JAVA JAVA BEANS JAVASCRIPT JoCAML JOY JUICE
K KALEIDOSCOPE KEVO LAGOONA LAMINA LAURE LEDA
LENS LIANA LIFE LIMBO LINDA LIS LISP LML LOGO
LOOEE LOOKS LORE LPC LUA LUSTRE M4 MAGENTA MAGMA
MATCHMAKER MATLAB MCPL MELD MELD C MENTAT MERCURY
MEROON MIRANDA MISA ML MODULA 2 MODULA 3 MOPS
MOZART MUMPS NAPIER 88 NAWK NEON NESL NET REXX
NEWTON NIAL O2 OAKLISP OBERON OBJ OBJECTIVE C
OBJECTIVE CAML OBJECT OBLIQ OCCAM OCTAVE OLABL
OMEGA ONTIC OPAL ORCA OSCAR OSQL OZ PAL PARASOL
PARLOG PASCAL PERL PECOS PFL PHOCUS PHANTOM PHP
PICO PICT PIKE PILE PIRANHA PIRL PIZZA PL/B PL/I
PLANKALKUL PLAIN PLIANT POLKA POLYGOTH PRESTO
PROGRESS POOL POP POPTALK POSTSCRIPT POWERBUILDER
PROBE PROFAN PROGRAPH PROLOG PROMETHEUS PROTEUS
PY PYTHON Q QUEST R REBOL REFINE REGINA RELFUN
REXX RPG RUBY S SAS SATHER SCALLOP SCHEME
SCREAMER SCRIPT X SED SEL SELF SEQUENCE L SEPIA
SIMULA SINA SIRI SISAL SMALLTALK SMSL SNAML
SNOBOL SNOOPS SPANNER SQUEAK SQL SR SUNEIDO T3X
Tcl/TK TECO TELESCRIPT TELOS TEMPO THETA TOM TOP
TRAITS TRELLIS TURBO PASCAL TURING TYCOON UFO
UNLAMBDA V VIRT VISUAL BASIC VISUAL BASICSCRIPT
VISUAL C WINDUCTOR WINTERP WOOL XLISP XML YAFL
YERK YORICK Z ZPL ZUSE XSCHEME
Assembler, C, C, Fortran, Java, Scheme, Lisp
42
?
Whats the purpose of a computer language?
43
Consider
To understand why there are so many computer
languages, we might also consider why there are
many spoken languages. Exactly how many
spoken languages are there?
44
Consider
Total Living Languages Percentage The
Americas 1,000 15 Africa 2,011 30 Europ
e 225 3 Asia 2,165 32 The
Pacific 1,302 19 ------ TOTAL 6,703
Source www.sil.org/ethnologue
45
Insight
We have so many spoken languages because there
are so many cultures and contexts for
societies. Similarly, there are many different
computer languages because they each come from
unique contexts, and address different types of
problems and needs.
46
Asked Another Way
Why are there so many computer languages? (Why
not create one perfect computer language?) For
whom does one write programs in a language? (Why
not write them in 1s and 0s?)
?
47
Typically
  • Different types of problems lead to different
    approaches with computer languages
  • Scientific computation - Fortran
  • List processing - Lisp
  • Business programming - Cobol
  • Systems programming - C
  • Old CS education favorite Pascal
  • Object-Oriented programming C
  • OOP thats good for running over internet - Java

48
Computer Languages
Computer languages have two related
purposes efficiently provide instructions to
machines that execute programs and
procedures provide a clear written record of
the theory or idea expressed in the
procedure
1.
Computer consumption
2.
Human consumption
49
Computer Languages
We could program in 1s and 0s (machine language),
but will instead employ a high-level language.
Were using a language called Scheme, plus
introducing some basic software tools (Matlab,
Excel, HTML, etc.) But note that -- what you
learn is not restricted to the language --
ideas expressed in the language can be
expressed in other languages
50
Revelation
We are therefore writing programs in Scheme (and
not 1s and 0s that the computers prefer)
because. . .
... we want other people to understand our ideas
and programs. In other words, computer
languages are used to communicate with OTHER
PEOPLE (as well as computers).
51
Note!
This is not a class in Scheme programming This
is a class about computation and problem
solving. Its just handy to actually use a
programming language while we study the subject
at hand programming and problem solving.
Scheme is "incidental but essential" to the
course subject. Were more concerned about
programming.
52
4. Why Scheme?
53
Scheme?
The choice of Scheme for beginners is
natural. Permits programmers to focus on two key
elements of programming Programs as relations
between quantities Evaluating programs for
specific inputs. Using just this core language,
students can develop complete programs during the
first session with a teacher.
54
Scheme?
The choice of Scheme for beginners is
natural. Easily arranged as a tower of language
levels. As students learn more about programming
and the language, the teacher can expose students
to richer layers of the language, which allows
students to write more interesting and more
concise programs.
55
Scheme?
The choice of Scheme for beginners is
natural. The DrScheme programming environment
offers a truly interactive evaluator.
Definitions window where students define
programs Interactions window which acts like a
pocket calculator. You can enter expressions
into the interactions window, and DrScheme
determines their values (just like with
pocket-calculator arithmetic). You proceed from
there to calculations with structures, lists, and
trees--the kinds of data that computer programs
often manipulate.
56
Scheme?
The choice of Scheme for beginners is
natural. Use of an interactive evaluator with a
rich data language permits you to focus on
problem solving and program design activities.
An interactive mode of evaluation encourages
you to experiment in all kinds of ways and
satisfy your curiosity!
57
Scheme?
The choice of Scheme for beginners is
natural. The nature of Scheme requires
programmers to use good abstraction to solve a
problem. Many computer scientists consider
learning a functional language like Scheme to be
an essential part of learning the process of
abstraction.
58
But we wanted to use a cool real-world language!!!
59
Real World Use of Scheme
Scheme is used! -- NASAs 1999 Software of the
Year was a Lisp program controlling the Deep
Space 1 spacecraft. -- Powers parts of Yahoos
backend -- CAD software engines used Lisp --
Its used by professionally trained software
engineers
60
Why Use Scheme?
Core Material at other Universities Berkeley
Brown CMU Indiana MIT Rice
Utah Yale
Florida
61
Why Use Scheme?
Speed -- Rapid prototyping -- Development
time (more expensive than
execution time) is quicker
62
Why Use Scheme?
Speed (contd) -- Scheme (derived from Lisp)
has an execution time that well to many other
fast languages We have repeated Prechelt's
study using Lisp as the implementation language.
Our results show that Lisp's performance is
comparable to or better than C in terms of
execution speed, with significantly lower
variability which translates into reduced project
risk. Furthermore, development time is
significantly lower and less variable than either
C or Java. NASA study of Lisp www-aig.jpl.n
asa.gov/public/home/gat/lisp-study.html
63
Why Use Scheme?
Cost Free!
Money allocated for an expensive C
compiler can now be squandered elsewhere.
64
  • SCHEME is a statically scoped and properly
    tail-recursive dialect of the LISP programming
    language invented by Guy Lewis Steele Jr. and
    Gerald Jay Sussman. It was designed to have an
    exceptionally clear and simple semantics and few
    different ways to form expressions. A wide
    variety of programming paradigms, including
    imperative, functional, and message passing
    style, find convenient expression in SCHEME.
  • SCHEME has lexical scoping, uniform evaluation
    rules, and uniform treatment of data types. It
    does not have the concept of pointers,
    uninitialized variables, specialized looping
    constructs, or explicit storage management. In
    SCHEME all data types are equal. What one can do
    to one data type, one can do to all data types.
    There are seven kinds of expressions constant,
    variable reference, procedure creation, procedure
    application, conditional, assignment, and
    sequence.
  • Numbers are especially interesting in that an
    integer is a rational and a real is a complex.
    SCHEME requires no looping constructs. Any
    function which calls itself in the "Tail"
    position is just a loop.
  • SCHEME has several important advantages it's
    elegantly simple in that regular structure and
    trivial syntax avoids "special case" confusion.
    It's expressiveness means that one spends little
    time trying to work around the language, it
    concentrates on what they want say rather than on
    how to say it. It supports a variety of styles
    (including OO) allows users to better match
    their solution style to the style of the problems
    to be solved.

Theres Much, Much More...
65
Scheme is cool
66
Finally
67
Ga Tech Experience
  • Monolingual programmers are not well equipped for
    the real world

68
Tough Nut to Crack
Java
Scheme
CS COMPUTATION
C
C
69
6. On to Scheme
)
70
6. On to Scheme
)))))))
71
6. On to Scheme
))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))) )))))))))))))))))))))))))))))))))))))))
))))))))))))))))))))) ))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))) )))))))))))))))))
))))))))))))))))))))))))))))))))))))))))))) ))))))
))))))))))))))))))))))))))))))))))))))))))))))))))
)))) )))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))) ))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))) )))))))))))))))))))))))
))))))))))))))))))))))))))))))))))))) ))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))) )
))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))) ))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))) )))))))))))))))))))))))))))))
))))))))))))))))))))))))))))))) ))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))) )))))))
))))))))))))))))))))))))))))))))))))))))))))))))))
)))
72
Installing Dr. Scheme
http//www.htdp.org
73
In cs1321, you will use the DrScheme program as
your editor and development environment
74
Definitions Window
Interactions Window
75
Execute buttons loads the Definitions into
the Interactions window
76
Saving your work
77
Important
Be sure to set the language level in Dr. Scheme
to Beginning Scheme, then click on "Execute
78
Perspective
We might categorize computer languages
procedural or imperative languages
-- BASIC, FORTRAN, C, Pascal -- Reflect
low-level focus of language -- Programmers must
create solutions that reflect the type of
hardware, restrictions and limits of the system
they are using
79
Perspective
Object-oriented programming
-- Java, C, Ada, Smalltalk -- Programmers must
think in terms of data and the behaviors
appropriate to that data -- Less design pressure
from the limits and requirements of the computer
system.
80
Perspective
Functional Programming
-- Lisp, Scheme -- Programs viewed as a
collection of procedures similar to mathematical
functions. -- Much easier to implement
81
Further Perspective
In addition to labels of functional, procedural,
and OO languages, we might also categorize
languages based on whether they are interpreted
or compiled (or even a hybrid). Interpreted
languages are evaluated one step at a time, with
values and variables being determined dynamically
at run time. Compiled languages are assembled
into memory, with address locations and offsets
precalculated, and then crafted into an
executable program.
82
The Big Picture
Paradigm
Object Oriented
Imperative/ Procedural
Functional
Other
Interpreted
Scheme Lisp
Smalltalk
Basic
Prolog
Compiled
Fortran C, Ada
C
Hybrid
Java
83
So
  • Scheme can execute a single line of code as soon
    as it is typed into the interactions window
  • ( 2 2)
  • 4

84
Simple Evaluation
Lets start with something simple. Suppose we
wanted to solve the following expression 3
4 5 This looks easy, but what might go
wrong?
85
Simple Evaluation
It might be that by mistake someone could read
this as 7 5 by adding before multiplying. A
clear mistake. 3 4 5 How can we
be sure nobody makes this mistake?
86
Simple Evaluation
We can add unnecessary but helpful parentheses
around part of the expression. 3 (4
5) We all know that parenthetical expressions
are evaluated first So this will make it
totally clear.
87
Simple Evaluation
We can go further, and add parentheses around the
entire expression. (3 (4
5)) This doesnt change the meaning of our
original short notation. It makes things
explicit.
88
Did You Know?
As it turns out, the choice of representing
operators (, ) between the operands (numbers)
is completely arbitrary. (1 2)
Convention could be to write this as (
1 2) or even 1 2
Assuming binary operands, because of the way
postfix notation works, theres no need for a
set of parentheses
89
Prefix Notation
Scheme uses prefix notation, where the operation
call occurs first, followed by the data or
arguments.
( ltoperationgt ltdatagt )
Examples ( 2 2) ( 3 1) (/ 10 0) ( 7 (
4 5))
Which of these would cause problems if entered
into a simple calculator?
90
Prefix Notation
Self quiz Write the following expression in
prefix notation 1) subtract 3 from 5 2)
subtract 10 from the quantity 5 minus 8 3)
divide 3 into 42 4) sum the squares of both 3
and 4
91
Using DrScheme
(in class demonstration)
92
Using the Stepper
DrScheme includes a remarkable stepping tool that
lets you see the order of evaluation. Lets
evaluate some complex expressions with this
tool. (/ ( 2 4) (/ ( 6 4) ( 12
4))) (in class demonstration)
93
Grading Notice
At this point, you might want to consult the
following grade-action chart
Desired Grade in cs1321
Actions You Will Take This Evening
Try numerous examples in DrScheme AND the
Stepper Run DrScheme, but not the
Stepper Merely read notes/slides Blow it off
until somethings due
A
B
C
D/F
94
Onto Functions
We noted that Scheme is a functional
programming language. This implies well be
working with functions. Lets review what we
knew about functions in mathematics.
95
Functions
Lets start with a simple function from
mathematics f(x) x x This function
squares a number, thus f(2) 2 2
4 f(5) 5 5 25

with me?
96
Functions
Recall that a function maps a set of values,
called the domain, onto a set of return
values, called the range, so that one of the
domain values is paired with one and only one of
the range values.
1
2
3
97
f(X)
f(X) XX
range
X
domain
98
Functional Vocabulary
Consider again
f(x) x x
99
Functional Vocabulary
f(x) x x f(2)
The function body is applied to the value 2
100
Observations
f(x) x x f(2) 4
Predictable, reproducible. Each time we apply
the function to the value 2, we get the same
result. Nothing changed about the parameter.
Each time we call f(2), we do not change the
value of 2. The function doesnt alter anything
in the world.
101
Observations
Composable. We can compose a function of other
functions. f(f(2)) The inner f(2) returns 4,
so the outer f() call is sent 4. The overall
expression returns 16 f(f(2)) ? f(4) ? 16
102
Basic Scheme
Having refreshed our memory of functions, lets
learn about a few basic Scheme data types.
103
Scheme
Elements
Integers, e.g. 4, -6, 0 Real Numbers, e.g.,
3.14159 Ratios, e.g., 1/2, 4/3 Symbols, e.g.,
x, y, foo, bar -- often used for the name of a
parameter, or a function name, or a variable
104
Scheme
Given what weve covered, how would you
categorize this

Is this a real number? An integer? A ratio?
Perhaps a symbol? Hmm
105
Scheme
Predefined symbols. As it turns out, there are
about 100 predefined symbols in scheme, including
the arithmetic operators
addition - subtraction
multiplication / division
106
Our First Example
Suppose we wished to use the predefined
function sqrt, which returns the square root of
a parameter. In the interpreter of DrScheme, you
might be tempted to type sqrt(4) Since were
used to expressions such as f(4) from
mathematics but Scheme uses a different style
107
Aha
As it turns out, in Scheme, you invoke functions
like so (sqrt 4) and not sqrt(4) Its
the same amount of typing the parens are in
different locations to clarify. (A more complex
example will show the merit of this.)
108
What if...
When we next type in (sqrt (sqrt 4)) we
see i1.4142135623730951 That looks like the
right value, but whats with the i in front?
This is schemes way of indicating an
approximation.
109
So let's write a Scheme function!
Remember square f(x) x x We start by naming
the function (define (sq x) and continue by
adding the body (define (sq x) ( x x)) And
we test (sq 3) 9
110
Remember Pythagoras?
111
And more...
c
b
a
c2 a2 b2 c sqrt(a2 b2)
112
And more...
(sqrt ( ( 3 3) ( 4 4))) 5
c
3
4
113
Recall
We use prefix notation, instead of infix ( 3
3) rather than ( 3 3) because scheme
expects the name of the function first (after the
opening paren). "" is a function that multiplies
numbers together
114
Repetition
(sqrt ( ( 3 3) ( 4 4)))
(sqrt ( ( 2 2) ( 8 8)))
c
2
8
If we had many triangles to work with, typing the
same expression over and over gets tedious.
115
A Fix
In mathematics, wed recognize the function at
play hypotenuse(a, b) sqrt ( aa bb
) Remember what that "" sign meant? It binds
the body to the function/parameter list. In
Scheme, we can do this binding by defining a
function. It begins with ( define
116
The Fix Is In
At this point, the Scheme interpreter would
expect you to name the function you hope to
define. We need to come up with a symbol
name. The symbol hypotenuse is
good (define (hypotenuse side1 side2) Here
side1 and side2 are "parameters" or "arguments
basically placeholders for the side lengths
117
Still More
At this point, weve have a symbol for the name
of the function and the names of the parameters
to the function.
(define (hypotenuse side1 side2)
The body of the function is easy to figure out
(define (hypotenuse side1 side2) (sqrt ( (
side1 side1) ( side2 side2))))
118
Style matters!(not to scheme, but for good
programming)
As a matter of style, we will use indents and
returns to better format the statement
(define (hypotenuse side1 side2)
(sqrt ( (
side1 side1) (
side2 side2)))) As opposed to (define
(hypotenuse side1 side2) (sqrt ( ( side1
side1) ( side2 side2))))
119
Can we abstract even further?
Note that we need to square the values and we
defined a square function sq earlier.
(define (hypotenuse side1 side2) (sqrt (
( side1 side1) ( side2 side2))))
(define (hypotenuse side1 side2) (sqrt (
(sq side1) (sq side2))))
120
Questions?
121
Reading Assignment
  • Read Chapter 1 from How to Design Programs!!

122
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com