introduction to functional programmingmgv/csce330f09/lecturenotes/siskind...introduction to...

32
Introduction to Functional Programming Jeffrey Mark Siskind [email protected] School of Electrical and Computer Engineering Purdue University University of South Carolina 14 September 2009 Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 1 / 32

Upload: others

Post on 23-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Introduction to Functional Programming

Jeffrey Mark [email protected]

School of Electrical and Computer EngineeringPurdue University

University of South Carolina14 September 2009

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 1 / 32

Page 2: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Scheme

SCHEME programs are built out of expressions.

Expressions are evaluated.

=⇒ means evaluates to.

(+ 1 2) =⇒ 3

(- 1 2) =⇒ -1

(+ (* 2 3) 4) =⇒ 10

(/ 10 5) =⇒ 2

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 2 / 32

Page 3: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

REPL: Read-Eval-Print Loop

Scheme is an interactive language.

Scheme->C -- 15mar93jfb> (+ 1 2)3> (- 1 2)-1> (+ (* 2 3) 4)10> (/ 10 5)2>

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 3 / 32

Page 4: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Definitions—I

> (sqrt (+ (sqr (- 4 3)) (sqr (- 9 8))))1.414213562373095> (sqrt (+ (sqr (- 8 2)) (sqr (- 9 8))))6.082762530298219> (define (distance x1 y1 x2 y2)

(sqrt (+ (sqr (- x2 x1)) (sqr (- y2 y1)))))> (distance 3 8 4 9)1.414213562373095> (distance 2 8 8 9)6.082762530298219>

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 4 / 32

Page 5: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Definitions—II

Put in a file then load with c-z l

