85
Ch.15 Functional Programming Languages 清清清清清清清 清清清 CS2403 Programming Languages Spring, 2011

Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Embed Size (px)

Citation preview

Page 1: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Ch.15 Functional Programming Languages

清華大學資工系 孫文宏CS2403 Programming Languages

Spring, 2011

Page 2: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Timing

一 二 三 四 五

3/21 3/24

未定 未定

• HW2 (50%)

• HW? (50%)

Page 3: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

References

• Articles:– 思考函數編程

http://jerrylovesrebol.blogspot.com/2008/05/fp_22.html– Why functional languages rock with multi-core

http://prettyprint.me/2009/08/01/why-functional-languages-rock-with-multi-core/

– Can Your Programming Language Do This?http://www.joelonsoftware.com/items/2006/08/01.html

• Book:– 《計算機怎樣解幾何題:談談自動推理》

Page 4: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

• Manuals:– 維基教科書 – Lisp 入門

http://zh.wikibooks.org/zh-tw/Lisp_入門– Common Lisp the Language, 2nd Edition

http://www-prod-gif.supelec.fr/docs/cltl/clm/clm.html– Common LISP Hints

http://www.n-a-n-o.com/lisp/cmucl-tutorials/LISP-tutorial.html– GNU Emacs Lisp Reference Manual

http://www.delorie.com/gnu/docs/elisp-manual-21/elisp_toc.html• Downloading LISP interpreter:

– GNU Common LISPftp://ftp.gnu.org/pub/gnu/gcl/binaries/stable/

– CLISPhttp://www.clisp.org/

Page 5: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Positioning…

• The programming language class includes roughly the following topics

Language Design

PhilosophyGrammar

Binding, type, scope

Expression, statement

Subprogram, parameter

passing

Language implement-

ation

Imperative language

Functional language

Logical language

Page 6: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

I’m good at C/C++ or Java. Why should I learn another kind

of language?

Page 7: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Answer 1: Would you like to learn how to use “multi-touch”?

Human ideas Translation ExecutionUser

Interface

I want to rotate a picture

Page 8: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Human ideas Translation ExecutionProgramming

language

I want to invent a new search engine

Imperative language

Functional language

Page 9: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Answer 2: Functional Language Renaissance

• Its power in parallel programming makes it hot again

• Parallel programming– It seems Moore’s law is reaching a limit. CPU clock

rate is not going faster infinitely– Multi-core architecture– Large computing/data center, cloud computing

Page 10: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Imperative Languages vs. Parallel Computing

• “There are quite a few new challenges, most of them are about scale, such as scaling your database, scaling your users sessions etc, but one of the most significant challenges is scaling your algorithm by parallelizing it.”

• “The problem is that imperative languages keep state. The state is in their variables. …When you want two or more threads to access the same variable, or the same state (and modify it) you need to synchronize them; Creating correct synchronization is hard and many times results in bottlenecks.”

Copied from http://prettyprint.me/2009/08/01/why-functional-languages-rock-with-multi-core/

Page 11: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

“State, Mutable Data”

Copied from http://prettyprint.me/2009/08/01/why-functional-languages-rock-with-multi-core/

Page 12: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

“Can Your Programming Language Do This?”

Copied from http://www.joelonsoftware.com/items/2006/08/01.html

Page 13: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program a.v1

// A trivial example: alert("I'd like some Spaghetti!"); alert("I'd like some Chocolate Moose!");

The following programs are all from http://www.joelonsoftware.com/items/2006/08/01.html

Page 14: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program a.v2

function SwedishChef( food ) {

alert("I'd like some " + food + "!"); }

SwedishChef("Spaghetti"); SwedishChef("Chocolate Moose");

• Code reuse• Maintainability, readability, abstraction…

Page 15: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program b.v1alert("get the lobster"); PutInPot("lobster"); PutInPot("water");

alert("get the chicken"); BoomBoom("chicken"); BoomBoom("coconut");

Page 16: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program b.v2function Cook( i1, i2, f ) {

alert("get the " + i1); f(i1); f(i2);

}

Cook( "lobster", "water", PutInPot ); Cook( "chicken", "coconut", BoomBoom );

