Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

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.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
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.
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.
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-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Functional Programming Chapter 14. History of Functional Languages Lisp is the second oldest language Motivated by a need to do symbolic, rather than.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
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.
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 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 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS314 – Section 5 Recitation 9
Functional Programming
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
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.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Lisp and Scheme I.
6.001 SICP Variations on a Scheme
COP4020 Programming Languages
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
More Scheme CS 331.
Presentation transcript:

Functional Programming CS331 Chapter 14

Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data structure –Developed by John McCarthy in the 60’s Used for symbolic data processing Example apps: symbolic calculations in integral and differential calculus, circuit design, logic, game playing, AI As we will see the syntax for the language is extremely simple –Scheme Descendant of LISP

Functional Languages “Pure” functional language –Computation viewed as a mathematical function mapping inputs to outputs –No notion of state, so no need for assignment statements (side effects) –Iteration accomplished through recursion In practicality –LISP, Scheme, other functional languages also support iteration, assignment, etc. –We will cover some of these “impure” elements but emphasize the functional portion Equivalence –Functional languages equivalent to imperative Clite can be implemented fairly straightforwardly in Scheme Scheme itself implemented in C Church-Turing Thesis

Typical Mathematical Function Square maps real numbers to real numbers Value of a function such as Square depends only on the values of its parameters and not on any previous computation –This property is called referential transparency

Lambda Calculus Foundation of functional programming Developed by Alonzo Church, 1941 A lambda expression defines –Function parameters –Body Does NOT define a name; lambda is the nameless function. Below x defines a parameter for the unnamed function:

Lambda Calculus Given a lambda expression Application of lambda expression Identity Constant 2:

Lambda Calculus Any identifier is a lambda expression If M and N are lambda expressions, then the application of M to N, (MN) is a lambda expression An abstraction, written where x is an identifier and M is a lambda expression, is also a lambda expression

Lambda Calculus Examples

Lambda Calculus First Class Citizens Functions are first class citizens –Can be returned as a value –Can be passed as an argument –Can be put into a data structure as a value –Can be the value of an expression

Free and Bound Variables Bound variable: x is bound when there is a corresponding λx in front of the λ expression: ((λy·y)y)y is bound ; y is free Free variable: x is not bound (analogous to a global variable we can access in the scope of a local function) –(λy·z); z is free –(λy·y); y is bound; y binding here

Lambda Calculus Bound variables are just placeholders; they can be renamed to anything else without impacting functionality Not bound implies free

Substitution Idea : function application is seen as a kind of substitution which simplifies a term –( (λx·M) N) as substituting N for x in M Not quite as simple as straightforward substitution –Consider ((λx·(λy·x+y)) 2 1) ((λy·2+y) 1) 2+1 = 3, looks OK –Now consider ((λx·(λy·x+y)) y 1); i.e. y+1 ((λy·y+y) 1) 1+1 = 2, WRONG! –But this would work if: ((λx·(λz·x+z)) y 1) ((λz·y+z) 1) y+1CORRECT! –Problem: y is a bound variable; need to rename before substituting

Lambda Calculus Substitution A substitution of an expression N for a variable x in M is written as M[x  N] If free variables of N have no bound occurrence in M, M[x  N] is formed by replacing all free occurrences of x in M by N Else –Assume that variable y is free in N and bound in M –Consistently replace the binding and corresponding bound occurrences of y in M by a new variable (say u) –Repeat this in M until there is no bound occurrence –Replace all free occurrences in M by N

Lambda Calculus Substitution

Lambda Calculus Semantics This is called Beta-Reduction Replace the lambda expression using the substitution principle Represents a single function application

Lambda Calculus Semantics Evaluation of lambda expression is a sequence of beta reductions A lambda expression that does not contain a function application is called a normal form

Lambda Calculus Functional programming is essentially an applied lambda calculus with built in - constant values - functions E.g. in Scheme, we have (* x x) for x*x instead of λx·x*x

Functional Languages Two ways to evaluate expressions Eager Evaluation or Call by Value –Evaluate all expressions ahead of time –Irrespective of if it is needed or not –May cause some runtime errors Example (foo 1 (/ 1 x))Problem; divide by 0

Lambda Calculus Lazy Evaluation –Evaluate all expressions only if needed (foo 1 (/ 1 x)); (/ 1 x) not needed, so never eval’d –Some evaluations may be duplicated –Equivalent to call-by-name –Allows some types of computations not possible in eager evaluation Example –Infinite lists E.g,. Infinite stream of 1’s, integers, even numbers, etc. –Replaces tail recursion with lazy evaluation call –Possible in Scheme using (force/delay)

Running Scheme for Class A version of Scheme called PLT Scheme (via DrScheme) is available on the Windows machines in the CS Lab Download: scheme.org/drscheme/ Unix, Mac versions also available if desired

DrScheme You can type code directly into the interpreter and Scheme will return with the results:

Make sure right Language is selected

