Functional Programming in Scheme and Lisp.

Slides:



Advertisements
Similar presentations
ALGEBRAIC EXPRESSIONS
Advertisements

Formal Models of Computation Part II The Logic Model
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
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 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
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.
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-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
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)
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
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.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Variables, Environments and Closures. Overview Touch on the notions of variable extent and scope Introduce the notions of lexical scope and dynamic.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS 330 Programming Languages 11 / 28 / 2006 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.
Forms Writing your own procedures CS 480/680 – Comparative Languages.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Introduction to Scheme
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
Introduction to Functional Programming in Racket
Nondeterministic Evaluation
Variables, Environments and Closures
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Lisp and Scheme I.
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
HIGHER ORDER FUNCTIONS
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
More Scheme CS 331.
Presentation transcript:

Functional Programming in Scheme and Lisp

Overview Functions are first class objects in functional programming languages – Can be created, put in data structures, composed, specialized, applied, stopped and restarted, etc. – Used to implement other control structures Functional programming languages also try to eliminate or at least minimize side effects – e.g., assignments, use of mutable data structures Well look at how the functional programming paradigm is manifested in Scheme

Preliminaries: eval Remember: Lisp code is just an s-expression You can call Lisps evaluation process with the eval function > (define s (list 'cadr ' ' (one two three))) > s (cadr ' (one two three)) > (eval s) two > (eval (list 'cdr (car '((quote (a. b)) c)))) b

Preliminaries: apply Apply takes a function F and a list of args, and returns result of applying F to the arguments > (apply + (1 2 3)) 6 It takes any number of args, so long as the last is a list > (apply (3 4 5)) 15 A simple version of apply could be written as (define (apply f list) (eval (cons f list)))

lambda The define special form creates a function and gives it a name However, functions neednt have names and we dont need to use define to create them The primitive way to create functions is to use the lambda special form These are often called lambda expressions, e.g – (lambda (x) (+ x 1)) – (lambda (x y) (sqrt (* x x)(* y y)))

lambda expression A lambda expression is a list of the symbol lambda, followed by a list of parameters, fol- lowed by a body of one or more expressions > (define f (lambda (x) (+ x 2))) > f # > (f 100) 102 > ( (lambda (x) (+ x 2)) 100) 102

Lambda expression lambda is a special form When evaluated, it creates a function and returns a reference to it The function does not have a name A lambda expression can be the first ele- ment of a function call: > ( (lambda (x) (+ x 100)) 1) 101 Other languages like python and javascript have adopted the idea