• Pass functions as parameters

Page 17: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program b.v2

function PutInPot( x ){ alert( “pot ” + x ); }funciton BoomBoom( x ){ alert( “boom ” + x ); }

function Cook( i1, i2, f ) {

alert("get the " + i1); f(i1); f(i2);

}

Cook( "lobster", "water", PutInPot ); Cook( "chicken", "coconut", BoomBoom );

Page 18: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program b.v3function Cook( i1, i2, f ) {

alert("get the " + i1); f(i1); f(i2);

}

Cook( "lobster", "water", function(x) { alert("pot " + x); } );

Cook( "chicken", "coconut", function(x) { alert("boom " + x); } );

• Create the function on the fly without naming it

Page 19: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program c.v1var a = [1,2,3];

for (i=0; i<a.length; i++) {

a[i] = a[i] * 2; }

for (i=0; i<a.length; i++) {

alert(a[i]); }

Page 20: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program c.v2function map(fn, a) {

for (i = 0; i < a.length; i++) {

a[i] = fn(a[i]); }

}

map( function(x){return x*2;}, a ); map( alert, a );

• Do something to every element in an array

Page 21: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program d.v1function sum(a) {

var s = 0; for (i = 0; i < a.length; i++)

s += a[i]; return s;

} function join(a) {

var s = ""; for (i = 0; i < a.length; i++)

s += a[i]; return s;

}

alert( sum([1,2,3]) ); alert( join(["a","b","c"]) );

Page 22: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Program d.v2function reduce(fn, a, init) {

var s = init; for (i = 0; i < a.length; i++)

s = fn( s, a[i] ); return s;

} function sum(a) {

return reduce( function(a, b){ return a + b; }, a, 0 ); } function join(a) {

return reduce( function(a, b){ return a + b; }, a, "" ); }

alert( sum([1,2,3]) ); alert( join(["a","b","c"]) );

• Combine all elements in an array to one

Page 23: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

• “Without understanding functional programming, you can't invent MapReduce, the algorithm that makes Google so massively scalable.”

• “Programming languages with first-class functions let you find more opportunities for abstraction, which means your code is more scalable. Lots of Google applications use MapReduce and they all benefit whenever someone optimizes it or fixes bugs.”

• “The terms Map and Reduce come from LISP and functional programming.”

• “Ok. I hope you're convinced by now.”

Copied from http://www.joelonsoftware.com/items/2006/08/01.html

Page 24: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

History Class…

Page 25: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Pictures copied from Internet

Page 26: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Pictures copied from Internet

Page 27: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Pictures copied from Internet

Page 28: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Bertrand Russell (1872-1970)

Pictures copied from Internet

Page 29: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

1+1=2

Pictures copied from Internet

Page 30: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

John Von Neumann (1903-1957)

Pictures copied from Internet

Page 31: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Alan Turing (1912-1954)

Pictures copied from Internet

Page 32: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Alonzo Church (1903-1995)

Pictures copied from Internet

Page 33: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Functional Programming Concepts

Page 34: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Copyright © 2009 Addison-Wesley. All rights reserved.

Introduction

• The design of the imperative languages is based directly on the von Neumann architecture– Efficiency is the primary concern, rather than the

suitability of the language for software development• The design of the functional languages is based

on mathematical functions– A solid theoretical basis that is also closer to the user,

but relatively unconcerned with the architecture of the machines on which programs will run

Page 35: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Mathematical Functions

• In an imperative language, – Operations are done and the results are stored in

variables for later use• The objective of the design of a functional

language is to mimic mathematical functions to the greatest extent possible– In an functional language, the evaluation of a

function always produces the same result given the same parameters

– No side effect, a.k.a. referential transparency

Copied and altered from textbook “Concepts of Programming Languages”

Page 36: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Copied and altered from textbook “Concepts of Programming Languages”

Lambda Calculus

• Lambda calculus, λ-calculus– Alonzo Church, 1930s– A formal system for function definition and

application• A mathematical function is a mapping

– of members of one set, called the domain set, – to another set, called the range set

• E.g.cube(x) x*x*x cube(2) results in 8

