Functional Programming - Boise State...
Transcript of Functional Programming - Boise State...
![Page 1: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/1.jpg)
Functional Programming
Alark Joshi
Some slides are based on the slides at http://courses.cs.vt.edu/~cs3304/Spring02/lectures/lect04.pdf
Thursday, November 15, 12
![Page 2: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/2.jpg)
Functional Programs
• Functional programs are made up of functions applied to data
• We write expressions rather than commands that need to be executed
• Pure functional languages have no side effects - What are side effects?
Thursday, November 15, 12
![Page 3: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/3.jpg)
Functional Programming in ML• ML was developed in Edinburgh in late
1970’s by Milner, Gordon and Wadsworth
• Meta-Language for automated theorem proving
• ML is not a pure language
• Reference variables, commands, I/O
Thursday, November 15, 12
![Page 4: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/4.jpg)
ML Characteristics
Thursday, November 15, 12
![Page 5: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/5.jpg)
ML Characteristics
• Functions are first class citizens
Thursday, November 15, 12
![Page 6: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/6.jpg)
ML Characteristics
• Functions are first class citizens
• Statically scoped
Thursday, November 15, 12
![Page 7: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/7.jpg)
ML Characteristics
• Functions are first class citizens
• Statically scoped
• Static typing via type inference
Thursday, November 15, 12
![Page 8: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/8.jpg)
ML Characteristics
• Functions are first class citizens
• Statically scoped
• Static typing via type inference
• Polymorphic types
Thursday, November 15, 12
![Page 9: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/9.jpg)
ML Characteristics
• Functions are first class citizens
• Statically scoped
• Static typing via type inference
• Polymorphic types
• Exception handling
Thursday, November 15, 12
![Page 10: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/10.jpg)
ML Characteristics
• Functions are first class citizens
• Statically scoped
• Static typing via type inference
• Polymorphic types
• Exception handling
• Garbage collection
Thursday, November 15, 12
![Page 11: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/11.jpg)
Using the ML Interpreter
Thursday, November 15, 12
![Page 12: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/12.jpg)
Using the ML Interpreter
• Type sml
Thursday, November 15, 12
![Page 13: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/13.jpg)
Using the ML Interpreter
• Type sml
• Hyphen (-) is the prompt, = waiting/incomplete
Thursday, November 15, 12
![Page 14: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/14.jpg)
Using the ML Interpreter
• Type sml
• Hyphen (-) is the prompt, = waiting/incomplete
• Type at the prompt or write a program in a file
Thursday, November 15, 12
![Page 15: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/15.jpg)
Using the ML Interpreter
• Type sml
• Hyphen (-) is the prompt, = waiting/incomplete
• Type at the prompt or write a program in a file
• Type use “file.sml” to load definition from the file
Thursday, November 15, 12
![Page 16: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/16.jpg)
Using the ML Interpreter
• Type sml
• Hyphen (-) is the prompt, = waiting/incomplete
• Type at the prompt or write a program in a file
• Type use “file.sml” to load definition from the file
• End session by typing Ctrl-D.
Thursday, November 15, 12
![Page 17: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/17.jpg)
Expressions
• Expression evaluation:
- 3;
val it = 3; int
- 23 - 6;
val it = 17; int
Name it refers to the last computed value
Thursday, November 15, 12
![Page 18: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/18.jpg)
Constants
• In ML, we name values rather than have variables
- val pi = 3.14159;
val pi = 3.14159 : real
- val r = 2.0;
val r = 2.0 : real
- val area = pi * r * r;
val area = 12.56636 : real
Thursday, November 15, 12
![Page 19: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/19.jpg)
Functions
• Syntax: fun name arg = expression
• Example:
- fun area (r) = pi * r * r;
val area = fn: real->real
• For a single parameter, the () are optional. Area could be invoked as area 3
Thursday, November 15, 12
![Page 20: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/20.jpg)
Functions
• A function can also be written as a value
- val area = fn r => pi *r *r
val area = fn: real -> real
- area 2.0; or area(2.0);
val it = 12.56636 : real
Thursday, November 15, 12
![Page 21: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/21.jpg)
Environment
• pi define outside of area
val pi = 3.14159;
fun area(r) = pi*r*r;
• Environment of function determines value
Thursday, November 15, 12
![Page 22: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/22.jpg)
Environment
• What happens if we change the value of pi?
- area 1.1;
val it = 3.8013239 : real
- val pi = 2000;
val pi = 2000 : int
- area 1.1;
val it = 3.8013239 : real
Thursday, November 15, 12
![Page 23: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/23.jpg)
Structured Data Types
• Tuples - ordered collection of values
• Records - collection of named values
• Lists - list of values of homogeneous types
Thursday, November 15, 12
![Page 24: Functional Programming - Boise State Universitycs.boisestate.edu/.../functional_programming_ml.pdf · Functional Programming in ML • ML was developed in Edinburgh in late 1970’s](https://reader036.fdocuments.net/reader036/viewer/2022081613/5fbe5bef3c273e5ced393645/html5/thumbnails/24.jpg)
Thursday, November 15, 12