CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

Slides:



Advertisements
Similar presentations
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
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.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages It’s elegant, minimal, can be.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Lisp A functional language. As always… How is it similar? First it runs on the same OS as all applications Uses runtime activation stack as others Needs.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
CS 154 Formal Languages and Computability March 17 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
CS 326 Programming Languages, Concepts and Implementation
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CS 153: Concepts of Compiler Design November 28 Class Meeting
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
Env. Model Implementation
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
FP Foundations, Scheme In Text: Chapter 14.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
The Metacircular Evaluator
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
Modern Programming Languages Lecture 20 Fakhar Lodhi
Streams, Delayed Evaluation and a Normal Order Interpreter
Announcements Quiz 5 HW6 due October 23
Recursive Procedures and Scopes
More Scheme CS 331.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 2 Assignment #3  You are provided with Scheme code that differentiates polynomial expressions using infix notation.  Write two additional procedures evaluate and evaluate-deriv that each takes two parameters, f and x. f is a list that represents the function’s polynomial expression in infix notation. x is the value of x to evaluate f or f ', respectively. _ (deriv '(a x ^ 5 + b x ^ x ^ x ^ x + 7) 'x)  (5 a x ^ b x ^ x ^ x + 3)

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 3 Assignment #3, cont’d  The coefficient values can be defined beforehand. Example: (define a 1)  Your procedures must work at least with the following values for f, a, and b, and x bound to 0 and then to 1. Of course, you should test with other values. (define f '(a x ^ 5 + b x ^ x ^ x ^ x + 7)) (deriv f 'x)  (5 a x ^ b x ^ x ^ x + 3) (define a 1) (define b 1) (evaluate f 0)  7 (evaluate f 1)  20 (evaluate-deriv f 0)  3 (evaluate-deriv f 1)  30

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 4 Assignment #3, cont’d  Tip: Write helper procedures to convert the polynomial expression from infix to prefix notation.  a zip file to Text files containing your new Scheme procedures. Text files containing output from the above example values for f, a, b, and x, and other test values. A short report (a few paragraphs) that briefly explains your code design.  Name your zip file after your team name, such as SuperCoders.zip.  Your subject line should be CS 152 Assignment #3, team name  CC all your team members.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 5 Flat Recursion Example: remove-top  Flatly recursive procedure remove-top removes all top-level occurrences of an item from a list:  Deeply recursive procedure remove-all removes all occurrences of an item from a list. No matter how deeply nested. (define remove-top (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (remove-top item (cdr lst))) (else (cons (car lst) (remove-top item (cdr lst))))) )) (remove-top 2 '(1 2 3 (1 2 3 (1 2)) 4))  (1 3 (1 2 3 (1 2)) 4)

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 6 Deep Recursion Example: remove-all (define remove-top (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (remove-top item (cdr lst))) (else (cons (car lst) (remove-top item (cdr lst))))) )) (define remove-all (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (remove-all item (cdr lst))) ((pair? (car lst)) (cons (remove-all item (car lst)) (remove-all item (cdr lst)))) (else (cons (car lst) (remove-all item (cdr lst))))) )) (remove-all 2 '(1 2 3 (1 2 3 (1 2)) 4))  (1 3 (1 3 (1)) 4)

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 7 Flat vs. Deep Recursion  Flat recursion Apply the recursive call only to the cdr of the list argument.  Deep recursion Also apply the recursive call to the car of the list argument if the car is a nested sublist. _

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 8 Deep Recursion Example: flatten  Deeply recursive procedure flatten brings all the elements of a list up to the top level. No matter how deeply nested the elements are. (define flatten (lambda (lst) (cond ((null? lst) '()) ((pair? (car lst)) (append (flatten (car lst)) (flatten (cdr lst)))) (else (cons (car lst) (flatten (cdr lst))))) )) (flatten '(1 2 (3 (4 (5 6)) 7 (((8)))) 9))  ( )

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 9 Deep Recursion Example: remove-leftmost  Deeply recursive procedure remove-leftmost removes the leftmost occurrence of an item from the argument list. No matter how deeply nested the item is.  What is the extra challenge? The leftmost occurrence can be in either the car or the cdr of the argument list. _

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 10 Deep Recursion Example: remove-leftmost  We need a helper procedure member-all? that determines whether or not an item occurs in a list, no matter how deeply nested.  Procedure remove-leftmost uses it to check the car of its argument list. If the item is in the car of the list, it recursively removes the item from the car of the list and cons ’s the result with the cdr of the list. If the item is not in the car of the list, it cons ’s the car with the result from recursively removing the item from the cdr of the list. (define remove-leftmost (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (cdr lst)) ((and (pair? (car lst)) (member-all? item (car lst))) (cons (remove-leftmost item (car lst)) (cdr lst))) (else (cons (car lst) (remove-leftmost item (cdr lst))))) ))

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 11 Deep Recursion Example: member-all?  Deeply recursive procedure member-all? checks both the car (if necessary) and the cdr of its argument list. (define member-all? (lambda (item lst) (if (null? lst) #f (or (equal? item (car lst)) (and (not (pair? (car lst))) (member-all? item (cdr lst))) (and (pair? (car lst)) (or (member-all? item (car lst)) (member-all? item (cdr lst)))))) )) (member-all? 2 '(1 2 3))  #t (member-all? 2 '(1 (2) 3))  #t (member-all? 2 '((1 2) 2 3))  #t (member-all? 2 '((1 9) 9 3))  #f

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 12 Deep Recursion Example: remove-leftmost (define remove-leftmost (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (cdr lst)) ((and (pair? (car lst)) (member-all? item (car lst))) (cons (remove-leftmost item (car lst)) (cdr lst))) (else (cons (car lst) (remove-leftmost item (cdr lst))))) )) (remove-leftmost 2 '(1 2 3))  (1 3) (remove-leftmost 2 '(1 (2) 3))  (1 () 3) (remove-leftmost 2 '((1 2) 2 3))  ((1) 2 3) (remove-leftmost 2 '((1 9) 9 3))  ((1 9) 9 3)

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 13 Recursion vs. Iteration  A recursive procedure requires a runtime stack to hold “pending” results during recursive calls. Example: A recursive definition of the factorial function. (define factorial (lambda (n) (if (zero? n) 1 (* n (factorial (sub1 n)))) )) (trace factorial) > (factorial 5) |(factorial 5) | (factorial 4) | |(factorial 3) | | (factorial 2) | | |(factorial 1) | | | (factorial 0) | | | 1 | | 2 | |6 | 24 |

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 14 Iterative Factorial  Rewrite the procedure so that its computation proceeds as an iterative process rather then a recursive one. An extra argument nfact holds the intermediate results.  nfact must be initialized to 1. Pending results no longer need to be kept on the runtime stack.  This is an example of tail recursion. (define iter-factorial (lambda (n nfact) (if (zero? n) nfact (iter-factorial (sub1 n) (* n nfact))) )) (trace iter-factorial) > (iter-factorial 5 1) |(iter-factorial 5 1) |(iter-factorial 4 5) |(iter-factorial 3 20) |(iter-factorial 2 60) |(iter-factorial 1 120) |(iter-factorial 0 120) |

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 15 Tail Recursion  In procedure factorial The result from each recursive call is multiplied by n. Therefore, a call to factorial cannot return a result until the recursive call returns its result.  In procedure iter-factorial The result from a recursive call is simply returned. Therefore, each call can return immediately.  Scheme automatically converts tail recursion to iteration. This optimization can greatly improve runtime efficiency. (define iter-factorial (lambda (n nfact) (if (zero? n) nfact (iter-factorial (sub1 n) (* n nfact))) )) (define factorial (lambda (n) (if (zero? n) 1 (* n (factorial (sub1 n)))) ))

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 16 Tail Recursion Example: Fibonacci  Why is this recursive procedure fib to compute Fibonacci numbers so highly inefficient? (define fib (lambda (n) (if (< n 2) n (+ (fib (- n 2)) (fib (- n 1)))) )) (trace fib) > (fib 5) |(fib 5) | (fib 4) | |(fib 3) | | (fib 2) | | |(fib 1) | | |1 | | |(fib 0) | | |0 | | 1 | | (fib 1) | | 1 | |2 | |(fib 2) | | (fib 1) | | 1 | | (fib 0) | | 0 | |1 | 3 | (fib 3) | |(fib 2) | | (fib 1) | | 1 | | (fib 0) | | 0 | |1 | |(fib 1) | |1 | 2 |5 5

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 17 Tail Recursion Example: Fibonacci  An iterative version Extra arguments fib1 and fib2 must be initialized to 0 and 1, respectively. (define iter-fib (lambda (n fib1 fib2) (if (= n 1) fib2 (iter-fib (sub1 n) fib2 (+ fib1 fib2))) )) (trace iter-fib) > (iter-fib ) |(iter-fib ) |(iter-fib 9 1 1) |(iter-fib 8 1 2) |(iter-fib 7 2 3) |(iter-fib 6 3 5) |(iter-fib 5 5 8) |(iter-fib ) |(iter-fib ) |(iter-fib ) |(iter-fib ) |55 55

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 18 Scope  In this lambda expression the variables x and y in the body (+ x y) are lexically bound (AKA lambda bound) since they are in the parameter list of the lambda expression.  In this lambda expression the variables x and y in the body are bound, but variables a and b are free.  In both these examples, the scope of x and y is the lambda expression. (lambda (x y) (+ x y) (lambda (x y) (+ x y a b)

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 19 Scope, cont’d  Scheme is statically scoped (AKA lexically scoped). The scope of a variable is determined by where the variable is declared in the source code. If a variable appears in the parameter list of a lambda expression, then its scope is the body of that lambda expression.  Nested scopes: ((lambda (x) ((lambda (y) (- x y)) 15)) 20)  5

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 20 Local Bindings with let  Scheme has the special form let to create scopes and to do local bindings of variables. Examples: (let ((a 2) (b 3)) (+ a b))  5 (let ((add2 (lambda (x) (+ x 2))) (b 10)) (- b (add2 b)))  -2

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 21 Local Bindings with let, cont’d  With let, we can rewrite procedure remove-leftmost without needing the member-all? helper procedure. (define remove-leftmost-let (lambda (item lst) (cond ((null? lst) '()) ((equal? item (car lst)) (cdr lst)) ((pair? (car lst)) (let ((remlst (remove-leftmost-let item (car lst)))) (cons remlst (if (equal? remlst (car lst)) (remove-leftmost-let item (cdr lst)) (cdr lst))))) (else (cons (car lst) (remove-leftmost-let item (cdr lst))))) )) (remove-leftmost-let 2 '(1 2 3))  (1 3) (remove-leftmost-let 2 '(1 (2) 3))  (1 () 3) (remove-leftmost-let 2 '((1 2) 2 3))  ((1) 2 3) (remove-leftmost-let 2 '((1 9) 9 3))  ((1 9) 9 3) Explain this equal? test.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 22 Local Bindings with letrec  This won’t work: In the recursive call, factorial-let is unbound. The procedure name wasn’t bound outside of the let expression.  Use letrec instead (let ((factorial-let (lambda (n) (if (zero? n) 1 (* n (factorial-let (sub1 n))))))) (factorial-let 5)) (letrec ((factorial-let (lambda (n) (if (zero? n) 1 (* n (factorial-let (sub1 n))))))) (factorial-let 5))  120

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 23 Local Bindings with letrec, cont’d  Use letrec to define an iterative factorial procedure that only requires one argument. The two-argument procedure is nested inside the one-argument procedure. The one-argument procedure is guaranteed to initialize the second argument of the two-argument procedure to 1. (define iter-factorial-letrec (lambda (n) (letrec ((iter-fact (lambda (k kfact) (if (zero? k) kfact (iter-fact (sub1 k) (* k kfact)))))) (iter-fact n 1)) )) (iter-factorial-letrec 5)  120

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 24 Closures  A lambda expression is a closure that consists of Its list of parameters. Its body. The runtime environment in which any free variables in its body are bound at the time the lambda expression is evaluated.  Recall that evaluating a lambda expression creates a procedure. (define addb (let ((b 100)) (lambda (x) (+ x b)))) (let ((b 10)) (addb 25))  125 The let creates a runtime environment where b is bound to 100. This environment is in effect when the lambda expression is evaluated, and it is a part of the lambda expression’s closure. When addb is applied to argument 25, it looks within its closure to get the value 100 for the free variable b in its body.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 25 Closures, cont’d (let ((b 2)) (let ((add2 (lambda (x) (+ x b))) (b 5)) (+ b (add2 b))) )  12 The outer let creates a runtime environment where b is bound to 2. This environment will be in effect when the lambda expression is evaluated, and it is a part of the lambda expression’s closure. The inner let creates a nested environment where b is bound to 5. In the expression (+ b (add2 b)), both occurrences of b are each bound to 5. But the free variable b in the lambda expression is bound to 2. Therefore, (+ 5 (+ 5 2))  12.

SJSU Dept. of Computer Science Spring 2014: February 24 CS 152: Programming Language Paradigms © R. Mak 26 Closures, cont’d  Closure is a key feature of Lisp and Scheme.  Are closures used in other languages? Java? C++? Python?  Reference: