Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Programming Languages and Paradigms Lisp Programming.
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.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
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.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
1 The Metacircular Evaluator Chapter 4 Section 4.1 We will not cover every little detail in the lectures, so you MUST read Section 4.1 in full.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
Chapter 15: Functional Programming Languages
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
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.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
CS314 – Section 5 Recitation 9
Functional Programming
Abstract Syntax cs7100 (Prasad) L7AST.
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Edited by Original material by Eric Grimson
CS 326 Programming Languages, Concepts and Implementation
6.001 SICP Variations on a Scheme
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
Lists in Lisp and Scheme
COP4020 Programming Languages
Env. Model Implementation
Original material by Eric Grimson
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
6.001 SICP Variations on a Scheme
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Announcements Quiz 5 HW6 due October 23
topics interpreters meta-linguistic abstraction eval and apply
More Scheme CS 331.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Functional Programming

Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any). –Evaluation does not produce side effects. –The value of an expression cannot change over time. –No notion of state. –Computation may generate new values, but not change existing ones.

Advantages Simplicity –No explicit manipulation of memory. –Values are independent of underlying machine with assignments and storage allocation. –Garbage collection. Power –Recursion –Functions as first class values Can be value of expression, passed as argument, placed in data structures. Need not be named.

Scheme (A Dialog of LISP) Primarily an interpreted language. Interacting with the interpreter –Supply an expression to be evaluated –Bind a name to a value (could be a function) > ; a number evaluates to itself > (define pi ) ; bind name to value pi > pi

Expressions Prefix notation (op operand1 operand2 operand3 … ) General Expression Evaluation ( E 1 E 2 E 3 … E K ) –Each E i is evaluated (in unspecified order) –E 1 must evaluate to a function which is applied to the values of E 2 … E K –Innermost (call-by-value) evaluation > (* 5 7) ; 35 > (+ 4 (* 5 7)) ; 39

Expressions (2) Uniform syntax is useful for manipulating programs as data. Some “special forms” are evaluated differently.

Function Definitions Lambda expressions (a special form) evaluate to anonymous functions: (lambda (formal-parameters) expression) > (define square (lambda (x) (* x x))) ; square > (square 5) ; 25 > ( (lambda (x) (* x x)) 5) ; 25

Quoting Quote (a special form) prevents evaluation of its operand: > (define pi ) ; pi > pi ; > (quote pi) ; pi > ‘pi ; pi > (define f *) ; f > (f 2 3) ; 6 > (define f ‘*) ; f > (f 2 3) ; Error * not a procedure

Lists A list is a sequence of zero or more values (symbols, numbers, booleans, functions, other lists). A list is written by enclosing its elements in parentheses. Some expressions are lists.

List length > (length ‘() ) ; 0 > (length ‘(a b c) ) ; 3 > (length ‘((a b c)) ) ; 1 > (length ‘((a b) c) ) ; 2 > (length ‘’a) ; 2 > (length ‘(+ 2 3) ) ; 3

Operations on Lists (null? X) ; true if x is the empty list, else false (car x) ; First element in the list x ( cdr x) ; List of all elements of x except the first (cons a x) ; List whose car is a and whose cdr is x ( append x y) ; The concatenation of lists x and y (list e 1 e 2 … e k ) ; List of values of e 1 … e k Operators do not change lists: > (define l ‘(b c d)) ; l > (cons ‘a l) ; (a b c d) > l ; (b c d)

Operations on Lists (2) > (null? ‘a) ; #f > (null? ‘(a)) ; #f > (null? ‘()) ; #t > (car ‘(a b c)) ; a > (car ‘((a b) c)) ; (a b) > (cdr ‘(a b c)) ; (b c) > (cdr ‘((a b) c)) ; (c) > (cons ‘a ‘(b c)) ; (a b c) > (cons ‘(a b) ‘(c d)) ; ((a b) c d) > (append ‘(a b) ‘(c d)) ; (a b c d) > (list ‘a (+ 3 4) ‘b) ; (a 7 b)

