Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
SICP Data abstraction revisited Data structures: association list, vector, hash table Table abstract data type No implementation of an ADT is necessarily.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 6: Type Systems Fall 2009.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Types I was eventually persuaded of the need to design programming notations so as to maximize.
Functional Programming Chapter 14. History of Functional Languages Lisp is the second oldest language Motivated by a need to do symbolic, rather than.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
COSC2007 Data Structures II
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
Programming Languages 2nd edition Tucker and Noonan Chapter 6 Type Systems I was eventually persuaded of the need to design programming notations so as.
Chapter 3 Part II Describing Syntax and Semantics.
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
Copyright © 2007 Pearson Education, Inc. Slide R-1.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 8 Semantic Interpretation To understand a.
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
Introduction to Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
CSC 533: Organization of Programming Languages Spring 2008
Programming Languages 2nd edition Tucker and Noonan
Programming Languages 2nd edition Tucker and Noonan
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
The Metacircular Evaluator
Programming Languages 2nd edition Tucker and Noonan
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
CSC 533: Organization of Programming Languages Spring 2007
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Copyright © 2006 The McGraw-Hill Companies, Inc. Contents 14.1 Functions and the Lambda Calculus 14.2 Scheme Expressions Expression Evaluation Lists Elementary Values Control Flow Defining Functions Let Expressions Example: Semantics of Clite Example: Symbolic Differentiation Example: Eight Queens 14.3 Haskell

Copyright © 2006 The McGraw-Hill Companies, Inc Example: Semantics of Clite Program state can be modeled as a list of pairs. E.g., ((x 1) (y 5)) Function to retrieve the value of a variable from the state: (define (get id state) (if (equal? id (caar state)) (cadar state) (get id (cdr state)) )) E.g., (get ‘y ‘((x 5) (y 3) (z 1))) = (get ‘y ‘((y 3) (z 1))) = 3

Copyright © 2006 The McGraw-Hill Companies, Inc. State transformation Function to store a new value for a variable in the state: (define (onion id val state) (if (equal? id (caar state)) (cons (list id val) (cdr state)) (cons (car state) (onion id val (cdr state))) )) E.g., (onion ‘y 4 ‘((x 5) (y 3) (z 1))) = (cons ‘(x 5) (onion ‘y 4 ‘((y 3) z 1))) = (cons ‘(x 5) (cons ‘(y 4) ‘((z 1)))) = ‘((x 5) (y 4) (z 1))

Copyright © 2006 The McGraw-Hill Companies, Inc. Modeling Clite Abstract Syntax Skip(skip) Assignment(assignment target source) Block(block s1 s2 … sn) Loop(loop test body) Conditional(conditional test thenbranch elsebranch) Expression Value(value val) Variable(variable id) Binary(operator term1 term2)

Copyright © 2006 The McGraw-Hill Companies, Inc. Semantics of Statements (define (m-statement statement state) (case (car statement) ((skip) (m-skip statement state)) ((assignment) (m-assignment statement state)) ((block) (m-block (cdr statement) state)) ((loop) (m-loop statement state) ((conditional) (m-conditional statement state)) (else ()) ))

Copyright © 2006 The McGraw-Hill Companies, Inc. Skip, Block, and Loop (define (m-skip statement state) state) (define (m-block alist state) (if (null? alist) state (m-block (cdr alist) (m-statement (car alist) state)) )) (define (m-loop) statement state) (if (m-expression (car statement) state) (m-loop statement (m-statement (cdr statement) state)) state ))

