functional programming language 1. a program is: composition of operations on data characteristics...
TRANSCRIPT
![Page 1: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/1.jpg)
FunctionalProgramming Language
1
![Page 2: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/2.jpg)
FunctionalProgramming 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 Key operations: Function
Assignment, Function Application, and Function Abstraction– Based on the Lambda Calculus
2
![Page 3: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/3.jpg)
Example: Functional Language Haskell Erlang ML Scheme Ruby Pure Needle
Clean J Pizza Mercur
y Goo …
3
![Page 4: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/4.jpg)
Scheme Language Book: http://www.schemers.org/
4
![Page 5: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/5.jpg)
Section 1: Getting Started
5
![Page 6: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/6.jpg)
Download DrRacket Racket is a Scheme Download DrRackethttp://www.informatics.buu.ac.th/~janya/886340_PPL/Programming%20Languages/Scheme/racket-6.1-i386-win32.exe
6
![Page 7: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/7.jpg)
The Racket Language Choose Language: The Racket
Language
7
Definition window
Interaction window
![Page 8: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/8.jpg)
Section 2: Numbers and Expressions
2.1 Numbers and Arithmetic2.2 Variables and Programs2.3 Errors2.4 Designing Programs
8
![Page 9: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/9.jpg)
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.
–There is no limit on the size of exact numbers in Scheme 9
![Page 10: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/10.jpg)
2.1 Numbers and Arithmetic
5 -5 23/ 173/ 14142165237321
The first is an integer, the second one a negative integer, the next two are fractions, and the last one is an inexact representation of a real number
10
![Page 11: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/11.jpg)
2.1 Numbers and Arithmetic
Numbers–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.
–Similarly, 3/2 is the fraction 3/2, while 3 / 2 is three separate expressions: the number 3, the division operator, and the number 2.
11
![Page 12: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/12.jpg)
2.1 Numbers and Arithmetic
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:– (sqrt 2)= 1.4142135623730951
12
![Page 13: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/13.jpg)
2.1 Numbers and Arithmetic Arithmetic Scheme expression has the
shape(operation A ... B)
Whenever A ... B are numbers, the expression can be evaluated; otherwise, A ... B are evaluated first.
13
![Page 14: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/14.jpg)
2.1 Numbers and Arithmetic Like a pocket calculator, 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 parenthesized and mention the operation first; the numbers follow the operation and are separated by spaces.
14
![Page 15: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/15.jpg)
2.1 Numbers and Arithmetic Exercise 2.1.1
1. (+ 5 6 3)2. (/ 6 -9 -1)3. (+ 2 (* 3 4))4. (* (+ 2 2) (-9 4))5. (+ (- 1 1) (* 2 5) (+ 4 8))
15
![Page 16: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/16.jpg)
2.1 Numbers and Arithmetic As in arithmetic or algebra, we can
nest expressions:(* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2))
Scheme evaluates these expressions exactly as we do. It first reduces the innermost parenthesized expressions to numbers, then the next layer, and so on:
16
![Page 17: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/17.jpg)
2.1 Numbers and Arithmetic Stepper DrRacket provides a Step tool that
shows how a complex expression is evaluated, step-by-step.
To use the Step tool, put the expression(s) you want to evaluate into DrRacket's top area, the definitions window, then click the Step button
17
![Page 18: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/18.jpg)
2.1 Numbers and Arithmetic Stepper For example, given the
expression(* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2))= (* 4 (/ (* 8 3) 2))= (* 4 (/ 24 2))= (* 4 12)= 48
18
![Page 19: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/19.jpg)
2.1 Numbers and Arithmetic Exercise 2.1.2
1. (- 2 (+ 10 9))2. (/ (* (- 2 1) (+ 5 -9)) 6) 3. (* (+ 5 5) (+ (- (* 2 3) (/ 12 6)) (* 10 3)))
19
![Page 20: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/20.jpg)
2.1 Numbers and Arithmetic
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;– (log A) computes the natural logarithm of A;– (sin A) computes the sine of A radians.
20
![Page 21: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/21.jpg)
2.2 Variables and Programs
The Shape of Definitions The location of parentheses in a
definition must match the following pattern exactly:
(define (f arg ...) expression)
start definitionstart program and input names
end input namesend definition
21
![Page 22: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/22.jpg)
2.2 Variables and Programs
In algebra we learn to formulate dependencies between quantities using VARIABLE EXPRESSIONS.
A variable is a placeholder that stands for an unknown quantity.
Using Scheme, we can formulate such an expression as a program and use the program as many times as necessary.
22
![Page 23: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/23.jpg)
2.2 Variables and Programs
For example,(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
23
![Page 24: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/24.jpg)
2.2 Variables and Programs
24
![Page 25: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/25.jpg)
2.2 Variables and Programs
A program is such a rule. It is a rule that tells us and the
computer how to produce data from some other data.
Large programs consist of many small programs and combine them in some manner.
A good name for our sample expression is area-of-disk.
25
![Page 26: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/26.jpg)
2.2 Variables and Programs
Using this name, 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 going to be (* 3.14 (* r r)) once we know what number r represents.
26
![Page 27: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/27.jpg)
2.2 Variables 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 area-
of-disk to 5.(area-of-disk 5)= (* 3.14 (* 5 5))= (* 3.14 25)= 78.5 27
![Page 28: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/28.jpg)
2.2 Variables and Programs
Variable Names Unlike most programming
languages, program and variable names in Scheme can contain almost any character.
For example, the following are legal names:
a-b in->ft my-2nd-program positive? !@$%^&/*~program
28
![Page 29: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/29.jpg)
2.2 Variables and Programs
Variable Names The name a-b does not mean a minus b;
it's just a name. 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 area-of-
disk, DrRacket reports that the name *3.14 is undefined.
29
![Page 30: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/30.jpg)
2.2 Variables and Programs
Variable Names Avoid the following characters in
variable names:" ' ` , # ; | \
Since quotes cannot be used in names, do not attempt to use x' as ``x prime.'' The pairs of bracket characters [ ] and { } are also special: they act just like parenthesis pairs ( ).
30
![Page 31: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/31.jpg)
2.2 Variables and Programs
Variable Names Names are case-sensitive, i.e.,
an upper-case letter is treated different from a lower-case letter in a name.
For example, the name area-of-disk is different from the name Area-Of-Disk.
31
![Page 32: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/32.jpg)
2.2 Variables and Programs
Variable 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, which means that the program requires two unknown quantities: the outer and the inner radii. Let us call these unknown numbers outer and inner. 32
![Page 33: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/33.jpg)
2.2 Variables and Programs
Variable Names Then the program that computes
the area of a ring is defined as follows:(define (area-of-ring outer inner)
(- (area-of-disk outer) (area-of-disk inner)))
33
![Page 34: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/34.jpg)
2.2 Variables and Programs
Variable Names That the program accepts two
inputs, called outer and inner, and that the result is going to be the difference between (area-of-disk outer) and (area-of-disk inner).
When we wish to use area-of-ring, we must supply two inputs:
(area-of-ring 5 3) 34
![Page 35: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/35.jpg)
2.2 Variables and Programs
Variable Names(area-of-ring 5 3) = (- (area-of-disk 5) (area-of-disk 3))= (- (* 3.14 (* 5 5)) (* 3.14 (* 3 3)))= ...
35
![Page 36: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/36.jpg)
2.2 Variables and Programs
Exercise 2.2.1 Define the program dollar->euro,
which consumes a number of dollars and produces the euro equivalent.
1 dollar is 1.17 euros
36
![Page 37: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/37.jpg)
2.2 Variables and Programs
Exercise 2.2.2 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 isarea of a triangle = 1/2 * base * height
37
![Page 38: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/38.jpg)
2.2 Variables and Programs
Exercise 2.2.3 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 out
how such a conversion works.38
![Page 39: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/39.jpg)
2.2 Variables and Programs
Exercise 2.2.4 Also formulate the following three
expressions as programs:1. n2 + 102. (1/2) · n2 + 203. 2 - (1/n)
Determine their results for n = 2 and n = 9 by hand and with DrRacket.
39
![Page 40: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/40.jpg)
2.3 Errors When an error occurs, DrRacket
highlights the source of the error, whether it is in the definitions window or the interactions window.
For example, typing (/ 1 0) in the interactions window causes that expression to be highlighted (and also produces the error message ``/: division by zero'').
40
![Page 41: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/41.jpg)
2.3 Errors For example, The first one contains an extra pair of
parentheses around the variable x, which is not a compound expression
(define (P x) (+ (x) 10)) The second contains two atomic
expressions, x and 10, instead of one.(define (Q x) x 10)
41
![Page 42: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/42.jpg)
2.3 Errors Exercise 2.3.1 Evaluate the
following sentences in DrRacket, one at a time:
1. (+ (10) 20) 5. (+ 10)2. (10 + 20) 6. (* 10)3. (10 20) 7. (- 10)4. (+ +) 8. (/ 10)
42
![Page 43: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/43.jpg)
2.3 Errors Exercise 2.3.2 Enter the
following sentences, one by one, into DrRacket's Definitions window and click Run:
1. (define (f 1) (+ x 10))2. (define (g x) + x 10)3. (define h(x) (+ x 10))
43
![Page 44: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/44.jpg)
2.3 Errors Exercise 2.3.3 Evaluate the
following grammatically legal Scheme expressions in DrRacket's Interactions window:
1. (+ 5 (/ 1 0))2. (sin 10 20)3. (somef 10)
44
![Page 45: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/45.jpg)
2.3 Errors Exercise 2.3.4 Enter the following
grammatically legal Scheme program into the Definitions window and click the Run button:(define (somef x) (sin x x))
Then, in the Interactions window, evaluate the expressions:(somef 10 20)(somef 10)
45
![Page 46: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/46.jpg)
2.4 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 amulti-lineblock comment. |#
46
![Page 47: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/47.jpg)
2.4 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)))
;; Tests:
(area-of-disk 5) "=" 78.5
47
![Page 48: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/48.jpg)
Section 3: Programs are Function Plus Variable Definitions
3.1 Composing Functions3.2 Variable Definitions3.3 Finger Exercises on
Composing Functions48
![Page 49: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/49.jpg)
Programs are Function Plus Variable Definitions In general, a program consists not just of one, but of many definitions.
The area-of-ring program, for example, consists of two definitions: the one for area-of-ring and another one for area-of-disk.
(define (area-of-ring outer inner) (- (area-of-disk outer)
(area-of-disk inner)))49
![Page 50: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/50.jpg)
Programs are Function Plus Variable Definitions We refer to both as FUNCTION DEFINITIONs and, using mathematical terminology in a loose way, say that the program is COMPOSED of several functions.
Because the first one, area-of-ring, is the function we really wish to use, we refer to it as the MAIN FUNCTION; the second one, area-of-disk, is an AUXILIARY FUNCTION, occasionally also called HELPER FUNCTION.
50
![Page 51: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/51.jpg)
3.1 Composing Functions
The use of auxiliary functions makes the design process manageable and renders programs readable. Compare the following two versions of area-of-ring:
(define (area-of-ring outer inner) (- (area-of-disk outer)(area-of-disk inner)))
(define (area-of-ring outer inner) (- (* 3.14 (* outer outer))(* 3.14 (* inner inner))))
51
![Page 52: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/52.jpg)
3.2 Variable Definitions
52
![Page 53: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/53.jpg)
3.2 Variable Definitions
The order for variable 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 definition of RADIUS when it tries to evaluate (* 2 RADIUS).
53
![Page 54: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/54.jpg)
3.3 Finger Exercises on Composing Functions
Exercise 3.3.1 Convert English measurements to metric ones
Here is a table that shows the six major units of length measurements of the English system:
English metric
1 inch = 2.54 cm
1 foot = 12in.
1 yard = 3 ft.
1 rod = 5(1/2) yd.
1 furlong = 40rd.
1 mile = 8fl.
54
![Page 55: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/55.jpg)
3.3 Finger Exercises on Composing Functions
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.
55
![Page 56: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/56.jpg)
3.3 Finger Exercises on Composing Functions
Exercise 3.3.2 Develop the program volume-cylinder. It consumes the radius of a cylinder's base disk and its height; it computes the volume of the cylinder.
56
![Page 57: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/57.jpg)
3.3 Finger Exercises on Composing Functions
Exercise 3.3.3 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.
57
![Page 58: Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations](https://reader035.vdocument.in/reader035/viewer/2022062519/5697bfbe1a28abf838ca27d1/html5/thumbnails/58.jpg)
การบ้�าน Exercise 3.3.1 ชื่�อไฟล์� ex3_3_1.rkt Exercise 3.3.2 ชื่�อไฟล์� ex3_3_2.rkt Exercise 3.3.3 ชื่�อไฟล์� ex3_3_3.rkt
ในโปรแกรมให้�ใส่� comment บ้อกรายล์ะเอ�ยดด�วย;; รห้�ส่น ส่ ต ชื่�อ-นามส่ก"ล์;; Contract: ………
;; Purpose: ………
;; Example: ………
;; Definition: ………
;; Tests: ………
Zip files โดยต�#งชื่�อเป%นชื่�อรห้�ส่น ส่ ต ส่�ง[email protected]
ส่�งภายในเส่าร�ว�นที่��1 พฤศจิ กายน2557 58