49
Functional Programming Langu age 1 Scheme Language: p

Functional Programming Language 1 Scheme Language: part 2

Embed Size (px)

Citation preview

Page 1: Functional Programming Language 1 Scheme Language: part 2

FunctionalProgramming Language

1

Scheme Language: part 2

Page 2: Functional Programming Language 1 Scheme Language: part 2

Section 4: Conditional Expressions

4.1 Booleans and Relations 4.2 Functions that Test Conditions 4.3 Conditionals and Conditional Functions

2

Page 3: Functional Programming Language 1 Scheme Language: part 2

4.1 Booleans and Relations

In mathematics we talk of true and false claims, which are conditions.

For example, if x and y are numbers, we state these three claims about x and y with1. x = y: ``x is equal to y'';2. x < y: ``x is strictly less than y'';3. x > y: ``x is strictly greater than y''.

3

Page 4: Functional Programming Language 1 Scheme Language: part 2

4.1 Booleans and Relations

In addition to determining whether an atomic claim holds in a given situation.

Consider the three claims above, which we can combine in several ways:1. x = y and x < y and x > y2. x = y or x < y or x > y3. x = y or x < y .

4

Page 5: Functional Programming Language 1 Scheme Language: part 2

4.1 Booleans and Relations

Like mathematics, Scheme has ''words'' for expressing truth and falsity, for stating atomic claims, for combining claims into compound claims, and for expressing that a claim is true or false.

The ''word'' for true is #t and the ''word'' for false is #f.

If a claim concerns the relationship between two numbers, it can typically be expressed with a RELATIONAL OPERATION, for example, =, <, >, <= and >=. 5

Page 6: Functional Programming Language 1 Scheme Language: part 2

4.1 Booleans and Relations

Translating the three mathematical claims from above follows our well-known pattern of writing a left parenthesis, followed by the operator, its arguments, and a right parenthesis:1. (= x y): ``x is equal to y'';2. (< x y): ``x is strictly less than y''; and3. (> x y): ``x is strictly greater than y''. 6

Page 7: Functional Programming Language 1 Scheme Language: part 2

4.1 Booleans and Relations

Like the arithmetic operations, the test operations can take more than two arguments.

For example, (< 1 2 3)

is legal Scheme notation for ``1 < 2 and 2 < 3.''

7

Page 8: Functional Programming Language 1 Scheme Language: part 2

4.1 Booleans and Relations

The and and or operators also take any number of arguments.

The not operation always takes one argument.

The and, or, and not operators work on Boolean values only.

1. (and (= x y) (< y z))2. (or (= x y) (< y z))3. (not (= x y)) 8

Page 9: Functional Programming Language 1 Scheme Language: part 2

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

9

Page 10: Functional Programming Language 1 Scheme Language: part 2

4.1 Booleans and Relations

Exercise 4.1.1 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))

10

Page 11: Functional Programming Language 1 Scheme Language: part 2

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

11

Page 12: Functional Programming Language 1 Scheme Language: part 2

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

12

Page 13: Functional Programming Language 1 Scheme Language: part 2

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 (exclusive) ;; or larger than or equal to 10(define (is-between-5-6-or-over-10? n)

(or (is-between-5-6? n) (>= n 10)))13

Page 14: Functional Programming Language 1 Scheme Language: part 2

4.2 Functions that Test Conditions

Exercise 4.2.1 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]:14

Page 15: Functional Programming Language 1 Scheme Language: part 2

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]

15

Page 16: Functional Programming Language 1 Scheme Language: part 2

4.2 Functions that Test Conditions

Exercise 4.2.2. 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) (<= x 5)))) 16

Page 17: Functional Programming Language 1 Scheme Language: part 2

4.2 Functions that Test Conditions

Mathematical equations in one variable are claims about an unknown number.

For example, the quadratic equation

(x2 + 2) (x + 1) = 0

17

Page 18: Functional Programming Language 1 Scheme Language: part 2

4.2 Functions that Test Conditions

We can use Scheme to formulate equational conditions as a function. ;; equation1 : number -> boolean;; to determine whether x is a solution

;; for (x2 + 2) (x + 1) = 0 (define (equation1 x)

(= (+ (* x x) (+ (* 2 x) 1)) 0))18

Page 19: Functional Programming Language 1 Scheme Language: part 2

4.2 Functions that Test Conditions

When we apply equation1 to some number, we get true or false:

(equation1 -1)= true

and(equation1 +1)= false 19

Page 20: Functional Programming Language 1 Scheme Language: part 2

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.20

Page 21: Functional Programming Language 1 Scheme Language: part 2

4.3 Conditionals and Conditional Functions

The general shape of a conditional expression is

Example:

21

