Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
10/5/2015 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 Lisp Copyright © 2006 The McGraw-Hill Companies, Inc. 14-2 1 10/5/2015 Overview of Functional Languages • They emerged in the 1960’s with Lisp • Functional programming mirrors mathematical functions: domain = input, range = output • Computation is viewed as a mathematical function mapping inputs to outputs. • Pure functional programming is state-free: no assignment • Referential transparency: a function’s result depends only upon the values of its parameters. Copyright © 2006 The McGraw-Hill Companies, Inc. 14-3 14.1 Functions and the Lambda Calculus The function Square has R (the reals) as domain and range. Square : R R Square(n) = n2 A function is total if it is defined for all values of its domain. Otherwise, it is partial. E.g., Square is total. A lambda expression is a particular way to define a function: LambdaExpression variable | ( M N) | ( variable . M ) M LambdaExpression N LambdaExpression E.g., ( x . x2 ) represents the Square function. Copyright © 2006 The McGraw-Hill Companies, Inc. 14-4 2 10/5/2015 Properties of Lambda Expressions In ( x . M), x is bound. Other variables in M are free. A substitution of N for all occurrences of a variable x in M is written M[x N]. Examples: Copyright © 2006 The McGraw-Hill Companies, Inc. 14-5 Lambda Expressions Lambda expressions describe nameless functions Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression e.g. (x . x * x * x) 2 → 2 * 2 * 2 → 8 The process of evaluating a lambda expression is called beta (β)-reduction. A lambda expression which may be reduced is called a redex (reducible expression). A lambda expression which may not be further reduced (i.e., has no redexes) is said to be in normal form. Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-6 3 10/5/2015 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 (x . y . x + y) 3 4 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-7 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 (x . y . x + y) 3 4 rede x 15-7b Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-8 4 10/5/2015 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 (x . y . x + y) 3 4 15-7c Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-9 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 15-7d Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-10 5 10/5/2015 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 Result is a function! 15-7e Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-11 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 rede x 15-7f Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-12 6 10/5/2015 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 15-7g Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-13 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 15-7h Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-14 7 10/5/2015 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 →7 15-7i Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-15 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 →7 (x . y . x + y) 3 → y . 3 + y 15-7j Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-16 8 10/5/2015 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 →7 (x . y . x + y) 3 → y . 3 + y (f . x . f x) (y . 3 + y) 4 15-7k Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-17 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 →7 (x . y . x + y) 3 → y . 3 + y (f . x . f x) (y . 3 + y) 4 rede 15-7l x Concepts of Programming Languages, 9th ed., by Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 The McGraw-Hill Companies, Inc. 14-18 9 10/5/2015 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 →7 (x . y . x + y) 3 → y . 3 + y (f . x . f x) (y . 3 + y) 4 15-7m Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-19 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 →7 (x . y . x + y) 3 → y . 3 + y (f . x . f x) (y . 3 + y) 4 → (x . (y . 3 + y) x) 4 15-7n Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-20 10 10/5/2015 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 →7 (x . y . x + y) 3 → y . 3 + y (f . x . f x) (y . 3 + y) 4 → (x . (y . 3 + y) x) 4 15-7o Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-21 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 (x . y . x + y) 3 4 → (y . 3 + y) 4 →3+4 →7 (x . y . x + y) 3 → y . 3 + y (f . x . f x) (y . 3 + y) 4 → (x . (y . 3 + y) x) 4 → (y . 3 + y) 4 15-7p Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-22 11 10/5/2015 Non-Terminating Lambda Expression (x . x x) (x . x x) → (x . x x) (x . x x) → (x . x x) (x . x x) →… Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-23 Function Evaluation Lazy evaluation = delaying argument evaluation in a function call until the argument is needed. – Advantage: flexibility Eager evaluation = evaluating arguments at the beginning of the call. – Advantage: efficiency Copyright © 2006 The McGraw-Hill Companies, Inc. 14-29 12 10/5/2015 Status of Functions In imperative and OO programming, functions have different (lower) status than variables. In functional programming, functions have same status as variables; they are first-class entities. – They can be passed as arguments in a call. – They can transform other functions. A function that operates on other functions is called a functional form. E.g., we can define g(f, [x1, x2, … ]) = [f(x1), f(x2), …], so that g(Square, [2, 3, 5]) = [4, 9, 25] Copyright © 2006 The McGraw-Hill Companies, Inc. 14-30 14.2 Lisp LISP is an acronym for LISt Processor LISP was invented by John McCarthy of MIT (later Stanford) in 1958 Data object types: originally only atoms and lists List form: parenthesized collections of sublists and/or atoms e.g., (A B (C D) E) Originally, LISP was a typeless language LISP lists are stored internally as singlelinked lists Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-31 13 10/5/2015 LISP Interpretation Lambda notation is used to specify functions and function definitions. Function applications and data have the same form. e.g., 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 first LISP interpreter appeared only as a demonstration of the universality of the computational capabilities of the notation 15-13 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-32 Primitive Functions Arithmetic: +, -, *, /, ABS, SQRT, REMAINDER, MIN, MAX e.g., (+ 5 2) yields 7 QUOTE - takes one parameter; returns the parameter without evaluation – – QUOTE is required because the Lisp interpreter, named EVAL, always evaluates parameters to function applications before applying the function. 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)) 15-16 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-33 14 10/5/2015 Running Lisp GCL – Gnu Common Lisp Path - /usr/bin/gcl Example brb0164@faculty% gcl GCL (GNU Common Lisp) 2.6.7 CLtL1 Feb 1 2012 09:07:26 >(+ 5 2) 7 >'(A B) (A B) >(A B) Error in EVAL [or a callee]: The function A is undefined. >^D Copyright 2012 BarrettCompanies, R. Bryant. Copyright © 2006©The McGraw-Hill Inc. 14-34 Function Definition: DEFUN A Function for Constructing Functions DEFUN - To bind names to lambda expressions e.g., (DEFUN cube (x) (* x x x)) Example use: (cube 4) Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-35 15 10/5/2015 Lisp Example: cube brb0164@faculty% gcl GCL (GNU Common Lisp) 2.6.7 CLtL1 Feb 1 2012 09:07:26 >(DEFUN cube (x) (* x x x)) CUBE >(cube 4) 64 >^D Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-36 Function Definition: LAMBDA Lambda Expressions – Form is based on notation e.g., (LAMBDA (x) (* x x x)) x is called a bound variable Lambda expressions can be applied e.g., ((LAMBDA (x) (* x x x)) 4) ((LAMBDA (x y) (+ x y)) 3 4) ((LAMBDA (x) (LAMBDA (y) (+ x y))) 3) 15-17 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-37 16 10/5/2015 Lisp Example: LAMBDA brb0164@faculty% gcl GCL (GNU Common Lisp) 2.6.7 CLtL1 Feb 1 2012 09:07:26 >((LAMBDA (x) (* x x x)) 4) 64 >((LAMBDA (x y) (+ x y)) 3 4) 7 >^D Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-38 Evaluation Parameters are evaluated, in no particular order The values of the parameters are substituted into the function body The function body is evaluated The value of the last expression in the body is the value of the function 15-15 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-39 17 10/5/2015 Control Flow: COND Multiple Selection - the special form, COND General form: (COND (predicate_1 expr {expr}) (predicate_2 expr {expr}) ... (predicate_n expr {expr}) (T expr {expr})) Returns the value of the last expression in the first pair whose predicate evaluates to true 15-22 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-40 Lisp Example: min brb0164@faculty% cat min.lsp (defun min (x y) (cond ((<= x y) x) ((>= x y) y) ) ) brb0164@faculty% gcl GCL (GNU Common Lisp) 2.6.7 CLtL1 Feb 1 2012 09:07:26 >(load "min") Loading min.lsp Warning: MIN is being redefined. Finished loading min.lsp T >(min 17 45) 17 >(min 0 1) 0 >(min 65 3) 3 >^D Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-41 18 10/5/2015 Lisp Example: factorial brb0164@faculty% cat factorial.lsp (defun factorial (n) (cond ((= n 0) 1) (t (* n (factorial (- n 1)))) ) ) brb0164@faculty% gcl >(load "factorial") Loading factorial.lsp Finished loading factorial.lsp T >(factorial 5) 120 >(factorial 10) 3628800 >^D Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-42 Representation of Lists '(A B C) • • • • A B '((A B) C D) • • • • A ∘ • C • • ∘ C • • ∘ D B Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-43 19 10/5/2015 List Functions: CAR and CDR CAR takes a list parameter; returns element of that list e.g., (CAR '(A B C)) yields A (CAR '((A B) C D)) yields (A CDR takes a list parameter; returns after removing its first element e.g., (CDR '(A B C)) yields (B (CDR '((A B) C D)) yields (C the first B) the list C) D) 15-25 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-44 List Functions: CAR and CDR Extensions (CADR L) ≡ (CAR (CDR L)) e.g., (CADR '(A B C)) yields B (CADR '((A B) C D)) yields C (CDDR L) ≡ (CDR (CDR L)) e.g., (CDDR '(A B C)) yields (C) (CDDR '((A B) C D)) yields (D) (CAAR L) ≡ (CAR (CAR L)) e.g., (CAAR '(A B C)) yields an error (CAAR '((A B) C D)) yields A Any number of A’s and D’s may be combined e.g., (CADADR '((A B) (C D))) yields D Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-45 20 10/5/2015 List Predicates: NULL, ATOM and LISTP NULL returns true (T) if the argument is null (NIL) and false (NIL) otherwise. ATOM returns true (T) if the argument is an atom and false (NIL) otherwise. LISTP returns true (T) if the argument is a list and false (NIL) otherwise. 15-27 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-46 Lisp Example: length brb0164@faculty% cat length.lsp (defun length (list) (cond ((null list) 0) (t (+ 1 (length (cdr list)))) ) ) brb0164@faculty% gcl >(load "length") Loading length.lsp Warning: LENGTH is being redefined. Finished loading length.lsp T >(length '(a b c d)) >(length nil) 4 0 >(length '(a (b c))) >(length '(a (b c) d (e f (g h)))) 2 4 Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-47 21 10/5/2015 List Functions: CONS CONS takes two parameters, the first of which can be either an atom or a list and the second of which should be 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) (CONS 'A 'B) returns (A . B) • • A B Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-48 List Functions: LIST LIST takes any number of parameters; returns a list with the parameters as elements e.g., (LIST 'A 'B 'C) returns (A B C) (LIST '(A B C)) returns ((A B C)) 15-24b Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-49 22 10/5/2015 List Functions: APPEND APPEND takes two parameters, the first of which is a list and the second of which should be a list; returns a new list with the CDR of the last node in the first parameter being the second parameter e.g., (APPEND '(A) '(B C)) returns (A B C) Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-50 Defining Local Scope: PROG, RETURN and SETQ (PROG (VAR1 VAR2 … VARn) EXP1 EXP2 … EXPm) VAR1, VAR2, … VARn have local scope in EXP1, EXP2, … EXPm The variables may be assigned values using the (SETQ VAR EXP) function. The last expression in the body, EXPm, is returned using a RETURN function which may also appear anywhere an expression may be used inside the block (e.g., to return a different value). Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-51 23 10/5/2015 Lisp Examples: List Manipulation brb0164@faculty% gcl > (setq a (cons 'x nil)) (x) > (setq b (cons 'y a)) (y x) > (setq c (append b 'z)) (y x . z) > (car c) y > (cadr c) x > (cddr c) z > (setq d (append b '(z))) (y x z) > (setq e (cons d d)) ((y x z) y x z) > (setq f (append d d)) (y x z y x z) > (setq g (cons d (list d))) ((y x z) (y x z)) > (car g) (y x z) > (cdr g) ((y x z)) > (cadr g) (y x z) > (caadr g) y > (cdadr g) (x z) Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-52 Functions That Build Code It is possible in Lisp to define a function that builds Lisp code and requests its interpretation This is possible because the interpreter is a user-available function, EVAL A similar function is APPLY which takes two parameters, the first a function F and the second a list of parameters L, and applies F to L. 15-37 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-53 24 10/5/2015 Lisp Examples: Lambda Expression Manipulation brb0164@faculty% gcl >(setq add '(lambda (x y) (+ x y))) (LAMBDA (X Y) (+ X Y)) >(car add) >(cdadr add) LAMBDA (Y) >(cdr add) >(caddr add) ((X Y) (+ X Y)) (+ X Y) >(cadr add) >(caaddr add) (X Y) + >(cddr add) >(apply add '(3 4)) ((+ X Y)) 7 >(caadr add) >(eval '(apply add '(3 4))) X 7 Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-54 Lisp Example: postfix brb0164@faculty% cat postfix.lsp ; postfix converts a postfix expression, ; represented as a list of operands and ; operators, into a syntax tree. (defun postfix (exp) (car (postfixstack exp nil))) Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-55 25 10/5/2015 Lisp Example: postfix (defun postfixstack (exp stack) (cond ((null exp) stack) (t (cond ((eq (car exp) '+) (postfixstack (cdr exp) (cons (list '+ (cadr stack) (car stack)) (cddr stack)))) ((eq (car exp) '–) (postfixstack (cdr exp) (cons (list '– (cadr stack) (car stack)) (cddr stack)))) ((eq (car exp) '*) (postfixstack (cdr exp) (cons (list '* (cadr stack) (car stack)) (cddr stack)))) ((eq (car exp) '/) (postfixstack (cdr exp) (cons (list '/ (cadr stack) (car stack)) (cddr stack)))) (t (postfixstack (cdr exp) (cons (car exp) stack))) )))) Copyright © 2007 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-56 Lisp Example: postfix brb0164@faculty% gcl >(load "postfix") Loading postfix.lsp Finished loading postfix.lsp T >(postfix '(a b c * +)) (+ A (* B C)) >(postfix '(a b + c * d / e –)) (– (/ (* (+ A B) C) D) E) Copyright © 2012 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-57 26 10/5/2015 Lisp Example: postfix (cont) (defun postfix (exp) (car (postfixstack exp nil))) (postfix '(a b c * +)) (car (postfixstack '(a b c * +) nil)) Copyright © 2010 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-58 Lisp Example: postfix (cont) (defun postfixstack (exp stack) (cond ((null exp) stack) (t (cond ((eq (car exp) '+) (postfixstack (cdr exp) (cons (list '+ (cadr stack) (car stack)) (cddr stack)))) … (t (postfixstack (cdr exp) (cons (car exp) stack))) )))) (postfixstack '(a b c * +) nil) (postfixstack (cdr '(a b c * +)) (cons (car '(a b c * +)) nil)) Copyright © 2010 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-59 27 10/5/2015 Lisp Example: postfix (cont) (defun postfixstack (exp stack) (cond ((null exp) stack) (t (cond ((eq (car exp) '+) (postfixstack (cdr exp) (cons (list '+ (cadr stack) (car stack)) (cddr stack)))) … (t (postfixstack (cdr exp) (cons (car exp) stack))) )))) (postfixstack '(b c * +) '(a)) (postfixstack (cdr '(b c * +)) (cons (car '(b c * +)) '(a))) Copyright © 2010 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-60 Lisp Example: postfix (cont) (defun postfixstack (exp stack) (cond ((null exp) stack) (t (cond ((eq (car exp) '+) (postfixstack (cdr exp) (cons (list '+ (cadr stack) (car stack)) (cddr stack)))) … (t (postfixstack (cdr exp) (cons (car exp) stack))) )))) (postfixstack '(c * +) '(b a)) (postfixstack (cdr '(c * +)) (cons (car '(c * +)) '(b a))) Copyright © 2010 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-61 28 10/5/2015 Lisp Example: postfix (cont) (defun postfixstack (exp stack) (cond ((null exp) stack) (t (cond … ((eq (car exp) '*) (postfixstack (cdr exp) (cons (list '* (cadr stack) (car stack)) (cddr stack)))) … )))) (postfixstack '(* +) '(c b a)) (postfixstack (cdr '(* +)) (cons (list '* (cadr '(c b a)) (car '(c b a))) (cddr '(c b a))) Copyright © 2010 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-62 Lisp Example: postfix (cont) (defun postfixstack (exp stack) (cond ((null exp) stack) (t (cond ((eq (car exp) '+) (postfixstack (cdr exp) (cons (list '+ (cadr stack) (car stack)) (cddr stack)))) … (t (postfixstack (cdr exp) (cons (car exp) stack))) )))) (postfixstack '(+) '((* b c) a)) (postfixstack (cdr '(+)) (cons (list '+ (cadr '((* b c) a) (car '((* b c) a))) (cddr '((* b c) a)))) Copyright © 2010 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-63 29 10/5/2015 Lisp Example: postfix (cont) (defun postfixstack (exp stack) (cond ((null exp) stack) (t (cond ((eq (car exp) '+) (postfixstack (cdr exp) (cons (list '+ (cadr stack) (car stack)) (cddr stack)))) … (t (postfixstack (cdr exp) (cons (car exp) stack))) )))) (postfixstack nil '((+ a (* b c)))) Copyright © 2010 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-64 Lisp Example: postfix (cont) (defun postfix (exp) (car (postfixstack exp nil))) (postfix '(a b c * +)) (car (postfixstack '(a b c * +) nil)) … '(+ a (* b c)) Copyright © 2010 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-65 30 10/5/2015 Tail Recursion in Lisp 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 15-34 Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-66 14.2.8 Example: Semantics of Clite Semantics - the meaning of the expressions, statements, and program units Static semantics – compiler time “correctness” of semantics, i.e., type checking, shows correctness of position := initial + rate * 60 with type conversion of 60 to floating point Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-67 31 10/5/2015 14.2.8 Example: Semantics of Clite Dynamic semantics – run-time behavior - fetch the value of rate from memory - multiply by 60.0 - fetch the value of initial and add to previous result - store the result in position Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-68 14.2.8 Example: Semantics of Clite Operational Semantics – Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement. Concepts of Programming Languages, 9th ed., Robert W. Sebesta. Addison Wesley, 2010. Copyright © 2006 Theby McGraw-Hill Companies, Inc. 14-69 32 10/5/2015 14.2.8 Example: Semantics of Clite Program memory store can be modeled as a function which maps locations to values. For simplicity, we map identifiers to integer values only. Initial store indicates all variables are undefined. (lambda (id) (cond (t 'undefined))) Successive stores bind ids to their values. (lambda (id) (cond ((eq id 'y) 5) ((eq id 'x) 32) (t 'undefined) ) ) Copyright © 2006 The McGraw-Hill Companies, Inc. 14-70 Accessing a Variable’s Value in the Store Function to retrieve the value of a variable from the state: (defun access (store id) (apply store (list id))) Copyright © 2014 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-71 33 10/5/2015 Updating a Variable’s Value in the Store Function to store a new value for a variable in the store: (defun update (store id value) (prog (currentenv newbinding newstore) (setq currentenv (cdar (cddr store))) (setq newbinding (cons (list 'eq 'id (list 'quote id)) (list value))) (setq newstore (list 'lambda (list 'V) (cons 'cond (cons newbinding currentenv)))) (return newstore) ) ) Copyright © 2014 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-72 Updating a Variable’s Value in the Store (update '(lambda (id) (cond ((eq id 'x) 32) (t 'undefined)))) 'y 5) (setq currentenv (cdar (cddr store))) currentenv = ((eq id 'x) 32) (t 'undefined)) (setq newbinding (cons (list 'eq 'id (list 'quote id)) (list value))) newbinding = (((eq id 'y) 5) ((eq id 'x) 32) (t 'undefined)) Copyright © 2014 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-73 34 10/5/2015 Updating a Variable’s Value in the Store (setq newstore (list 'lambda (list 'id) (cons 'cond (cons newbinding currentenv)))) newstore = (lambda (id) (cond ((eq id 'y) 5) ((eq id 'x) 32) (t 'undefined)) ) ) Copyright © 2014 Barrett R. Bryant.Inc. Copyright © 2006 The McGraw-Hill Companies, 14-74 35