Page 37: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Lambda Expressions

• A method for defining nameless functions• It specifies the parameter(s) and the mapping

of a function in the following form• E.g.

(x) x*x*x ((x) x*x*x)(2) results in 8

Copied and altered from textbook “Concepts of Programming Languages”

Page 38: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Functional Forms

• A higher-order function, or functional form, is one that – either takes functions as parameters,– or yields a function as its result, – or both

• Some kinds of function forms– Function composition– Apply-to-all (map)– Construction– Reduce

Copied and altered from textbook “Concepts of Programming Languages”

Page 39: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Function Composition• A functional form that

– takes two functions as parameters – and yields a function whose value is the first actual

parameter function applied to the application of the second

• Form: – h f ° g– which means h (x) f ( g ( x))

• E.g.– For f(x) x + 2 and g(x) 3 * x,– h f ° g yields (3 * x)+ 2

Copied and altered from textbook “Concepts of Programming Languages”

Page 40: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Apply-to-all (map)• A functional form that

– takes a single function as a parameter – and yields a list of values obtained by applying the

given function to each element of a list of parameters

• Form: –

• E.g.– For h(x) x * x– (h, (2, 3, 4)) yields (4, 9, 16)

Copied and altered from textbook “Concepts of Programming Languages”

Page 41: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Construction• A functional form that

– takes a list of functions as parameters – and yields a list of values obtained by applying the list

of functional parameters to an argument • Form:

– [f, g, h]• E.g.

– f(x) x*x– g(x) x*2– h(x) x/2– [f,g,h](4) yields (16, 8, 2)

Copied and altered from textbook “Concepts of Programming Languages”

Page 42: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Some Famous Functional Languages

Page 43: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

43

Genealogy of Common Languages

(Fig. 2.1)

Page 44: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

LISP• LISP is Invented by John McCarthy in 1958 at MIT

– Lisp is the second-oldest high-level programming language in widespread use today (Fortran is the oldest)

– The name LISP strands for "LISt Processing“– The original LISP uses dynamic scope

• The 1st LISP interpreter– was implemented by Steve Russell on a IBM 704 mainframe

• The first LISP compiler – was implemented by Tim Hart and Mike Levin in 1962 at MIT

• It has a large family of dialects– Such as Stanford LISP 1.6, XLISP, AutoLISP– Scheme and Common LISP are the most widely known dialects

Copied and altered from http://en.wikipedia.org/wiki/Lisp_(programming_language)

Page 45: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Scheme

• Scheme was developed – by Guy L. Steele and Gerald Jay Sussman, in the

mid-1970s, at the MIT AI Lab

• It was designed to be a cleaner, more modern, and simpler version than the contemporary dialects of LISP– Uses only static scoping

Copied and altered from textbook “Concepts of Programming Languages”

Page 46: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

COMMON LISP• A combination of many popular LISP dialects

– It is developed in 1980s, by the efforts of Guy L. Steele – Included in the ANSI standard in 1984

• A large and complex language -- the opposite of Scheme• Features include:

– records – arrays – character strings– powerful I/O capabilities– iterative control statements– supporting both static and dynamic scope– …etc.

Copied and altered from textbook “Concepts of Programming Languages”

Page 47: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Other Well-know FPLs• ML (1970s)

– Uses type declarations, and type inferencing– It is strongly typed (LISP and Scheme are essentially

typeless) – Uses less parenthesis, the syntax is closer to Pascal

than to LISP• Haskell (1990)

– Similar to ML (static scoped, strongly typed, type inferencing)

– Different from ML in that it is purely functional (e.g., no variables, no assignment statements)

Copied and altered from textbook “Concepts of Programming Languages”

Page 48: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

• Erlang (1986)– Developed by Ericsson– Originally Ericsson's proprietary language, released as

open source in 1998– Supporting soft-real-time and hop swapping

• Scala (2003)– Run on JVM and is compatible with Java programs

• F# (2005?)– Developed by Microsoft Research– Supported by .Net framework and Visual Studio

Page 49: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

LISP / Common LISP

Page 50: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Getting Started• In this lecture, we will be using GNU Common LISP