(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"])

Page 22: Functional Programming Language 1 Scheme Language: part 2

4.3 Conditionals and Conditional Functions

Exercise 4.3.1 Decide which of the following two cond-expressions is legal:

22

(cond[(< n 10) 20][(> n 20) 0][else 1])

(cond [(< n 10) 20] [(and (> n 20) (<= n 30))] [else 1])

Page 23: Functional Programming Language 1 Scheme Language: part 2

4.3 Conditionals and Conditional Functions

Exercise 4.3.2 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 more than $5,000.

23

Page 24: Functional Programming Language 1 Scheme Language: part 2

4.3 Conditionals and Conditional Functions

;; interest-rate : number -> number;; to determine the interest rate for the given amount(define (interest-rate amount)

(cond[(<= amount 1000) 0.040][(<= amount 5000) 0.045][else 0.050])) 24

Page 25: Functional Programming Language 1 Scheme Language: part 2

Section 5: Symbolic Information

25

Page 26: Functional Programming Language 1 Scheme Language: part 2

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?

26

Page 27: Functional Programming Language 1 Scheme Language: part 2

5. Symbolic Information

Scheme provides only one basic operation on symbols: symbol=?, a comparison operation.1. (symbol=? 'Hello 'Hello)

= true2. (symbol=? 'Hello 'Howdy)

= false

27

Page 28: Functional Programming Language 1 Scheme Language: part 2

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] [(symbol=? s 'GoodEvening)

'BoyAmITired]))28

Page 29: Functional Programming Language 1 Scheme Language: part 2

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?“ The operation needed is string=?,

which compares two strings The way symbol=? compares two

symbols. 29

Page 30: Functional Programming Language 1 Scheme Language: part 2

Section 6: Structures 6.1 Structures 6.2 Extended Exercise: Drawing Simple Pictures 6.3 Structure Definitions

30

Page 31: Functional Programming Language 1 Scheme Language: part 2

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 lang/htdp-beginner. (require lang/posn)

It has an x coordinate, and it has a y coordinate, which tells us where the pixel is located.

31

Page 32: Functional Programming Language 1 Scheme Language: part 2

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 extracts the y coordinate. 32

Page 33: Functional Programming Language 1 Scheme Language: part 2

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)) 3. (posn-y (make-posn 7 0))

33

Page 34: Functional Programming Language 1 Scheme Language: part 2

6.1 Structures

34

Page 35: Functional Programming Language 1 Scheme Language: part 2

6.1 Structures Example: 1. (distance-to-0 (make-posn 3 4))

= 5 2. (distance-to-0 (make-posn 8 6))

= 10 3. (distance-to-0 (make-posn 5

12))= 13

35

Page 36: Functional Programming Language 1 Scheme Language: part 2

6.1 Structures Exercise 6.1.1 Evaluate the

following expressions:1. (distance-to-0 (make-posn 3 4))2. (distance-to-0 (make-posn (* 2 3) (* 2 4)))3. (distance-to-0 (make-posn 12 (- 6 1)))

36

Page 37: Functional Programming Language 1 Scheme Language: part 2

6.2 Extended Exercise: Drawing Simple Pictures

DrRacket provides the graphics teachpack draw.rtk, which introduces simple graphics operations:

1. draw-solid-line, – which consumes two posn

structures, – the beginning and the end of the

line on the canvas, – and a color. 37

Page 38: Functional Programming Language 1 Scheme Language: part 2

6.2 Extended Exercise: Drawing Simple Pictures

2. draw-solid-rect, – which consumes four arguments: – a posn structure for the upper-left

corner of the rectangle, – a number for the width of the

rectangle, – another number for its height,

and a color.

38

Page 39: Functional Programming Language 1 Scheme Language: part 2

6.2 Extended Exercise: Drawing Simple Pictures

3. draw-solid-disk, – which consumes three arguments: – a posn structure for the center of the

disk, – a number for the radius of the disk,– and a color.

4. draw-circle, which consumes three arguments:

– a posn structure for the center of the circle,

– a number for the radius, – and a color.

39

Page 40: Functional Programming Language 1 Scheme Language: part 2

6.2 Extended Exercise: Drawing Simple Pictures

We refer to the action to the canvas as an EFFECT.

Each drawing operation also comes with a matching clear- operation: clear-solid-line, clear-solid-rect, clear-solid-disk, and clear-circle.

Read the documentation for draw.ss in DrRacket's HelpDesk.

40

Page 41: Functional Programming Language 1 Scheme Language: part 2

6.2 Extended Exercise: Drawing Simple Pictures

Drawing operations on computers interpret the screen as follows:

The origin of the plane is in the upper-left corner. 41

Page 42: Functional Programming Language 1 Scheme Language: part 2

6.2 Extended Exercise: Drawing Simple Pictures

When you evaluate (start 500 500) with the draw.ss library, DrRacket create a new, empty drawing canvas, like this one:

42

Page 43: Functional Programming Language 1 Scheme Language: part 2

6.2 Extended Exercise: Drawing Simple Pictures

Evaluating a drawing expressions,

(draw-solid-line (make-posn 100 100) (make-posn 200 200) 'red)

43

Page 44: Functional Programming Language 1 Scheme Language: part 2

6.2 Extended Exercise: Drawing Simple Pictures

Exercise 6.2.1 Evaluate the following expressions in order:

(start 300 300)(draw-solid-line (make-posn 10 10)

(make-posn 110 10) 'red)(draw-solid-rect (make-posn 10 30) 100

50 'blue) (draw-circle (make-posn 110 30) 30 'yellow)

(draw-solid-disk (make-posn 10 80) 50 'green)

(stop) 44

Page 45: Functional Programming Language 1 Scheme Language: part 2

6.3 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.

45

Page 46: Functional Programming Language 1 Scheme Language: part 2

6.3 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 consumes three values.

(make-entry 'PeterLee 15270 '606-7771) 46

Page 47: Functional Programming Language 1 Scheme Language: part 2

6.3 Structure Definitions

The define-struct definition of entry also introduces new selectors:entry-name entry-zip entry-phone

Here is how we can use the first one:(entry-name (make-entry 'PeterLee 15270 '606-7771))= 'PeterLee 47

Page 48: Functional Programming Language 1 Scheme Language: part 2

6.3 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) 48

Page 49: Functional Programming Language 1 Scheme Language: part 2

6.3 Structure Definitions

Exercise 6.3.1 Consider the following structure definitions:1. (define-struct movie (title producer))2. (define-struct boyfriend (name hair eyes phone))3. (define-struct cheerleader (name number))4. (define-struct CD (artist title price))5. (define-struct sweater (material size producer))

49