242-203 comp. eng. sw lab ii: fp with scheme 1 computer eng. software lab ii 242-203, semester 2,...
TRANSCRIPT
![Page 1: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/1.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 1
Computer Eng. Software Lab II242-203, Semester 2, 2014-2015
Who I am:Andrew DavisonCoE, WiG Lab Office [email protected]
Functional Programming with Scheme
Please askquestions
![Page 2: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/2.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 2
Overview
1. Why Scheme?
2. Constants (names)
3. Functions
4. Lists
5. No-name Functions: lambda
6. cond
7. Recursion
continued
![Page 3: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/3.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 3
8. Higher Order Functions
9. Using DrScheme
10. More Information
![Page 4: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/4.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 4
1. Why Scheme?
• Scheme is one of the simpler functional programming languages– a Scheme program is a collection of functions
• Scheme also has assignment for greater programming flexibility– but it is not needed in most programs
continued
![Page 5: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/5.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 5
• Scheme has plenty of advanced features– e.g. first-class functions
• function body code can be manipulated as data• data can be used as function body code
• Scheme is used in lots of Artficial Intelligence (AI) projects and textbooks.
![Page 6: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/6.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 6
2. Constants (names)
myPi is evaluated thenprinted
'myPi is not evaluated
More details on howto use DrScheme aregiven in Section 9.
myPi is defined here
'pi is pre-defined
![Page 7: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/7.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 7
3. Functions
• A function call is written as:(operator arg1 arg2 … argn)
• For example:(+ 2 3) means 2 + 3
![Page 8: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/8.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 8
Function Call Examples
The + operator(and other arithmetic)operators can takeany number ofarguments.
The same as: (2+3)*(1+2)
![Page 9: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/9.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 9
Test Functions (Predicates)
• Test functions return boolean values true or false.
• Their names end with a '?'– e.g. number? char?
![Page 10: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/10.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 10
Defining Functions (v.1)
Similar C code is: int sq(int x) { return x*x; }
![Page 11: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/11.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 11
Similar C code is: int myMax(int x,int y) { if (x > y) return x; else return y; }
![Page 12: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/12.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 12
Combining Functions
• Scheme functions can be easily combined together– make a function call be the argument of another
function
function callarguments
![Page 13: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/13.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 13
4. Lists
• List values are placed inside (list …):– (list ) // an empty list– (list 1 2 3) // a list of numbers– (list 'a 'b 'c) // a list of names– (list (list 1 2) (list 2 3 4)) // a list of
two lists
• If the language includes list abbreviations, then (list ...) can be written as '(...)– '(1 2 3) is the same as (list 1 2 3)
![Page 14: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/14.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 14
Basic List Operations• (null? x)
– returns true is x is an empty list; otherwise returns false
• (car x)– returns the first element (head) of a non-empty
list x• (cdr x)
– returns the tail of a non-empty list x• everything except the first element
continued
![Page 15: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/15.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 15
• (cadr x)– returns the head element of the tail of x
• (cons h l)– makes a new list where the head is h, the tail is l
• (list arg1 arg2 … argn)– places all of its arguments into a new list
![Page 16: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/16.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 16
List Operation Examples
![Page 17: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/17.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 17
plusList plusList pulls out the first two arguments of alist and adds them
![Page 18: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/18.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 18
Some Other Useful List Functions
• (length x)– returns the length of a list x
• (append x y)– makes a new list by appending list x onto the
front of list y• (reverse x)
– makes a list by reversing the list x
and many, many more...
continued
![Page 19: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/19.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 19
combining reverseand append
![Page 20: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/20.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 20
5. No-name Functions: lambda
Similar to the C-like code: int ??(int x) { return x *x; }
Similar to a C-likefunction call: ??(3)
The language 'level' must beincreased for lambda to be supported.
![Page 21: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/21.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 21
Defining Functions (v.2)
Similar to the C-like code: sq2 = int ??(int x) { return x * x; }
![Page 22: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/22.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 22
Why have lambda?
• For simple examples (like ours), there is no need for lambda.
• lambda becomes very useful in higher order programming, where data and functions need to be translated into each other– e.g. parsing, AI
![Page 23: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/23.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 23
6. cond: the multi-branch if
Similar to the C code: int sizer(int x) { if (x == 0) return 0; else if (x > 0) return 1; else return -1; }
![Page 24: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/24.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 24
7. Recursion
• Base case:– the function returns an answer and stops
• Recursion step:– the body of the function calls itself (with
smaller arguments)
![Page 25: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/25.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 25
length Defined
recursive step
base case
![Page 26: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/26.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 2633
append Defined
![Page 27: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/27.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 27
member Defined mem? is a predicate(a test function)
mem is the wrongname
![Page 28: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/28.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 28
8. Higher Order Functions
• A higher order function has 1 or more arguments which are function names– higher order functions are also called
functionals
• Higher order functions are very important to advanced functional programming and AI.
An ‘Intermediate Student’ feature
![Page 29: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/29.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 29
apply
• (apply f x)– its first argument, f, is a function name– its second argument, x, is the input for f– same as executing (f x)
![Page 30: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/30.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 30
![Page 31: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/31.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 31
map
• (map f x)– returns a list by applying the function f to each
element of the list x
![Page 32: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/32.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 32
map and plusList
plusList is appliedto the 4 lists in theinput list.
![Page 33: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/33.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 33
9. Using DrScheme
• Download the installation program for DrScheme from:
http://fivedots.coe.psu.ac.th/Software.coe/LAB/
FuncProg/
• The filename:plt-4.1.4-bin-i386-win32.exe
• Installs on Windows XP or later– the installation is called PLT Scheme, v4.1.4
![Page 34: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/34.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 34
• Start DrScheme from the Windows Start menu item PLT Scheme:
definitions go here
execute functionshere
![Page 35: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/35.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 35
Some Notes
• Set the language mode to “Beginning Student with List Abbreviations”– under the Language >
Choose Language menu item
continued
![Page 36: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/36.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 36
• Press the "Run" button after adding a new function to the definitions window– this makes the new function visible down in the
execution window
![Page 37: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/37.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 37
Create a Scheme Programusing any text editor
![Page 38: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/38.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 38
Load myMax.scmuse the File/Open menu item
Press the "Run" button for the execution window to appear at the bottom.
![Page 39: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/39.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 39
Use the myMax Function
![Page 40: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/40.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 40
• Alternatively, you can type the function into DrScheme's definition window, and save it from there.– you get colour-coded syntax, indenting
(tabbing), and error checking as you type
![Page 41: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/41.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 41
10. More Information
• The "Help/ Help Desk" menu item:
![Page 42: 242-203 Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II 242-203, Semester 2, 2014-2015 Who I am: Andrew Davison CoE, WiG Lab Office](https://reader035.vdocument.in/reader035/viewer/2022062221/56649e155503460f94aff9e3/html5/thumbnails/42.jpg)
242-203 Comp. Eng. SW Lab II: FP with Scheme 42
PLT Scheme Racket
• PLT Scheme v.5 was renamed to be called Racket– two websites:
• http://plt-scheme.org/ (frozen in 2010)• http://racket-lang.org/ (active)
• At the beginner's level (i.e. for us), there's no difference between the two languages.