Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations.

Similar presentations


Presentation on theme: "Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations."— Presentation transcript:

1 Functional Programming Language 1

2 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 Abstracti on– Based on the Lambda Calculus 2

3 Example: Functional Language Haskell Erlang ML Scheme Ruby Pure Needle Clean J Pizza Mercury Goo … 3

4 Scheme Language Book: http://www.schemers.org/ 4

5 Section 1: Getting Started 5

6 Download DrRacket Racket is a Scheme Download DrRacket http://www.informatics.buu.ac.th/~jan ya/886340_PPL/Programming%20La nguages/Scheme/racket-6.1-i386- win32.exe 6

7 The Racket Language Choose Language: The Racket Language 7 Definition window Interaction window

8 Section 2: Numbers and Expressions 2.1 Numbers and Arithmetic 2.2 Variables and Programs 2.3 Errors 2.4 Designing Programs 8

9 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

10 2.1 Numbers and Arithmetic 5-52/317/3 1.4142165237321 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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 A B ; –(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

21 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 definition start program and input names end input names end definition 21

22 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

23 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

24 2.2 Variables and Programs 24

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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 is area of a triangle = 1/2 * base * height 37

38 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

39 2.2 Variables and Programs Exercise 2.2.4 Also formulate the following three expressions as programs: 1. n 2 + 10 2. (1/2) · n 2 + 20 3. 2 - (1/n) Determine their results for n = 2 and n = 9 by hand and with DrRacket. 39

40 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

41 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

42 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

43 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

44 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

45 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

46 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 a multi-line block comment. |# 46

47 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

48 Section 3: Programs are Function Plus Variable Definitions 3.1 Composing Functions 3.2 Variable Definitions 3.3 Finger Exercises on Composing Functions 48

49 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

50 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

51 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

52 3.2 Variable Definitions 52

53 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

54 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: Englishmetric 1 inch= 2.54cm 1 foot= 12in. 1 yard= 3ft. 1 rod= 5(1/2)yd. 1 furlong= 40rd. 1 mile= 8fl. 54

55 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

56 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

57 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

58 การบ้าน 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 โดยตั้งชื่อเป็นชื่อรหัสนิสิต ส่ง mai.janya@gmail.com ส่งภายในเสาร์วันที่ 1 พฤศจิกายน 2557 58


Download ppt "Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations."

Similar presentations


Ads by Google