DrScheme – Loading Code You can open code saved in a file. DrScheme uses the extension “.scm” so consider the following file “fact.scm” created with a text editor or saved from DrScheme: (define factorial (lambda (n) (cond ((= n 1) 1) (else (* n (factorial (- n 1)))) ) 1: Open 2: Run 3: Invoke functions

Functional Programming Pure functional programming –No implicit notion of state –No need for assignment statement No side effect –Looping No state variable Use Recursion Most functional programming languages have side effects, including Scheme –Assignments –Input/Output

Functional Programming -Refreshingly simple -Syntax is learned in about 10 seconds -Surprisingly powerful -Recursion -Functions as first class objects (can be value of an expression, passed as an argument, put in a data structure) -Implicit storage management (garbage collection)

Scheme Dialect of Lisp Simple but provides the core constructs Functions as first class objects Lexical scoping –Earlier LISPs did not do that (dynamic) Interpreter –Compiled versions available too

Expressions Syntax - Cambridge Prefix –Parenthesized –(* 3 4) –(* (+ 2 3) 5) –(f 3 4) In general: –(functionName arg1 arg2 …) Everything is an expression –Sometimes called s-expr (symbolic expr)

Expression Evaluation Replace symbols with their bindings Constants evaluate to themselves –2, 44, #f –No nil in DrScheme; use ‘() Nil = empty list, but DrScheme does have empty Lists are evaluated as function calls written in Cambridge Prefix notation (+ 2 3) (* (+ 2 3) 5)

Scheme Basics Atom –Anything that can’t be decomposed further a string of characters beginning with a letter, number or special character other than ( or ) e.g. 2, #t, #f, “hello”, foo, bar #t = true #f = false List –A list of atoms or expressions enclosed in () –(), (1 2 3), (x (2 3)), (()()())

Scheme Basics S-expressions – Atom or list () –Both atom and a list Length of a list –Number at the top level

Quote If we want to represent the literal list (a b c) –Scheme will interpret this as apply the arguments b and c to function a To represent the literal list use “quote” –(quote x)  x –(quote (a b c))  (a b c) Shorthand: single quotation mark ‘a == (quote a) ‘(a b c) == (quote (a b c))

Global Definitions Use define function (define f 20) (define evens ‘( )) (define odds ‘( )) (define color ‘red) (define color blue); Error, blue undefined (define num f); num = 20 (define num ‘f); symbol f (define s “hello world”); String

Lambda functions Anonymous functions –(lambda ( ) ) –(lambda(x) (* x x)) –((lambda(x) (* x x)) 5)  25 Motivation –Can create functions as needed –Temporary functions : don’t have to have names Can not use recursion

Named Functions Use define to bind a name to a lambda expression (define square (lambda (x) (* x x))) (square 5) Using lambda all the time gets tedious; alternate syntax: (define ( ) …) Last expression evaluated is the one returned (define (square x) (* x x)) (square 5)  25

Conditionals (if ) - Return value is either expr1 or expr2 (cond (P1 E1) (P2 E2) (P n E n ) (else E n+1 )) - Returns whichever expression is evaluated

Common Predicates Names of predicates end with ? –Number? : checks if the argument is a number –Symbol? : checks if the argument is a symbol –Equal? : checks if the arguments are structurally equal –Null? : checks if the argument is empty –Atom? : checks if the argument is an atom –List? : checks if the argument is a list

Conditional Examples (if (equal? 1 2) ‘x ‘y) ; y (if (equal? 2 2) ‘x ‘y); x (if (null? ‘())1 2); 1 (cond ((equal? 1 2) 1) ((equal? 2 3) 2) (else 3)); 3 (cond ((number? ‘x) 1) ((null? ‘x) 2) ((list? ‘(a b c)) (+ 2 3)); 5 )

Dissecting a List Car : returns the first argument –(car ‘(2 3 4)) –(car ‘((2) 4 4)) –Defined only for non-null lists Cdr : (pronounced “could-er”) returns the rest of the list –DrScheme: list must have at least one element –Always returns a list (cdr ‘(2 3 4)) (cdr ‘(3)) (cdr ‘(((3)))) Compose (car (cdr ‘(4 5 5))) (cdr (car ‘((3 4))))

Shorthand (cadr x)= (car (cdr x)) (cdar x)= (cdr (car x)) (caar x) = (car (car x)) (cddr x) = (cdr (cdr x)) (cadar x) = (car (cdr (car x))) … etc… up to 4 levels deep in DrScheme (cddadr x) = ?

Why Car and Cdr? Leftover notation from original implementation of Lisp on an IBM 704 CAR = Contents of Address part of Register –Pointed to the first thing in the current list CDR = Contents of Decrement part of Register –Pointed to the rest of the list

Building a list Cons –Cons(truct) a new list from first and rest –Takes two arguments –Second should be a list If it is not, the result is a “dotted pair” which is typically considered a malformed list –First may or may not be a list –Result is always a list

Building a list X = 2 and Y = (3 4 5) : (cons x y)  ( ) X = () and Y =(a b c) : (cons x y)  (() a b c) X = a and Y =() : (cons x y )  (a) What is –(cons 'a (cons 'b (cons 'c '()))) –(cons (cons ‘a (cons ‘b ‘())) (cons ‘c ‘()))

Numbers Regular arithmetic operators are available +, -, *, / –May take variable arguments ( ), (* ) (/ 9 2)  4.5 ; (quotient 9 2)  4 Regular comparison operators are available = = E.g. (= 5 (+ 3 2))  #t = only works on numbers, otherwise use equal?

Example Sum all numbers in a list (define (sumall list) (cond ((null? list) 0) (else (+ (car list) (sumall (cdr list)))))) Sample invocation: (sumall ‘(3 45 1))

Example Make a list of n identical values (define (makelist n value) (cond ((= n 0) '()) (else (cons value (makelist (- n 1) value)) ) In longer programs, careful matching parenthesis.

Example Determining if an item is a member of a list (define (member? item list) (cond ((null? list) #f) ((equal? (car list) item) #t) (else (member? item (cdr list))) ) Scheme already has a built-in (member item list) function that returns the list after a match is found

Example Remove duplicates from a list (define (remove-duplicates list) (cond ((null? list) '()) ((member? (car list) (cdr list)) (remove-duplicates (cdr list))) (else (cons (car list) (remove-duplicates (cdr list)))) )