(GCL) interpreter– The development of the original LISP language has been

frozen for a long time– There are many implementations for Common LISP

interpreter• CLISP, CMUCL, GNU Common Lisp, Emacs Lisp, …etc

– The syntax of Common LISP is very much the same with the original LISP (, comparing with Scheme)

• Downloading :– GNU Common LISP

ftp://ftp.gnu.org/pub/gnu/gcl/binaries/stable/

Page 51: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

LISP Data Types and Structures• There were two types of data objects in the

original LISP:– Atoms– Lists

• List form: – parenthesized collections of sub-lists and/or atoms– e.g., (A B C), (A B (C D) E)

• Original LISP was a typeless language– Numbers and symbols…

• LISP lists are stored internally as single-linked lists

Copied and altered from textbook “Concepts of Programming Languages”

Page 52: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

LISP Functions• A function is expressed in the same way that

data is expressed• Function form:

– (func_name arg_1 … arg_n)– E.g. (A B C), (+ 5 7)

• If the list (A B C) is interpreted as data – it is a simple list of three atoms, A, B, and C

• If it is interpreted as a function application– it means that the function named A is applied to the two

parameters, B and C

• The value of the last expression in the body is the value of the function

Copied and altered from textbook “Concepts of Programming Languages”

Page 53: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Arithmetic Functions

• +, -, *, /, ABS, SQRT, MOD, REMAINDER, MIN, MAX

– expression value42 42(+ 5 7) 12(+ 5 7 8) 20(- 15 7 2) 6(- 24 (* 4 3)) 12

Copied and altered from textbook “Concepts of Programming Languages”

Page 54: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

EVAL, QUOTE

• EVAL– A function that can evaluate any other functions– An implementation of EVAL could serve as a LISP

interpreter• QUOTE

– It returns the parameter without evaluation– QUOTE is required because the interpreter, i.e.

EVAL, always evaluates the parameters. QUOTE is used to avoid parameter evaluation when it is not appropriate

– QUOTE can be abbreviated with the apostrophe prefix operator. '(A B) is equivalent to (QUOTE (A B))

Copied and altered from textbook “Concepts of Programming Languages”

Page 55: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

– expression return value(QUOTE A) A‘A A‘(A B C) (A B C)(CAR (A B C)) error

(CAR ‘(A B C)) A(CAR (+ 5 7)) error(CAR ‘(+ 5 7)) +

Copied and altered from textbook “Concepts of Programming Languages”

Page 56: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

List Functions: CAR, CDR• CAR

– takes a list parameter; returns the first element of that list– e.g., (CAR '(A B C)) yields A(CAR '((A B) C D)) yields (A B)

• CDR – takes a list parameter; returns the list after removing its

first element– e.g. (CDR '(A B C)) yields (B C)(CDR '((A B) C D)) yields (C D)

Copied and altered from textbook “Concepts of Programming Languages”

Page 57: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

• The naming of CAR, CDR…– The two functions are implemented in the 1st LISP

interpreter, on an IBM 704 mainframe, by Steve Russell

– They evolved from two IBM 704 assembly language macros

• CAR: Contents of Address Register• CDR: Contents of Data Register

Page 58: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

• Repetitions(compositions) of CAR and CDR– C????R

• Both Common Lisp and Scheme supports up to 4 repetitions of A or D– E.g. (CADDDR ‘(1 2 3 4 5))

equals(CAR (CDR (CDR (CDR ‘(1 2 3 4 5)))))

Page 59: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

List Functions: CONS, LIST• CONS

– takes two parameters, the first of which can be either an atom or a list and the second of which is a list;

– returns a new list that includes the first parameter as its first element and the second parameter as the remainder of its result

– e.g. (CONS 'A '(B C)) returns (A B C)• LIST

– takes any number of parameters; returns a list with the parameters as elements

– e.g. (LIST A B C) returns (A B C)

Copied and altered from textbook “Concepts of Programming Languages”

Page 60: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Predicate Function: EQ

• EQ– takes two symbolic parameters; it returns T if

both parameters are atoms and the two are the same; otherwise NIL