Conditionals Booleans #t #f Predicates number? symbol? list? equal? etc. If special form (if P E 1 E 2 ) Cond special form (cond (P 1 E 1 ) (P 2 E 2 ) … (P k E k ) (else E k+1 ))

Examples: factorial, length (define factorial (lambda (n) (if (equal? n 0) 1 (* n (factorial (- n 1)))))) (define length (lambda (x) (if (empty? x) 0 (+ 1 (length (cdr x))))))

Example: flatten (define flatten (lambda (x) (cond ((null? x) x) ((list? (car x)) (append (flatten (car x)) (flatten (cdr x)))) (else (cons (car x) (flatten (cdr x))))))) > (flatten '(a (b (c) d) e) ) ; (a b c d e)

Scoping Let special form (let ((x 1 e 1 ) (x 2 e 2 ) … (x k e k )) f 1 f 2 … f n ) 1. Each e i is evaluated (in parallel) 2. Each f j is evaluated with x i denoting the value of e i 3. The result is the value of f n Let* special form (sequential let) (let* ((x 1 e 1 ) (x 2 e 2 ) … (x k e k )) f 1 f 2 … f n ) Bindings to x i ’s take place sequentially. That is, e i+1 is evaluated with the binding of x i to e i already in effect.

Scope Examples > (+ (square 3) (square 3)) ; 18 > (let ((sq3 (square 3))) (+ sq3 sq3)) ; 18 > (define x 0) ; x > (let ((x 2) (y x)) y) ; 0 > (let* ((x 2) (y x)) y) ; 2

Lexical Scoping > (define g (lambda (f) (let ((x 5)) (f) ))) > (let ((x 0)) (g (lambda () (+ 1 x)) )) ; 1

Naming vs. Assignment C int f(int i) { int x = 0; if (i > 10) x = 1; else x = 2; return x; } Scheme (define f (lambda (i) (let ((x 0)) ; arbitrary code ; … x))) ; What does f return?

Naming vs. Assignment (2) C int f(int i) { int x = 0; if (i > 10) { int x = 1; } else { int x = 2; } return x; } Scheme (define f (lambda (i) (let ((x 0)) (if (> i 10) (let ((x 1)) x) (let ((x 2)) x)) x))) ; What does f return?

Efficiency: Cons vs. Append > (define x1 ‘()) > (define x2 (cons ‘c x1)) > (define x3 (cons ‘b x2)) > (define x4 (cons ‘a x3)) > x1 ; () > x2 ; (c) > x3 ; (b c) > x4 ; (a b c) a b c Cons cells x4 x3 x2 x1

Efficiency: Cons vs. Append (2) > (define x1 ‘(a)) > (define x2 (append x1 ‘(b))) > (define x3 (append x2 ‘(c))) > x1 ; (a) > x2 ; (a b) > x3 ; (a b c) Cons cells a x1 b x3 c a x2 a b

Efficiency: Tail Recursion When a recursive function returns the result of its recursive call, there is no need to maintain a stack of activation records. (define last (lambda (x) (cond ((null? X) ‘()) ((null? (cdr x)) (car x)) (else (last (cdr x))))))

Efficiency: Tail Recursion (2) (define length (lambda (x) (if (null? X) 0 (+ 1 (length (cdr x))))) (define length (lambda (x) (length-help x 0))) (define length-help (lambda (x n) (if (null? x) n (length-help (cdr x) (+ 1 n)))))

Applications: sorting ; Merge two sorted lists (define merge (lambda (x y) (cond ((null? X) y) ((null? Y) x) ((< (car x) (car y)) (cons (car x) (merge (cdr x) y))) (else (cons (car y) (merge x (cdr y))))))) ; Split a list into two equal length halfs define split (lambda (x halfs) (cond ((null? X) halfs) ((nulll? (cdr x)) (list (cons (car x) (car halfs)) (cadr halfs))) (else (split (cddr x) (list (cons (car x) (car halfs)) (cons (cadr x) (cadr halfs))))))))

Applications: sorting (2) ; Mergesort a list (define mergesort (lambda (x) (if (< (length x) 2) x (let* ((halfs (split x ‘(() ()) )) (r1 (mergesort (car halfs))) (r2 (mergesort (cadr halfs)))) (merge r1 r2)))))

Association Lists List of key/value pairs (assoc exp x) ; Find the first pair in x whose ; key has the same value as exp > (define alist ‘((a 1) (b 2) (c 3) (b 4))) ; alist > (assoc ‘b alist) ; (b 2)

Higher Order Functions: Map Let l 1, l 2, … l k be lists of equal length, n. Let a i,j be the j th element of l i. Then: (map f l 1 l 2 … l k ) = ((f a 1,1 a 2,1 … a k,1 ) (f a 1,2 a 2,2 … a k,2 ) … (f a 1,n a 2,n … a k,n )) > (map + ‘(1 2 3) ‘(4 5 6)) ; (5 7 9) > (map cons ‘(a b c) ‘(() (c d) (e))) ; ((a) (b c d) (c e)) > (map + ‘(1 2 3) ‘(4 5 6) ‘(7 8 9)) ; ( )

Higher Order Functions (2) How to write a tail recursive version of length without adding helper functions to top level name space? (define length (lambda (x) (let ((helper (lambda (x n) (if (null? X) n (helper (cdr x) (+ 1 n)))))) (helper x 0)))) But this doesn’t work. Scheme does not allow you to use a symbol before it is bound in a let, so we can’t make the recursive call.

Higher Order Functions (3) Add an additional parameter to the helper function for the function to call. Pass the helper function to itself (after it is defined), so that it can make the recursive call. Each time a recursive call is made, the helper function is passed along so the recursion can continue. (define length (lambda (x) (let ((helper (lambda (help x n) (if (null? X) n (help help (cdr x) (+ 1 n)))))) (helper helper x 0))))

Example: Searching A directed acyclic graph in Scheme (define g ‘((a (b c d)) (b ()) (c (e)) (d (f g)) (e ()) (f (h I j)) (g ()) (h ()) (i ()) (j ()))) A BCD EF HIJ G

Searching (2) (define search (lambda (g start goal) (reverse (expand g goal (list (list start)))))) (define expand (lambda (g goal paths) (if (null? paths) '() (let ((path (car paths)) (paths (cdr paths))) (if (equal? goal (car path)) path (expand g goal (append (map (lambda (x) (cons x path)) (cadr (assoc (car path) g))) paths)))))))

Example: Expression Evaluator Consider the following grammar for expressions with “scheme-like” semantics: E ::= (num val) |(var symbol) |(plus E E) |(times E E) |(let (params) E) params ::= (symbol E) | (symbol E) params

Expression Evaluator (2) (define lookup (lambda (x env) (cadr (assoc x env)))) (define bind (lambda (params env) (append (map (lambda (y) (list (car y) (eval (cadr y) env))) params) env)))

Expression Evaluator (3) (define eval (lambda (exp env) (cond ((equal? (car exp) 'num) (cadr exp)) ((equal? (car exp) 'var) (lookup (cadr exp) env)) ((equal? (car exp) 'plus) (+ (eval (cadr exp) env) (eval (caddr exp) env))) ((equal? (car exp) 'let) (eval (caddr exp) (bind (cadr exp) env))))))

Expression Evaluator (4) > (eval '(let ((x (num 2)) (y (num 3))) (plus (var x) (var y))) '()) ; 5 > (eval '(let ((x (num 1))) (plus (let ((x (num 2)) (y (num 3))) (plus (var x) (var y))) (var x))) '()) ; 6