Copyright © 2006 The McGraw-Hill Companies, Inc. Expression (define (m-expression expr state) (case (car expr) ((value) (cadr expr)) ((variable) (get (cadr expr) state)) (else (applyBinary (car expr) (cadr expr) (caddr expr) state)) )) (define (applyBinary) op left right state) (let ((leftval (m-expression left state)) ((rightval (m-expression right state))) (case op ((plus) (+ leftval rightval)) … ))

Copyright © 2006 The McGraw-Hill Companies, Inc. To Do: 1. Show that these definitions give 5 as the meaning of y+2 in the state ((x 5) (y 3) (z 1)). I.e., show that (m-expression ‘(plus (variable y) (value 2)) ‘((x 5) (y 3) (z 1))) … = 5 2. Give a definition of m-assignment. 3. What about defining m-conditional?

Copyright © 2006 The McGraw-Hill Companies, Inc Example: Symbolic Differentiation Symbolic Differentiation Rules Fig 14.2

Copyright © 2006 The McGraw-Hill Companies, Inc. Scheme Encoding 1.Uses Cambridge Prefix notation E.g., 2x + 1 is written as (+ (* 2 x) 1) 2.Function diff incorporates these rules. E.g., (diff ‘x ‘(+ (* 2 x) 1)) should give an answer. 3.However, no simplification is performed. E.g. the answer for (diff ‘x ‘(+ (* 2 x) 1)) is (+ (+ (* 2 1) (* x 0)) 0) which is equivalent to the simplified answer, 2.

Copyright © 2006 The McGraw-Hill Companies, Inc. Scheme Program (define (diff x expr) (if (not (list? Expr)) (if (equal? x expr) 1 0) (let ((u (cadr expr)) (v (caddr expr))) (case (car expr) ((+) (list ‘+ (diff x u) (diff x v))) ((-) (list ‘- (diff x u) (diff x v))) ((*) (list ‘+ (list ‘* u (diff x v)) (list ‘* v (diff x u)))) ((/) (list ‘div (list ‘- (list ‘* v (diff x u)) (list ‘* u (diff x v))) (list ‘* u v))) ))))

Copyright © 2006 The McGraw-Hill Companies, Inc. Trace of the Example (diff ‘x ‘(+ ‘(* 2 x) 1)) = (list ‘+ (diff ‘x ‘(*2 x)) (diff ‘x 1)) = (list ‘+ (list ‘+ (list ‘* 2 (diff ‘x ‘x)) (list ‘* x (diff ‘x 2))) (diff ‘x 1)) = (list ‘+ (list ‘+ (list ‘* 2 1) (list ‘* x (diff ‘x 2))) (diff ‘x 1)) = (list ‘+ (list ‘+ ‘(* 2 1) (list ‘* x (diff ‘x 2))) (diff ‘x 1)) = (list ‘+ (list ‘+ ‘(* 2 1) (list ‘* x (diff ‘x 2))) (diff ‘x 1)) = (list ‘+ (list ‘+ ‘(* 2 1) (list ‘* x 0)) 0) = ‘(+ (+ (* 2 1) (* x 0)) 0)

Copyright © 2006 The McGraw-Hill Companies, Inc Example: Eight Queens A backtracking algorithm for which each trial move’s: 1.Row must not be occupied, 2.Row and column’s SW diagonal must not be occupied, and 3.Row and column’s SE diagonal must not be occupied. If a trial move fails any of these tests, the program backtracks and tries another. The process continues until each row has a queen (or until all moves have been tried).

Copyright © 2006 The McGraw-Hill Companies, Inc. Checking for a Valid Move (define (valid move soln) (let ((col (length (cons move soln)))) (and (not (member move soln)) (not (member (seDiag move col) (selist soln))) (not (member (swDiag move col) (swlist soln))) ))) Note: the and encodes the three rules listed on the previous slide.

Copyright © 2006 The McGraw-Hill Companies, Inc. Representing the Developing Solution Positions of the queens kept in a list soln whose nth entry gives the row position of the queen in column n, in reverse order. E.g., soln = (5 3 1) represents queens in (row, column) positions (1,1), (3,2), and (5,3); i.e., see previous slide. End of the game occurs when soln has N (= 8) entries: (define (done soln) (>= (length soln) N)) Continuing the game tests hasmore and generates nextmove : (define (hasmore move) (<= move N)) (define (nextmove move) (+ move 1)

Copyright © 2006 The McGraw-Hill Companies, Inc. Generating Trial Moves (define (trywh move soln) (if (and (hasmore move) (not (car soln))) (let ((atry (tryone move (cdr soln)))) (if (car atry) atry (trywh (nextmove move) soln)) ) soln )) The try function sets up the first move: (define (try soln) (trywh 1 (cons #f soln)))

Copyright © 2006 The McGraw-Hill Companies, Inc. Trying One Move (define (tryone move soln) (let ((xsoln (cons move soln))) (if (valid move soln) (if (done xsoln) (cons #t xsoln) (try xsoln)) (cons #f soln)) )) Note: the #t or #f reveals whether the solution is complete.

Copyright © 2006 The McGraw-Hill Companies, Inc. SW and SE Diagonals (define (swdiag row col) (+ row col)) (define (sediag row col) (- row col)) (define (swlist alist) (if (null? Alist) ‘() (cons (swDiag (car alist) (length alist)) (swlist (cdr alist))))) (define (selist alist) (if (null? Alist) ‘() (cons (seDiag (car alist) (length alist)) (selist (cdr alist)))))