– e.g., (EQ 'A 'A) yields T(EQ 'A 'B) yields NIL(EQ 2 2) yields T

Copied and altered from textbook “Concepts of Programming Languages”

Page 61: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Numeric Predicate Functions

• Comparison of numbers– =, />, >, <, >=, <=– e.g., (= 2 2) yields T(= ‘A ‘A) yields error

• Type predicates for numbers– ZEROP, NUMBERP, EVENP, ODDP, INTEGERP, FLOATP

Copied and altered from textbook “Concepts of Programming Languages”

Page 62: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

List Predicate Functions

• NULL– takes one parameter; it returns T if the parameter

is an empty list; otherwise NIL

• ATOM – takes one parameter; it returns T if the parameter

is an atom; otherwise NIL

• LISTP– takes one parameter; it returns T if the parameter

is a list; otherwise NILCopied and altered from textbook “Concepts of Programming Languages”

Page 63: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Defining Symbols: SETQ

• SETQ– To set value to a symbol– E.g.> (SETQ a 5)5> a5> (+ a 5)10

Copied and altered from textbook “Concepts of Programming Languages”

Page 64: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Functions for Constructing Functions,: DEFUN, LAMBDA

• DEFUN– A Function for Constructing Functions– Forms:

(DEFUN func_name (para_name) (func_body) )– Ex1.

> (DEFUN cube (x) (* x x x))> (cube 2)8

Ex2.> (DEFUN 2sides (x) (CONS (CAR x) (LAST x)))> (2sides ‘(1 2 3 4 5))(1 5)

Copied and altered from textbook “Concepts of Programming Languages”

Page 65: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

• LAMBDA– E.g.> ((LAMBDA (x) (* x x x)) 2)

8

Copied and altered from textbook “Concepts of Programming Languages”

Page 66: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Defining Symbol Within Function: LET

• LET– To set value to a symbol temporarily within a function– E.g.> (DEFUN f (x)

(LET (i 2) (j 3))(* x i j))

• LET*– It performs for each variable immediately– E.g.> (DEFUN f (x)

(LET* (i 2) (j (+ i 1)))(* x i j))

Copied and altered from textbook “Concepts of Programming Languages”

Page 67: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Control Flow: IF

• IF– Forms:

(IF predicate than_body else_body)– E.g.> (DEFUN avg (total count)

(IF (= count 0)

0

(/ total count)))

> (avg 100 5)

20

Copied and altered from textbook “Concepts of Programming Languages”

Page 68: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Control Flow: COND• COND

– Forms:(COND(predicate_1 body_1)(predicate_2 body_2)...)

– Returns the value of the last expression in the first pair whose predicate evaluates to true

Copied and altered from textbook “Concepts of Programming Languages”

Page 69: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

– Ex1:> (COND (nil 1) (t 2) (nil 3))2

– Ex2:> (COND (t 1 2 3))3

– Ex3:> (DEFUN compare (x y) (COND ((> x y) “x is larger”) ((< x y) “y is larger”)))> (compare 1 2)“x is larger”

Copied and altered from textbook “Concepts of Programming Languages”

Page 70: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Functional Forms• Composition

