From Lambda Calculus to LISP Functional Programming Academic Year 2005-2006 Alessandro Cimatti

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline Repeated f Accelerating computations – Fibonacci Let and let* Recursion examples – Palindrome? – Log.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Recursion.
Joseph Lindo Recursion Sir Joseph Lindo University of the Cordilleras.
Fall 2008Programming Development Techniques 1 Topic 3 Linear Recursion and Iteration September 2008.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. 2 Outline Scoping and block structure Recursive and iterative processes Orders of growth.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
Defining functions in lisp In lisp, all programming is in terms of functions A function is something which –takes some arguments as input –does some computing.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 2. Reminder: Recursive algorithm Reduce problem to one or more sub-problems of smaller sizes (linear or tree.
Imperative programming public int factorial (int N){ int F = 1; for(X=N; X>1; X-- ){ F= F*X; } return F; } Functional programming (defun factorial(N) (cond.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
1 Chapter 1: Introduction What you have learnt in Comp1220 or Comp1170? What will be taught in Comp 1200? - more Abstract Data Types -efficient algorithms.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 4. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Structure and Interpretation of Computer Programs Presented by Yan Yan CSE 294, UCSD, April 13, Chapter Harold Abelson, Gerald and Julie.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Scheme Tutorial. Goals Combine several simple ideas into one compound idea to obtain complex ideas Bring two ideas together to obtain relations Seperate.
Glen Martin - School for the Talented and Gifted - DISD Recursion Recursion Recursion Recursion Recursion Recursion.
RECURSION.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Algorithmic Recursion. Recursion Alongside the algorithm, recursion is one of the most important and fundamental concepts in computer science as well.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
1 Programming Languages and Paradigms Functional Programming.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Recursion: Linear and Tree Recursive Processes and Iteration CMSC Introduction to Computer Programming October 7, 2002.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Recursion, pt. 1 The Foundations. What is Recursion? Recursion is the idea of solving a problem in terms of itself. – For some problems, it may not possible.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Today’s topics Orders of growth of processes Relating types of procedures to different orders of growth.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS220 Programming Principles 프로그래밍의 이해 2003 가을학기 Class 2 한 태숙.
Lisp "List Processing". Lisp history John McCarthy developed the basics behind Lisp during the 1956 Dartmouth Summer Research Project on Artificial Intelligence.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 3. Outline High order procedures –Finding Roots –Compose Functions Accelerating Computations –Fibonacci 2.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
Analyzing Programs: Order of Growth CMSC Introduction to Computer Programming October 11, 2002.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
CS314 – Section 5 Recitation 10
Computer Science 4 Mr. Gerb
Example of formula (defun roots (a b c) (list
Fibonacci Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 Definition:
Closures and Streams cs784(Prasad) L11Clos
CS21b: Structure and Interpretation
Applied Algorithms (Lecture 17) Recursion Fall-23
Functional Programming
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
L Calculus.
Rosen 5th ed., §§ ~18 slides, ~1 lecture
Abstraction and Repetition
Lecture 2 מבוא מורחב.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Lecture 2 מבוא מורחב.
Presentation transcript:

From Lambda Calculus to LISP Functional Programming Academic Year Alessandro Cimatti

The LISP Language Proposed by John McCarthy in the late 1950s Based on Lambda Calculus Key concepts –Interpreted language –Dynamic data structures –LISP Procedures as LISP data Basic notions –primitive expressions –combination of simple into compound expressions –abstraction, compound objects are named and manipulated as units

LISP Expressions > > ( ) 531 > ( ) 666 > (* 5 99) 495 > (/ 10 5) 2 > (/ 10 6) > ( ) 12.7 > ( ) 75 > (* ) 1200 The operator is always the leftmost element Parentheses rule out ambiguities

LISP Expressions > (+ (* 3 5) (- 10 6)) 19 > (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) 33

Naming the Environment > (setq size 2) 2 > (setq pi ) > (* pi (* radius radius)) > (setq circumference (* 2 pi radius))

Evaluating Combinations > (* (+ 2 (* 4 6)) ( )) ; (* ( ) ( )) ; (* ( ) 15 ) ; (* ) 390 Any evaluation order is ok (in this simple example)

Evaluating Combinations > (* (+ 2 (* 4 6)) ( )) * *

Compound Functions > (defun square (x) (* x x)) square > (square 21) 441 > (square (+ 2 5)) 49 > (square (square 3)) 81 > (defun ( ) )

Compound Functions > (defun sum-of-squares (x y) (+ (square x) (square y))) sum-of-squares > (sum-of-squares 3 4) 25 > (defun f (a) (sum-of-squares (+ a 1) (* a 2))) f > (f 5) 136

Procedure Application > (f 5) ((λ a.(sum-of-squares (+ a 1) (* a 2)) 5) (sum-of-squares (+ 5 1) (* 5 2)) (sum-of-squares 6 10 ) ((λ x y.(+ (square x) (square y))) 6 10) (+ (square 6) (square 10)) (+ ((λ x.(* x x)) 6) ((λ x.(* x x)) 10)) (+ (* 6 6) (* 10 10)) ( ) 136

Procedure Application > (f 5) (sum-of-squares (+ 5 1) (* 5 2)) (+ (square (+ 5 1)) (square (* 5 2))) (+ (* (+ 5 1) (+ 5 1)) (* (* 5 2) (* 5 2))) (+ (* 6 6 ) (* 10 10)) ( ) 136 The result is the same, but the evaluation process is different! Notice that (+ 5 1) and (* 5 2) are evaluated twice.

Reduction Methods Normal-order evaluation –fully expand body, and then reduce Applicative-order evaluation –evaluate the arguments and then apply –additional efficiency, reduced evaluations More to come…

Conditional Expressions > (defun abs (x) (cond ((> x 0) x) ((= x 0) 0) ((< x 0) (- x)))) abs > (defun abs (x) (cond ((< x 0) (- x)) (else x))) abs > (defun abs (x) (if (< x 0) (- x) x)) abs

Conditional Expressions > (cond ( ) ( )... ( )) > (if )

Logical Operators > (and... ) > (or... ) > (not ) > (and (> x 5) (< x 10)) > (defun >= (x y) (or (= x y) (> x y))) > (defun >= (x y) (not (< x y))

Exercise Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers

A Solution > (defun sos-greatest (x y z) (cond ((and (< x y) (< x z)) (sum-of-squares y z)) ((and (< y x) (< y z)) (sum-of-squares x z)) (else (sum-of-squares x y)))))

Recursion > (defun factorial (n) (if (= n 1) 1 (* n (factorial (- n 1)))) factorial > (factorial 6) (* 6 (factorial 5)) (* 6 (* 5 (factorial 4))) (* 6 (* 5 (* 4 (factorial 3)))) (* 6 (* 5 (* 4 (* 3 (factorial 2))))) (* 6 (* 5 (* 4 (* 3 (* 2 (factorial 1)))))) (* 6 (* 5 (* 4 (* 3 (* 2 1))))) (* 6 (* 5 (* 4 (* 3 2)))) (* 6 (* 5 (* 4 6))) (* 6 (* 5 24)) (* 6 120) 720

Recursion > (defun factorial (n) (f-iter 1 1 n)) factorial > (defun f-iter (res cnt max) (if (> cnt max) res (f-iter (* cnt res) (+ cnt 1) max))) f-iter

Recursion > (factorial 6) (f-iter 1 1 6) (f-iter 1 2 6) (f-iter 2 3 6) (f-iter 6 4 6) (f-iter ) (f-iter ) (f-iter ) 720

Comparison Linear recursive Stack needed Conceptually simpler Iterative Does not need stack State is finite Powerful compilation technique

Fibonacci Numbers The sequence of Fibonacci –Fib(n) = 0 if n = 0, else –Fib(n) = 1 if n = 1, else –Fib(n) = Fib(n – 1) + Fib(n – 2) 0, 1, 1, 2, 3, 5, 8, 13, 21,... > (defun fib (n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2))))) fib

Fibonacci: Computation > (fib 5) (+ (fib 4) (fib 3)) (+ (fib 4) (+ (fib 2) (fib 1))) (+ (fib 4) (+ (+ (fib 1)(fib 0)) 1)) (+ (fib 4) (+ (+ 1 0) 1)) (+ (+ (fib 3) (fib 2)) 2) (+ (+ (fib 3) (+ (fib 1) (fib 0))) 2) (+ (+ (fib 3) (+ 1 0)) 2) (+ (+ 2 1) 2) 5

Exercise Tree recursion –Can be very expensive Can we define linear iteration for Fibonacci numbers? Hint –identify the state: we need only the two previous values of the series, say a an b –Initially, a is 1 and b is 0 –At each step, a becomes a + b b becomes a a 1, 1, 2, 3, 5, 8, 13, 21, 34,... b 0, 1, 1, 2, 3, 5, 8, 13, 21,...

Linear Iteration for Fibonacci > (defun fib (n) (fib-iter 1 0 n)) fib > (defun fib-iter (a b cnt) (if (= cnt 0) b (fib-iter (+ a b) a (- cnt 1)))) fib-iter

Exercise Exponentiation –b n = 1, if n = 0 –b n = b * b n-1, otherwise Exponentiation in LISP –using linear recursion –using linear iteration

Exponentiation > (defun expt (b n) (if (= n 0) 1 (* b (expt b (- n 1))))) expt

Iterative Exponentiation > (defun expt (b n) (expt-iter b n 1) expt > (defun expt-iter (b cnt res) (if (= cnt 0) res (expt-iter b (- cnt 1) (* b res)))) expt-iter

Iterative Squaring We have seen –linear recursion –linear iteration Can we do better? –E.g. use only a logarithmic number of steps? Exponentiation also follows these rules –b 2n = (b n ) 2 –b 2n+1 = b * b 2n

Iterative Squaring > (defun fast-exp (b n) (cond ((= n 0) 1) ((even? n) (square (fast-exp b (/ n 2))) (else (* b (fast-exp b (- n 1))))) fast-exp