(define (factorial n)(if (= n 0)

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

> (factorial 5)120

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 5 / 32

Page 6: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Recursion as Induction

0! = 1 base casen! = n× (n− 1)! inductive case

(define (factorial n)(if (= n 0)

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

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 6 / 32

Page 7: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Peano Arithmetic—I

n+ = n + 1

n− = n− 1

0

(define (increment n) (+ n 1))

(define (decrement n) (- n 1))

0

(zero? n)

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 7 / 32

Page 8: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Peano Arithmetic—II

m + 0 = m base casem + n = (m + n−)+ inductive case

(define (+ m n)(if (zero? n)

m(increment (+ m (decrement n)))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 8 / 32

Page 9: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Peano Arithmetic—III

m− 0 = m base casem− n = (m− n−)− inductive case

(define (- m n)(if (zero? n)

m(decrement (- m (decrement n)))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 9 / 32

Page 10: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Peano Arithmetic—IV

m× 0 = 0 base casem× n = (m× n−) + m inductive case

(define (* m n)(if (zero? n)

0(+ (* m (decrement n)) m)))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 10 / 32

Page 11: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Peano Arithmetic—V

0n = 0 base casemn =

(m−nn

)+ inductive case

(define (/ m n)(if (zero? m)

0(increment (/ (- m n) n))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 11 / 32

Page 12: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Peano Arithmetic–VI

m 6< 0 base case0 < n base case

m− < n− → m < n inductive case

(define (< m n)(if (zero? n)

#f(if (zero? m)

#t(< (decrement m) (decrement n)))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 12 / 32

Page 13: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

COND Syntax

(cond (p1 e1)(p2 e2)...(pn en)(else e))

;

(if p1e1(if p2

e2...(if pn

en

e). . .))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 13 / 32

Page 14: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Uses of COND

(define (< m n)(if (zero? n)

#f(if (zero? m)

#t(< (decrement m) (decrement n)))))

(define (< m n)(cond ((zero? n) #f))

((zero? m) #t)(else (< (decrement m) (decrement n))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 14 / 32

Page 15: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Syntax vs. Semantics

Procedures extend the semantics of a language.

Procedures are evaluated via =⇒.

Macros extend the syntax of a language.

Macros rewrite expressions to other expressions.

; means rewrites to.

SCHEME contains both builtin syntax and builtin semantics.

Users can define new procedures to extend the semantics of SCHEME.

It is possible for the user to define new syntax via macros. We willnot have need for this in this course. So I will not teach how. Seethe manual for details.

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 15 / 32

Page 16: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Lists—I

(list 1 2 3) =⇒ (1 2 3)

(first (list 1 2 3)) =⇒ 1

(rest (list 1 2 3)) =⇒ (2 3)

(first (rest (list 1 2 3))) =⇒ 2

(rest (rest (list 1 2 3))) =⇒ (3)

(rest (rest (rest (list 1 2 3)))) =⇒ ()

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 16 / 32

Page 17: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

Lists—II

(list) =⇒ ()

(null? (list)) =⇒ #t

(null? (list 1)) =⇒ #f

(null? (list (list))) =⇒ #f

(cons 1 (list 2 3)) =⇒ (1 2 3)

(cons 1 (list)) =⇒ (1)

(cons (list 1 2) (list 3 4)) =⇒ ((1 2) 3 4)

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 17 / 32

Page 18: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—I

(define (length list)(if (null? list)

0(+ (length (rest list)) 1)))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 18 / 32

Page 19: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—II

(define (list-ref list i)(if (= i 0)

(first list)(list-ref (rest list) (- i 1))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 19 / 32

Page 20: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—III

(define (list-replace-ith list i x)(if (= i 0)

(cons x (rest list))(cons (first list)

(list-replace-ith(rest list) (- i 1) x))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 20 / 32

Page 21: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—IV

(define (list-insert-ith list i x)(if (= i 0)

(cons x list)(cons (first list)

(list-insert-ith(rest list) (- i 1) x))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 21 / 32

Page 22: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—V

(define (list-remove-ith list i)(if (= i 0)

(rest list)(cons (first list)

(list-remove-ith(rest list) (- i 1)))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 22 / 32

Page 23: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—VI

(define (member? x list)(cond ((null? list) #f)

((= x (first list)) #t)(else (member? x (rest list)))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 23 / 32

Page 24: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—VII

(define (position x list)(cond((null? list) (panic "Element not found"))((= (first list) x) 0)(else (+ (position x (rest list)) 1))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 24 / 32

Page 25: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—VIII

(define (remove-one x list)(cond ((null? list) (list))

((= (first list) x) (rest list))(else (cons (first list)

(remove-one x (rest list))))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 25 / 32

Page 26: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—IX

(define (remove-all x list)(cond((null? list) (list))((= (first list) x) (remove-all x (rest list)))(else (cons (first list)

(remove-all x (rest list))))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 26 / 32

Page 27: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—X

(define (replace-one x y list)(cond((null? list) (list))((= (first list) x) (cons y (rest list)))(else (cons (first list)

(replace-one x y (rest list))))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 27 / 32

Page 28: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—XI

(define (replace-all x y list)(cond((null? list) (list))((= (first list) x)(cons y (replace-all x y (rest list))))

(else (cons (first list)(replace-all x y (rest list))))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 28 / 32

Page 29: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—XII

(define (append list1 list2)(if (null? list1)

list2(cons (first list1)

(append (rest list1) list2))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 29 / 32

Page 30: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—XIII

(define (reverse l)(if (null? l)

(list)(append (reverse (rest l))

(list (first l)))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 30 / 32

Page 31: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—XIV

(define (sum list)(if (null? list)

0(+ (first list) (sum (rest list)))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 31 / 32

Page 32: Introduction to Functional Programmingmgv/csce330f09/lectureNotes/Siskind...Introduction to Functional Programming Jeffrey Mark Siskind qobi@purdue.edu School of Electrical and Computer

List Processing—XV

(define (product list)(if (null? list)

1(* (first list) (product (rest list)))))

Jeffrey Mark Siskind (Purdue/ECE) Functional Programming SC September 2009 32 / 32