define vs. define (define (add2 x) (+ x 2) ) (define add2 (lambda (x) (+ x 2))) (define add2 #f) (set! add2 (lambda (x) (+ x 2))) The define special form comes in two varieties 1 st arg is a list 1 st arg is a symbol Three expressions to the left are equivalent The first define form is just more familiar and convenient when defining a function

Functions as objects While many PLs allow functions as args, nameless lambda functions add flexibility Sort takes a list of items to be sorted and a function that determines the ordering > (sort '((a 100)(b 10)(c 50)) (lambda (x y) (< (second x) (second y)))) ((b 10) (c 50) (a 100)) No need to gice the function a name!

lambdas in other languages Lambda expressions are found in many modern languages, e.g., Python: >>> f = lambda x,y: x*x + y >>> f at 0x10048a230> >>> f(2, 3) 7 >>> (lambda x,y: x*x+y)(2,3) 7

Map pattern A common programming pattern is to apply a function to every item in a collection and return a collection of the results This was first supported (I think) by Lisp where the collection is a list Example > (map square ( )) ( ) > (map square null) null

Mapping functions Lisp & Scheme have several mapping functions map (mapcar in Lisp) is the most useful Takes a function and 1 lists and returns a list of the results of applying function to elements taken from each list > (map abs '( )) ( ) > (map + (1 2 3) (4 5 6)) (5 7 9) > (map + (1 2 3) (4 5 6) (7 8 9)) ( )

More map examples > (map cons '(a b c) '(1 2 3)) ((a. 1) (b. 2) (c. 3)) > (map (lambda (x) (+ x 10)) (1 2 3)) ( ) > (map + '(1 2 3) '(4 5)) map: all lists must have same size; arguments were: # (1 2 3) (4 5) === context === /Applications/PLT/collects/scheme/private/misc.ss:74:7

Defining map Defining a simple one argument version of map is easy (define (map1 func list) (if (null? list) null (cons (func (first list)) (map1 func (rest list)))))

Define Lisps every and some every and some take a predicate and one or more sequences Given just one sequence, they test whether the elements satisfy the predicate > (every odd? (1 3 5)) #t > (some even? (1 2 3)) #t Given >1 sequences, they take as many args as sequences: > (every > (1 3 5) (0 2 4)) #t

Defining every is easy (define (every1 f list) ;; note the use of the and function (if (null? list) #t (and (f (first list)) (every1 f (rest list))))) Note: this is the easy case for functions taking only one argument

Define some similarly (define (some1 f list) (if (null? list) #f (or (f (first list)) (some1 f (rest list))))) Note: this is the easy case for functions taking only one argument

Functions with any number of args Defining functions that takes any number of arguments is easy in Scheme (define (foo. args) (printf "My args: ~a\n" args)))printf If the parameter list ends in a symbol as opposed to null (cf. dotted pair), then its value is the list of the remaining arguments values (define (f x y. more-args) …) (define (map f. lists) … ) Works for lambda also (lambda args …) or (lambda (x. args) …)

Every for functions of any arityarity (define (every f. lists) (if (null? (first lists)) #t (and (apply f (map first lists)) (apply every f (map rest lists))))) Good example of where map is useful!

Every for functions of any arityarity > (every odd? '(1 3 5)) #t > (every odd? '( )) #f > (every < '(1 2 3) '( )) #t > (every < '(1 2 3) '( )) #f > (define lists '((1 2 3)( ))) # > (map first lists) '(1 10) > (map rest lists) '((2 3) (20 30)) >

Complement The complement of a boolen function f is one that returns true whenever f returns false and false whenever f returns true (define (complement f) (lambda (x) (not (f x)))) For example: (define (positive n) (>= n 0)) (define negative (complement positive))

Will this work? You can prove that P is true for some list ele- ment by showing that it isnt false for every one Will this work? > (define (some1 f list) (not (every1 (complement f) list))) > (some1 odd? '( )) #t > (some1 (lambda (x) (> x 10)) '( )) #t

Will this work? You can prove that P is true for some list ele- ment by showing that it isnt false for every one You can show that someone in this class is taller than six feet by showing that it is not true that everyone in the class is shorter than six feet x p(x) not x not p(x))

filter (filter ) returns a list of the elements of which satisfy the predicate > (filter odd? ( )) (1 3 5) > (filter (lambda (x) (> x 98.6)) ( )) ( )

Example: filter (define (filter1 func list) ;; returns a list of elements of list where funct is true (cond ((null? list) null) ((func (first list)) (cons (first list) (filter1 func (rest list)))) (#t (filter1 func (rest list))))) > (filter1 even? ( )) (2 4 6)

Example: filter Define integers as a function that returns a list of integers between a min and max (define (integers min max) (if (> min max) null (cons min (integers (add1 min) max)))) Define prime? as a predicate that is true of prime numbers and false otherwise > (filter prime? (integers 2 20) ) ( )

Heres another pattern We often want to do something like sum the elements of a sequence (define (sum-list l) (if (null? l) 0 (+ (first l) (sum-list (rest l))))) Other times we want their product (define (multiply-list l) (if (null? l) 1 (* (first l) (multiply-list (rest l)))))

Heres another pattern We often want to do something like sum the elements of a sequence (define (sum-list l) (if (null? l) 0 (+ (first l) (sum-list (rest l))))) Other times we want their product (define (multiply-list l) (if (null? l) 1 (* (first l) (multiply-list (rest l)))))

Example: reduce Reduce takes (i) a function, (ii) a final value and (iii) a list of arguments Reduce of +, 0, (v1 v2 v3 … vn) is just V1 + V2 + V3 + … Vn + 0 In Scheme/Lisp notation: > (reduce + 0 ( )) 15 (reduce * 1 ( )) 120

Example: reduce (define (reduce function final list) (if (null? list) final (function (first list) (reduce function final (rest list)))))

Using reduce (define (sum-list list) ;; returns the sum of the list elements (reduce + 0 list)) (define (mul-list list) ;; returns the sum of the list elements (reduce * 1 list)) (define (copy-list list) ;; copies the top level of a list (reduce cons () list)) (define (append-list list) ;; appends all of the sublists in a list (reduce append () list))

The roots of mapReduce MapReduce is a software frame- work developed by Google for parallel computation on large datasets on computer clusters MapReduce Its become an important way to exploit parallel computing using conventional programming languages and techniques. See Apaches Hadoop for an open source versionHadoop The framework was inspired by functional programmings map, reduce & side-effect free programs

Function composition Math notation: g h is a composition of func- tions g and hcomposition If f=g h then f(x)=g(h(x)) Composing functions is easy in Scheme > compose # > (define (sq x) (* x x)) > (define (dub x) (* x 2)) > (sq (dub 10)) 400 > (dub (sq 10)) 200 > (define sd (compose sq dub)) > (sd 10) 400 > ((compose dub sq) 10) 200

Defining compose Heres compose for two functions in Scheme (define (compose2 f g) (lambda (x) (f (g x)))) Note that compose calls lambda which returns a new function that applies f to the result of applying g to x Well look at how the variable environments work to support this in the next topic, closures But first, lets see how to define a general ver- sion of compose taking any number of args

Compose in Scheme (define (compose. functions) ;; Returns the identity function if no args given (if (null? functions) (lambda (x) x) (lambda (x) ((first functions) ((apply compose (rest FS)) x))))) ; anonymous composition ((compose sqrt negate square) 5) ; ===> 0+5i ; = (sqrt (negate (square 5)))

Functional Programming Summary Lisp is the archetypal functional programming language It treated functions as first-class objects and uses the same representation for data & code The FP paradigm is a good match for many problems, esp. ones involving reasoning about or optimizing code or parallel execution While no pure FP languages are considered mainstream, many PLs support a FP style