Transcript Scheme

Functional Programming
Language
Scheme Languag
1
Functional Programming
Language
A program is: Composition of
operations on data
 Characteristics (in pure form):
– Name values, not memory
locations
– Value binding through
parameter passing
– Recursion rather than iteration
2
 Key operations: Function

Example: Functional
Language
Haskell
 Erlang
 ML
 Scheme
 Ruby
 Pure
 Needle

Clean
J
 Pizza
 Mercur
y
 Goo
…

3
Scheme Language Tutorial


Book: http://www.schemers.org/
Contents: http://www.htdp.org/200309-26/Book/curriculum-Z-H1.html#node_toc_start
4
Contents
Section 1: Getting Started
Section 2: Numbers and Expressions
Section 3: Programs are Function Plus
Name Definitions
Section 4: Conditional Expressions
Section 5: Symbolic Information
Section 6: Structures
Section 7: The Varieties of Data
Section 8: Syntax and Semantics
5
Section 9: Lists
Section 1: Getting
Started
 Racket is a Scheme

Download DrRacket (http://racketlang.org/download/)

Choose Language: The Racket
Language
Definition window
Interaction window
6
Section 2: Numbers and
Expressions
2.1 Numbers and Arithmetic
2.2 Names and Programs
2.3 Designing Programs
7
2.1 Numbers and Arithmetic
Numbers
Scheme numbers are printed in the
standard way, but spaces are not
allowed within a number.
 Numbers come in many different
flavors: positive and negative
integers, fractions (also known as
rationals), and reals are the most
widely known classes of numbers. 8

Numbers
integer negative integer
fractions
5

-5
17/3
1.4142165
real
Thus, -5, with no space between
the – and 5, is the negative
integer -5. But – 5, with space in
between, is two separate
expressions: the minus operator
and the number 5.
9
Numbers
Scheme computes with EXACT
integers and rationals as long as
we use primitive operations that
produce exact results.
– Thus, it displays the result of (/
7 14) as 1/2.
 The square root of 2 is not a
rational but a real number,
Scheme uses an INEXACT
NUMBER:

10
Arithmetic

Scheme expression has the shape
(operation A ... B)
The simplest of computers,
Scheme permits programmers to
add, subtract, multiply, and divide
numbers:
(+ 5 5)
(+ -5 5)
(- 5 5)
(* 3
4)
(/ 8 12)
 All arithmetic expressions are
11

Exercise 2.1: Numbers and
Arithmetic
1. (+ 5 6 3)
2. (/ 6 -9 -1)
3. (+ 2 (* 3 4))
4. (* (+ 2 2) (-9 4))
5. (+ (- 1 1) (* 2 5) (+ 4 8))
12
2.1 Numbers and Arithmetic
As in arithmetic or algebra, we
can nest expressions:
(* (+ 2 2) (/ (* (+ 3 5) (/ 30
10)) 2))
 It first reduces the innermost
parenthesized expressions to
numbers, then the next layer, and
so on:

13
Debuging


DrRacket provides a Debug tool
that shows how a complex
expression is evaluated, stepby-step.
To use the Debug tool, put the
expression(s) you want to
evaluate into DrRacket's top
area, the definitions window,
then click the Debug button
14
Stepper
For example, given the
expression
(* (+ 2 2) (/ (* (+ 3 5) (/ 30
10)) 2))

= (* 4 (/ (* 8 3) 2))
= (* 4 (/ 24 2))
= (* 4 12)
= 48
15
Exercise 2.2: Numbers and
Arithmetic
1. (- 2 (+ 10 9))
2. (/ (* (- 2 1) (+ 5 -9)) 6)
3. (* (+ 5 5) (+ (- (* 2 3) (/ 12 6))
(* 10 3)))
16
Arithmetical Operations

Scheme not only provides simple
arithmetical operations but a whole
range of advanced mathematical
operations on numbers. Here are
five examples:
– (sqrt A) computes (A)1/2
– (expt A B) computes AB;
– (remainder A B) computes the
remainder of the integer division
A/B;
17
2.2 Names and Programs
The Shape of Definitions
 The location of parentheses in a
definition must match the
start definition
following
pattern exactly:
start program and input names
(define (f arg ...)end definition
end input names
expression)
18
Example 2.1: Names and
Programs
(n / 3) + 2
 Here is the program that
corresponds to the above
expression:
(define (f n) (+ (/ n 3) 2))
 First determine the result of the
expression at n = 2, n = 5, and n =
9 by hand
19
Example 2.2: Names and
Programs

20
Example 2.2: Names and
Programs
A program is such a rule.
 We would express the rule for
computing the area of a disk as
follows:
(define (area-of-disk r) (* 3.14
(* r r)))
 area-of-disk is a rule, that it
consumes a single INPUT, called r,
and that the result, or OUTPUT, is 21
going to be (* 3.14 (* r r)) once we

Example 2.2: Names and
Programs
We may write expressions whose
operation is area-of-disk followed
by a number:
(area-of-disk 5)
 We also say that we APPLY areaof-disk to 5.
(area-of-disk 5)
= (* 3.14 (* 5 5))
= (* 3.14 25)
22

Names
Unlike most programming languages,
program and names in Scheme can
contain almost any character.
 For example, the following are legal
names:
a-b
in->ft
my-2ndprogram
positive?
!@$%^&/*~program
 Avoid the following characters in
names:

23
Names
Even *3.14 is a name. Suppose that you
forget the space between * and 3.14
when defining area-of-disk:
(define (area-of-disk r) (*3.14 (* r
r)))
 When you test this definition of areaof-disk, DrRacket reports that the name
*3.14 is undefined.
 Names are case-sensitive, i.e., an uppercase letter is treated different from a
24
lower-case letter in a name.

Names
Many programs consume more than
one input.
 Say we wish to define a program that
computes the area of a ring.
 The area of the ring is that of the outer
disk minus the area of the inner disk.
(define (area-of-ring outer inner)
(- (area-of-disk outer) (area-ofdisk inner)))
 When we wish to use area-of-ring, we 25

Exercise 2.3: Names and
Programs
Define the program dollar->euro,
which consumes a number of
dollars and produces the euro
equivalent.
 1 dollar is 1.17 euros

26
Exercise 2.4: Names and
Programs
Also formulate the following three
expressions as programs:
1. n2 + 10
2. (1/2) · n2 + 20
3. 2 - (1/n)
 Determine their results for n = 2
and n = 9 by hand and with
DrRacket.

27
2.3 Designing Programs
DrRacket ignores anything
between the first semi-colon in a
line and the end of the line.
;; program : input1 input2 ... ->
output
 DrRacket ignores block comments
starting with #| and ending with |#
#| This is a
28
multi-line

2.3 Designing Programs

The design recipe: A complete
example
;; Contract: area-of-disk : number ->
number
;; Purpose: to compute the area of a
disk of radius r.
;; Example: (area-of-disk 5) should
produce 78.5
;; Definition: [refines the header]
(define (area-of-disk r) (* 3.14 (* r r)))29
Section 3: Programs
are Function Plus
Name Definitions
3.1 Composing Functions
3.2 Name Definitions
3.3 Finger Exercises on
Composing Functions
30
3.1 Composing Functions
The use of auxiliary functions
makes the design process
manageable and renders programs
readable. Compare the following
two(area-of-ring
versions of area-of-ring:
(define
(define (area-of-ring

outer inner)
(- (area-of-disk outer)
(area-of-disk inner)))
outer inner)
(- (* 3.14 (* outer outer))
(* 3.14 (* inner inner))))
31
3.2 Name Definitions

32
3.2 Name Definitions
The order for name definitions is
significant.
(define RADIUS 5)
(define DIAMETER (* 2 RADIUS))
is fine, but
(define DIAMETER (* 2 RADIUS))
(define RADIUS 5)
DrRacket does not yet know the
33
definition of RADIUS when it tries to

Section 4: Conditional
Expressions
4.1 Booleans and Relations
4.2 Functions that Test
Conditions
4.3 Conditionals and
Conditional Functions
34
4.1 Booleans and Relations
In mathematics we talk of true
and false claims, which are
conditions.
x = y: ``x is equal to y'';
x > y: ``x is strictly greater than
y''.
 In addition to determining
whether an atomic claim holds in
a given situation.
35

4.1 Booleans and Relations



Like mathematics, Scheme has
''word'' for true is #t and the
''word'' for false is #f.
It can typically be expressed with
a RELATIONAL OPERATION, for
example, =, <, >, <=
Pattern of writing a left
parenthesis, followed by the
operator, its arguments, and a
36
right parenthesis:
4.1 Booleans and Relations




Like the arithmetic operations, the
test operations can take more
than two arguments.
(< 1 2 3)
is legal Scheme notation for “1 <
2 and 2 < 3”.
The and and or operators also
take any number of arguments.
The not operation always takes 37
4.1 Booleans and Relations



Example, Consider the following
compound condition:
(and (= 5 5) (< 5 6))
It consists of two atomic claims: (=
5 5) and (< 5 6).
Both evaluate to true.
= (and true true)
= true
38
Exercise 4.1: Booleans and
Relations

What are the results of the
following Scheme conditions?
1. (> 7/2 3)
2. (= (* 2 2) 4)
3. (and (> 4 3) (<= 10 100))
4. (or (> 4 3) (= 10 100))
5. (not (= 2 3))
39
4.2 Functions that Test
Conditions
Here is a simple function that
tests some condition about a
number:
;; is-5? : number -> boolean
;; to determine whether n is
equal to 5
(define (is-5? n) (= n 5))

40
4.2 Functions that Test
Conditions
Here is a slightly more interesting
function with a boolean output:
;; is-between-5-6? : number ->
boolean
;; to determine whether n is
between 5 and 6 (exclusive)
(define (is-between-5-6? n) (and
(< 5 n) (< n 6)))

41
4.2 Functions that Test
Conditions
The following third function from
numbers to boolean values represents
the most complicated form of interval:
;; is-between-5-6-or-over-10? :
number -> boolean
;; to determine whether n is between
5 and 6
;; or larger than or equal to 10
(define (is-between-5-6-or-over-10?
n)

42
Exercise 4.2: Functions that
Test Conditions

Translate the following five
intervals on the real line into
Scheme functions that accept a
number and return true if the
number is in the interval and false
if it is outside:
1. the interval (3,7]:
2. the interval [3,7]:
43
Exercise 4.2: Functions that
Test Conditions
3. the interval [3,9):
4. the union of (1,3) and (9,11):
5. the range of numbers outside of
[1,3]
44
Exercise 4.3: Functions that
Test Conditions
Translate the following three Scheme
functions into intervals on the line of
reals:
1. (define (in-interval-1? x)
(and (> -3 x) (< x 0)))
2. (define (in-interval-2? x)
(or (< x 1) (> x 2)))
3. (define (in-interval-3? x)
(not (and (>= 1 x) (<= 45x
4.2 Functions that Test
Conditions
Mathematical equations in one
name are claims about an
unknown number.
 For example, the quadratic
equation
;; equation1 : number ->
(equation1 boolean (x2 + 2) (x + 1) = 0

;; to determine whether x is a
solution
;; for (x2 + 2) (x + 1) = 0
(define (equation1 x)
(equation1 +
46
4.3 Conditionals and
Conditional Functions


In many time we interest function
must determine which of several
conditions holds for the input.
We say that the function is a
CONDITIONAL FUNCTION, and we
formulate the definition of such
functions using CONDITIONAL
EXPRESSIONS.
47
4.3 Conditionals and
Conditional Functions
 The general shape of a
conditional expression is

Example:
(cond
[(<= n 50) "D"]
[(<= n 60) "C"]
[(<= n 70) "B"]
[(> n 80) "A"])
(cond
[(<= n 50) "D"]
[(<= n 60) "C"]
[(<= n 70) "B"]
[else "A"])
48
Exercise 4.4: Conditionals and
Conditional Functions

Suppose the bank pays
4% for deposits of up to $1,000
(inclusive),
4.5% for deposits of up to $5,000
(inclusive),
and
5% for
deposits of
;; interest-rate
: number
-> number
more
$5,000.
;; to than
determine
the interest rate for
the given amount
(define (interest-rate amount)
(cond
[(<= amount 1000)
0.040]
[(<= amount 5000)
49
Section 5: Symbolic
Information
50
5. Symbolic Information


Scheme supports several ways to
express symbolic information:
symbols, strings, (keyboard)
characters, and images.
A symbol is a sequence of
keyboard characters preceded by
a single forward quotation mark:
'a 'dog
'chocolate 'cat!
'two^3 'and%so%on?
51
5. Symbolic Information

Scheme provides only one basic
operation on symbols: symbol=?,
a comparison operation.
1. (symbol=? 'Hello 'Hello)
= true
2. (symbol=? 'Hello 'Howdy)
= false
52
5. Symbolic Information

Symbols were first introduced to
computing by researchers in artificial
intelligence who wanted to design
functions that could have
conversations with people.
(define (reply s)
(cond
[(symbol=? s 'GoodMorning) 'Hi]
[(symbol=? s 'HowAreYou?) 'Fine]
[(symbol=? s 'GoodAfternoon)
'INeedANap]
53
5. Symbolic Information
A string is a second form of
symbolic data. Like a symbol, a
string consists of a sequence of
keyboard characters, but they are
enclosed in string quotes:
"the dog"
"isn't"
"made of"
"chocolate" "two^3"
"and so on?“
54
 The operation needed is string=?,

Section 6: Structures
6.1 Structure Definitions
55
6.1 Structures




Scheme provides many different
methods for compounding data.
DrRacket's teachpacks represent
pixels with posn structures.
The posn structure type that is
also provided by
(require lang/posn)
It has an x coordinate, and it has a
y coordinate, which tells us where56
the pixel is located.
6.1 Structures
Here is DrRacket's definition of posn:
(define-struct posn (x y))
 When DrRacket evaluates this
structure definition, it creates three
operations for us,
1. make-posn, the CONSTRUCTOR,
which creates posn structures;
2. posn-x, a SELECTOR, which
extracts the x coordinate;
3. posn-y, also a selector, which
57

6.1 Structures



A posn structure combines two
numbers.
The posn structure is built into
DrRacket's teaching languages,
including make-posn, posn-x, and
posn-y.
For example,
1. (make-posn 3 4)
2. (posn-x (make-posn 7 0))
58
6.1 Structures


In general, we know from geometry
that the distance from the origin to a
position with coordinates x and y is
distance
Now consider a function that
computes how far some pixel is from
the
origin. The contract, header, and
;; distance-to-0 : posn -> number
purpose
statement
easyto to
;; to compute
the distance are
of a-posn
the origin
(define (distance-to-0 a-posn) ...)
formulate:
59
Exercise 6.1: Structures
4))
6))
1. (distance-to-0 (make-posn 3
2. (distance-to-0 (make-posn 8
3. (distance-to-0 (make-posn 5
12))
4. (distance-to-0 (make-posn (* 2
3) (* 2 4)))
5. (distance-to-0 (make-posn 1260
6.2 Structure Definitions


A STRUCTURE DEFINITION is, as
the term says, a new form of
definition.
In general, the names of these
new operations are created by
prefixing the name of the
structure with ``make-'' and by
postfixing the name with all the
field names.
61
6.2 Structure Definitions
Now consider the following
example:
(define-struct entry (name
zip phone))
 Each entry combines three values.
 We also say that each entry
structure has three fields: name,
zip, and phone.
 The constructor make-entry
62

6.2 Structure Definitions
The define-struct definition of
entry also introduces new
selectors:
entry-name entry-zip entryphone
 Here is how we can use the first
one:

(entry-name (make-entry 'PeterLee
15270 '606-7771))
63
6.2 Structure Definitions
If we give the structure a name,
(define phonebook (make-entry
'PeterLee 15270 '606-7771))
 then we can use the selectors in
the Interactions window to extract
the data from the three fields:
(entry-name phonebook)
(entry-zip phonebook)
(entry-phone phonebook)
64

6.2 Structure Definitions

Here is one final example, a structure
for representing rock stars:
(define-struct star (last first instrument sales))


It defines the class of star structures,
each of which has four fields.
Accordingly, we get five new primitive
operations:
make-star, star-last, star-first, star-instrument, star-sales
The first is for constructing star
structures; the others are selector
65
6.2 Structure Definitions

To create a star structure, we apply
make-star to three symbols and a
positive integer:
(make-star 'Friedman 'Dan 'ukelele 19004)
(make-star 'Talcott 'Carolyn 'banjo 80000)
(make-star 'Harper 'Robert 'bagpipe 27860)
66
Section 7: The Varieties of
Data
7.1 Mixing and Distinguishing
Data
67





Section 7: The Varieties of
Data
Our functions have always processed
subclasses of four different kinds of
data:
numbers: representations of numeric
information;
booleans: truth and falsity;
symbols: representations of symbolic
information; and
structures: representations of
compounds of information.
68





7.1 Mixing and
Distinguishing Data
Scheme provides PREDICATES, which
are operations that recognize a
particular form of data.
number?, produces true if the value is
a number.
boolean?, produces true if the value is
a boolean value.
symbol?, produces true if the value is a
symbol.
struct?, produces true if the value is a
69
structure.

7.1 Mixing and
Distinguishing Data
Example
1. (number? (+ 12 10))
2. (boolean? false)
3. (symbol? 'cat)
4. (number? (make-posn 2 3))
70
7.1 Mixing and
Distinguishing Data
For each structure definition,
(define-struct posn (x y))
(define-struct star (last first instrument
sales))
(define-struct airplane (kind max-speed
max-load price))

Then, Scheme also knows the following
three predicates:
– posn?, produces true if the value is a posn
structure.
– star?, produces true if the value is a star
71
structure.


7.1 Mixing and
Distinguishing Data
Example
1. (posn? 23)
2. (posn? (make-posn 23 3))
3. (star? (make-star 1 2 3 4))
4. (star? '123)
72

7.1 Mixing and
Distinguishing Data
The two conditions match the two
possible inputs of the new distanceto-0 function.
(define (distance-to-0 a-pixel)
(cond
[(number? a-pixel) a-pixel]
[(posn? a-pixel) (sqrt (+ (sqr
(posn-x a-pixel))
(sqr (posn-y apixel))))]))
73
7.1 Mixing and
Distinguishing Data

The function perimeter, which
computes the perimeter of a
shape.
(define-struct square (nw-corner
length))
(define-struct circle (center radius))
(define (perimeter a-shape)
(cond
[(square? a-shape) (* (square74
length a-shape) 4)]
Section 8: Syntax and
Semantics
8.1 The Scheme Vocabulary
8.2 The Scheme Grammar
75
8.1 The Scheme Vocabulary



Names: names of functions and
values.
Constants: boolean, symbolic, and
numeric constants.
Primitive operations: basic
76
8.2 The Scheme Grammar

Beginning Student Scheme: The
full grammar
77
8.2 The Scheme Grammar

Exercise 8.1 Why are the
sentences
1. (3 + 4)
2. (+ 1 (not x))
3. (define (f x) x)
4. (define (f x) y)
5. (define (f x y) 3)
6. (define (f 'x) x)
7. (define (f x y z) (x))
8. (define (f) 10)
syntactically legal expressions?
78
8.2 The Scheme Grammar

Exercise 8.2 Why are the
sentences
(cond
[false 1]
[true (+ 1 1)]
[else 3])
syntactically legal expressions?
79
8.2 The Scheme Grammar

Exercise 8.3 Why are the
sentences
1. (define PRICE 5)
2. (define SALES-TAX (* .08
PRICE))
3. (define TOTAL (+ PRICE SALESTAX))
syntactically legal expressions?
80
8.2 The Scheme Grammar

Exercise 8.4 Why are the
sentences
1. (define-struct point (x y z))
2. (define-struct (point x y z))
3. (define-struct point x y z)
syntactically legal expressions?
81
Section 9: Lists
82
9. Lists

When we form a list, we always
start out with the empty list. In
Scheme, represents the empty list.
empty

or
'()
We can construct a longer list
with the operation list or cons.
3 4 5)
'(1 2 3 4 5)
or
(list 1 2
or (cons 1 '(2 3 4 5))
83
9. Lists
List creates a list whose elements are
the arguments:
84
9. Lists



In this example, we constructed a
list from the empty list and the
symbol 'Mercury.
(cons 'Mercury empty)
The box for cons has two fields:
first and rest.
In this specific example the first
85
9. Lists


(cons 'Venus (cons 'Mercury
empty))
(cons 'Earth (cons 'Venus (cons
'Mercury empty)))
86
9. Lists

From here, we can construct a
longer list with the operation
cons. Here is a simple example:
= (cons 1 '(2 3 4 5))
= (cons 1 (cons 2 '(3 4 5)))
= (cons 1 (cons 2 (cons 3 '(4 5))))
= (cons 1 (cons 2 (cons 3 (cons 4
'(5)))
= (cons 1 (cons 2 (cons 3 (cons 4
87
9. Lists

In general a list does not have to
contain values of one kind, but may
contain arbitrary values:
(cons 'RobbyRound (cons 3 (cons
true empty)))

Here the first item is a symbol, the
second one is a number, and the last
one a boolean.
88
9. Lists
Scheme implements operations for
extracting the fields from a
constructed list: first and rest.
 The first or car operation extracts the
item that we used to construct a list.
 The rest or cdr operation extracts the
list field.
Example: Now suppose we define a list of
numbers.
(define num (cons 1 (cons 2 (cons 3
89
(cons 4 (cons 5 '()))))))

9. Exercise 9.1: Lists
Let num be the list
(define num (cons 10 (cons 20 (cons
5 empty))))
 What are the values of the following
expressions?
1. (rest num)
2. (first (rest num))
3. (rest (rest num))
4. (first (rest (rest num)))
90
5. (rest (rest (rest num)))

9. Lists
If you want define list
((4 5 6) (a b c d) (2) 8)
You can define this list by list operator
– (define list1 '((4 5 6) (a b c d) (2)
8))
– (define list2 (list '(4 5 6) '(a b c d)
'(2) 8))
– (define list3 (list (list 4 5 6) (list 'a
'b 'c 'd) (list 2) 8))

91
9. Lists


If you want create list
((1 2 3) (4 5))
You can create this list by list
operator
= '((1 2 3) (4 5))
= (list '(1 2 3) '(4 5))
= (list (list 1 2 3) (list 4 5))
92
9. Lists


If you want create list
((1 2 3) (4 5))
You can create this list by cons operator
= (cons '(1 2 3) (cons '(4 5) empty))
= (cons (cons 1 '(2 3)) (cons (cons 4 '(5))
empty))
= (cons (cons 1 (cons 2 '(3))) (cons (cons
4 (cons 5 empty)) empty))
= (cons (cons 1 (cons 2 (cons 3 empty)))
(cons (cons 4 (cons 5 '())) '()))
93
Exercise 9.2: Lists



If you want create list
(5 9 (7 3 (8 4) 1))
How can create this list by list
operator?
How can create this list by cons
operator?
94
9. Lists

In the following example we take
advantage of structured lists to
produce a translation dictionary.
(define (translate wd)
(lookup wd '((window fenetre) (book livre)
(computer ordinateur)
(house maison) (closed ferme) (pate pate)
(liver foie)
(faith foi) (weekend (fin de semaine))
((practical joke) attrape) (pal copain))))
(define (lookup wd dictionary)
(cond [(empty? dictionary) 'The-word-is-not95
found]
9. Lists

In our dictionary
'((window fenetre)
(book livre)
(computer ordinateur)
(house maison)
(closed ferme)
(pate pate)
(liver foie)
(faith foi)
((practical joke) attrape)
(weekend (fin de semaine))
(pal copain))
96
9. Lists

Then, in the Interactions window,
evaluate the expressions:
> (translate 'computer)
'ordinateur
> (translate '(practical joke))
'attrape
> (translate 'recursion)
'The-word-is-not-found
97
Homework 1: Names and
Programs
Define the program triangle.
 It consumes the length of a
triangle's side and the
perpendicular height.
 The program produces the area of
the triangle.
 Formula for computing the area of
a triangle is
area of a triangle = 1/2 * base * 98

Homework 2 : Names and
Programs
Define the program convert3. It
consumes three digits, starting with
the least significant digit, followed
by the next most significant one,
and so on. The program produces
the corresponding number. For
example, the expected value of
(convert3 1 2 3)
 is 321. Use an algebra book to find
99
out how such a conversion works.

Homework 3: Composing
Functions
Convert English measurements to
metric ones
 Here is a table that shows the six
English
major units
of length Metric
1 inch
= English
2.54 cm
measurements
of the
1 foot
= 12
system:

1 yard
= 3 ft.
1 rod= 5.5 yd.
1 furlong = 40 rd.
1 mile
= 8 fl.
100
Develop the functions inches->cm,
feet->inches, yards->feet, rods>yards, furlongs->rods, and miles>furlongs.
 Then develop the functions feet>cm, yards->cm, rods->inches, and
miles->feet.

101
Homework 4: Composing
Functions
Develop volume-cylinder.
 It consumes the radius of a
cylinder's base disk and its height;
it computes the volume of the
cylinder.

102
Homework 5: Composing
Functions
Develop area-cylinder.
 The program consumes the radius
of the cylinder's base disk and its
height. Its result is the surface area
of the cylinder.

103
Homework 6: Structure and
Condition




ให้สร ้าง struct cylinder ประกอบไปด้วย
radius และ height
กาหนดฟั งก ์ช ัน volume-cylinder และ
่ านวณหาปริมาตรและ
area-cylinder เพือค
้ ของทรงกระบอก
่
พืนที
่
กาหนดฟั งก ์ช ัน cal-cylinder เพือตรวจสอบ
เงื่อนไขว่าต้องการคานวณหาปริมาตร หรือ
้ ่ จากนันเรี
้
่ องการ
พืนที
ยกใช้ฟังก ์คานวณทีต้
รู ปแบบการเรียกใช้ฟังก ์ช ัน เช่น
(cal-cylinder (make-cylinder 10 50)104
Homework 7: List

Define a function to compute an
summation of all the data in the
given list.
1. (sum-list1 '(1 2 3 4 5))
=15
2. (sum-list2 '((Jenny 50) (Robert 20)
(George 45) (John 27)))
=142
105
Homework




้ อไฟล
่
่
การตังชื
์ เช่น Homework 1 ชือไฟล
์
hw1.rkt
ในโปรแกรมให้ใส่ comment บอกรายละเอียด
ด้วย
;; ID…….first name……last name……
;; Contract: ………
;; Purpose: ………
;; Example: ………
;; Definition: ………
;; Tests: ………
้ั อเป็
่ นชือ
่ folder รหัสนิ สต
Zip files โดยตงชื
ิ
้ วข้อเมล ์ว่า
ส่ง [email protected] ตังหั
106