– It has been used in previous examples– E.g.> (CDR (CDR '(A B C)))(C)

• Apply to All – LISP function name: MAPCAR – Applies the a function to all elements in a list– E.g.> (MAPCAR ‘CAR '((a b) (c d) (e f)))(A C E)

• Reduce– LISP function name: REDUCE– E.g.> (REDUCE ‘+ '(1 2 3))6

Page 71: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Lazy Evaluation

• Lazy evaluation, a.k.a. delayed evaluation– Such as LISP, Scheme, Haskell– Do not compute the result until they are required

• Opposite: eager evaluation, a.k.a. greedy evaluation– Such as Erlang

Copied and altered from textbook “Concepts of Programming Languages”

Page 72: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Tail Recursion

• Definition: A function is tail recursive if its recursive call is the last operation in the function

• A tail recursive function can be automatically converted by a compiler to use iteration, making it faster

Copied and altered from textbook “Concepts of Programming Languages”

Page 73: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Factorial

• Factorial (recursive)> (DEFUN factorial (n)

(IF (= n 0)

1

(* n (factorial (- n 1)))))

> (factorial 3)

6

Copied and altered from textbook http://www.psg.com/~dlamkins/sl/chapter04.html

Page 74: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

call factorial(3) call factorial(2) call factorial(1) call factorial(0) return 1; return 1*1 = 1 return 2*1 = 2return 3*2 = 6

Copied and altered from textbook http://www.psg.com/~dlamkins/sl/chapter04.html

Page 75: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

• Factorial (tail-recursive)> (DEFUN factorial (n)

(factorial-helper n 1))

> (DEFUN factorial-helper (n product)

(COND

((= n 0) product)

(t

(factorial-helper (- n 1) (* product n)))))

Copied and altered from textbook http://www.psg.com/~dlamkins/sl/chapter04.html

Page 76: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

call factorial(3) call factorial-helper(3 1) call factorial-helper(2 3) call factorial-helper(1 6) call factorial-helper(0 6) return 6 return 6 return 6 return 6return 6

Copied and altered from textbook http://www.psg.com/~dlamkins/sl/chapter04.html

Page 77: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Functions That Build Code• We can write a function in LISP, which

generates some LISP codes, and then request to interpret those codes

• This is possible because the interpreter is a user-available function, EVAL

Copied and altered from textbook “Concepts of Programming Languages”

Page 78: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Adding a List of Numbers

• Recursive> (DEFUN add (li) (IF (NULL li) 0 (+ (CAR li) (add (CDR li)))))> (add ‘(1 2 3))6

Copied and altered from textbook “Concepts of Programming Languages”

Page 79: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

• By EVAL > (DEFUN add (li) (COND ((NULL li) 0) (t (EVAL (CONS '+ li)))))

– It inserts a + operator to the list, and submit the new list to evaluate the list

Copied and altered from textbook “Concepts of Programming Languages”

Page 80: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

Some LISP Sample Programs

Page 81: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

APPEND

• my-append– It takes two lists; returns the first parameter list with

the elements of the second parameter list appended at the end

> (DEFUN my-append (li1 li2)

(IF (NULL li1)

li2

(CONS (CAR li1) (my-append (CDR li1) li2))))

> (my-append ‘(1 2) ‘(3 4))

(1 2 3 4)

Copied and altered from textbook “Concepts of Programming Languages”

Page 82: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

REVERSE

• my-reverse– It takes a lists; returns a list that reverses the order of

the elements in the given list> (DEFUN my-reverse (li)

(IF (NULL li)

'()

(APPEND (my-reverse (CDR li)) (LIST (CAR li)))))

> (my-reverse ‘(1 2 3))

(3 2 1)

Copied and altered from textbook “Concepts of Programming Languages”

Page 83: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

CONTAINS

• contains– It takes a simple list and an atom as parameters;

returns T if the atom is in the list; otherwise returns NIL

> (DEFUN contains (li a)

(COND

((NULL li) nil)

((EQ a (CAR li)) t)

(t (contains (CDR li) a))))

> (contains ‘(a b c) b)

T Copied and altered from textbook “Concepts of Programming Languages”

Page 84: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

EQLI

• eqli– It takes two simple lists; returns whether the two

lists are equal > (DEFUN eqli (li1 li2)

(COND

((NULL li1) (NULL li2))

((NULL li2) nil)

((EQ (CAR li1) (CAR li2))

(eqli (CDR li1) (CDR li2)))

(t nil)))

Copied and altered from textbook “Concepts of Programming Languages”

Page 85: Ch.15 Functional Programming Languages 清華大學資工系 孫文宏 CS2403 Programming Languages Spring, 2011

EQLI-GEN

• eqli-gen– It takes two general lists; returns whether the two

lists are equal> (DEFUN eqli-gen (li1 li2)

(COND

((ATOM li1) (EQ li1 li2))

((ATOM li2) nil)

((NULL li1) (NULL li2))

((NULL li2) nil)

((eqli-gen (CAR li1) (CAR li2))

(eqli-gen (CDR li1) (CDR li2)))

(t nil)))Copied and altered from textbook “Concepts of Programming Languages”