Programming Languages 2nd edition Tucker and Noonan

Slides:



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

Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
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.
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.
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 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 10 Function Implementation In theory, there.
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.
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 14 Functional Programming It is better to.
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.
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.
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.
Semantics. Semantics is a precise definition of the meaning of a syntactically and type-wise correct program. Ideas of meaning: –Operational Semantics.
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.
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
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.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
CS314 – Section 5 Recitation 9
Additional Scheme examples
Functional Programming Languages
CS 550 Programming Languages Jeremy Johnson
Section 15.4, 15.6 plus other materials
Semantic Analysis Chapter 4.
A Simple Syntax-Directed Translator
Propositional Calculus: Boolean Functions and Expressions
Introduction to Scheme
CSC 533: Programming Languages Spring 2017
Chapter 15 – Functional Programming Languages
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: Programming Languages Spring 2016
CSC 533: Organization of Programming Languages Spring 2008
COP4020 Programming Languages
Original material by Eric Grimson
Propositional Calculus: Boolean Functions and Expressions
Propositional Calculus: Boolean Algebra and Simplification
Programming Languages 2nd edition Tucker and Noonan
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Data abstraction, revisited
The Metacircular Evaluator
Programming Languages 2nd edition Tucker and Noonan
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Announcements Quiz 5 HW6 due October 23
COP4020 Programming Languages
CSC 533: Organization of Programming Languages Spring 2007
Programming Languages 2nd edition Tucker and Noonan
Presentation transcript:

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

Contents 14.1 Functions and the Lambda Calculus 14.2 Scheme 14.2.1 Expressions 14.2.2 Expression Evaluation 14.2.3 Lists 14.2.4 Elementary Values 14.2.5 Control Flow 14.2.6 Defining Functions 14.2.7 Let Expressions 14.2.8 Example: Semantics of Clite 14.2.9 Example: Symbolic Differentiation 14.2.10 Example: Eight Queens 14.3 Haskell

14.2.8 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

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))

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)

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 ()) ))

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

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)) …

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?

14.2.9 Example: Symbolic Differentiation Symbolic Differentiation Rules Fig 14.2

Scheme Encoding Uses Cambridge Prefix notation E.g., 2x + 1 is written as (+ (* 2 x) 1) Function diff incorporates these rules. E.g., (diff ‘x ‘(+ (* 2 x) 1)) should give an answer. 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.

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))) ))))

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))) = (list ‘+ (list ‘+ ‘(* 2 1) (list ‘* x (diff ‘x 2))) = (list ‘+ (list ‘+ ‘(* 2 1) (list ‘* x 0)) 0) = ‘(+ (+ (* 2 1) (* x 0)) 0)

14.2.10 Example: Eight Queens A backtracking algorithm for which each trial move’s: Row must not be occupied, Row and column’s SW diagonal must not be occupied, and 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).

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.

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)

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)))

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.

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) (cons (seDiag (car alist) (length alist)) (selist (cdr alist)))))