Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.

Slides:



Advertisements
Similar presentations
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Advertisements

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.
Chapter 11 Proof by Induction. Induction and Recursion Two sides of the same coin.  Induction usually starts with small things, and then generalizes.
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.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
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.
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.
CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.
Today - Limits of computation - Complexity analysis examples.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
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.
The ACL2 Proof Assistant Formal Methods Jeremy Johnson.
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 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.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 8: Accumulating.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS 603: Programming Language Organization Lecture 9 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
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.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
CS314 – Section 5 Recitation 10
Additional Scheme examples
History of Computing – Lisp
CS 550 Programming Languages Jeremy Johnson
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSC 533: Programming Languages Spring 2017
The Environment Model*
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CSC 533: Programming Languages Spring 2016
CS 270 Math Foundations of CS Jeremy Johnson
Introduction to Functional Programming in Racket
Proving Properties of Recursive List Functions
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
Announcements HW4 due today. There is no HW5. (It was merged into HW4.) HW6 (Scheme, Problem 1) out today, due October 23rd We’ll cover 100% of what you.
Introduction to Functional Programming in Racket
Announcements Exam 2 on Friday, November 2nd Topics
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
More Scheme CS 331.
Presentation transcript:

Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements page Functional correctness: 75%, comments: 25% Writing “comments”. We require that you use the style from “How to Design Programs” (some modifications by me)

Spring 16 CSCI 4430, A Milanova/BG Ryder 2 Writing “Comments” Each function should have the following sections: ;; Contract: len : (list ‘a) -> number ;; Purpose: to compute length of a list lis ;; Example: (len ‘(1 (2 3) 4)) should return 3 ;; Definition: (define (len lis) (if (null? lis) 0 (+ 1 (len (cdr lis)))))

3 Writing “Comments” ;; Contract: ;; len : (list ‘a) -> number Has two parts. The first part, to the left of the colon, is the name of the function. The second part, to the right of the colon, states what type of data it consumes and what type of data it produces We shall use ‘a, ‘b, ‘c, etc. to denote type parameters, and list to denote the list type Thus, len is a function, which consumes a list whose elements are of some type ‘a, and produces a number

Spring 16 CSCI 4430, A Milanova/BG Ryder 4 ;; Contract: ;; lcs : (list symbol) * (list symbol) -> (list symbol) lcs : is a function, which consumes a list of symbols, another list of symbols, and returns a list of symbols (you may assume that the inputs are lists of symbolic constants such as a, b, sam) ;; Purpose: to compute the longest common sublist of two lists Writing “Comments”

Comments are extremely important in Scheme. Perhaps more so than in Java, C++ and C Why? Our “comments” amount to adding an unchecked type signature + an informal behavioral spec Spring 16 CSCI 4430, A Milanova/BG Ryder 5

6 Functional Programming Languages Scheme S-expressions, lists cons, car, cdr Defining Functions Recursive Functions Shallow recursion, Deep recursion Last Class

7 Functional Programming with Scheme Keep reading: Scott, Chapter 10

Spring 16 CSCI 4430, A Milanova/BG Ryder 8 Lecture Outline Scheme Equality testing Higher-order functions map, foldr, foldl

Spring 16 CSCI 4430, A Milanova/BG Ryder 9 Equality Testing eq? Built-in predicate that can check atoms for equal values Doesn’t work on lists as you would expect equal? Built-in predicate that works on lists eql? Our predicate that works on lists (define (eql? x y) (or (and (atom? x) (atom? y) (eq? x y)) (and (not (atom? x)) (not (atom? y)) (eql? (car x) (car y)) (eql? (cdr x) (cdr y)) )))

Spring 16 CSCI 4430, A Milanova/BG Ryder 10 Equality testing: Examples (eql? ‘(a) ‘(a)) yields what? (eql? ‘a ‘b) yields what? (eql? ‘b ‘b) yields what? (eql? ‘((a)) ‘(a)) yields what? Built-in equal? works like our eql? (eq? ‘a ‘a) yields what? (eq? ‘(a) ‘(a)) yields what?

Spring 16 CSCI 4430, A Milanova/BG Ryder 11 Models for Variables (Scott, p. 225) Value model for variables A variable is a location that holds a value I.e., a named container for a value a := b Reference model for variables A variable is a reference to a value Every variable is an l-value Requires dereference when r-value needed (usually, but not always implicit) l-value (the location)r-value (the value held in that location)

Spring 16 CSCI 4430, A Milanova/BG Ryder 12 Models for Variables: Example Value model for variables b := 2b: c := b c: a := b+ca: Reference model for variables b := 2b c := b c a := b+ca b := 2; c := b; a := b + c;

Questions What is the model for variables in C/C++? Value model Python? Reference model Java? Mixed. Value model for variables of simple type, reference model for variables of reference type Scheme? Spring 16 CSCI 4430, A Milanova/BG Ryder 13

Spring 16 CSCI 4430, A Milanova/BG Ryder 14 Equality Testing: How does eq? work? Scheme uses the reference model for variables (define (f x y) (list x y)) Call (f ‘a ‘a) yields (a a) x refers to atom a and y refers to atom a. eq? checks that x and y both point to the same place. Call (f ‘(a) ‘(a)) yields ((a) (a)) x and y do not refer to the same list. xyxy a xyxy ( ) a A cons cell.

Equality Testing In languages with reference model for variables we need two tests for equality One tests reference equality, whether two references refer to the same object eq? in Scheme (eq? ‘(a) ‘(a)) yields #f == in Java Other tests value equality. Even if the two references do not refer to the same object, the objects can have the same value equal? in Scheme (equal? ‘(a) ‘(a)) yields #t.equals() in Java 15

16 Higher-order Functions Functions are first-class values A function is said to be a higher-order function if it takes a function as an argument or returns a function as a result Functions as arguments (define (f g x) (g x)) (f number? 0) yields #t (f len ‘(1 (2 3)) ) yields what? (f (lambda (x) (* 2 x)) 3) yields what? Functions as return value

Spring 16 CSCI 4430, A Milanova/BG Ryder 17 Higher-order Functions: map Higher-order function used to apply another function to every element of a list Takes 2 arguments: a function f and a list lis and builds a new list by applying the f to each element of lis (define (mymap f lis) (if (null? lis) ‘() (cons (f (car lis)) (mymap f (cdr lis))))))

map (map f lis) ( e1 e2 e3 … en ) ( r1 r2 r3 … rn ) There is a build-in function map Spring 16 CSCI 4430, A Milanova/BG Ryder 18 ff ff

Spring 16 CSCI 4430, A Milanova/BG Ryder 19 map (define (mymap f l) (if (null? l) ‘( ) (cons (f (car l)) (mymap f (cdr l)))))) (mymap abs ‘( )) yields ( ) (mymap (lambda (x) (+ 1 x)) ‘(1 2 3)) yields what? (mymap (lambda (x) (abs x)) ‘( )) yields what?

20 map Remember atomcount, calculates the number of atoms in a list (define (atomcount s) (cond ((null? s) 0) ((atom? s) 1) (else (+ (atomcount (car s)) (atomcount (cdr s)))))) We can write atomcount2, using map: (define (atomcount2 s) (cond ((atom? s) 1) (else (apply + (map atomcount2 s))))) (atomcount2 ‘(1 2 3)) yields 3 (atomcount2 ‘((a b) d)) yields 3 (atomcount2 ‘(1 ((2) 3) (((3) (2) 1)))) yields what? How does this function work?

21 Question My atomcount2 defined below (define (atomcount2 s) (cond ((atom? s) 1) (else (apply + (map atomcount2 s))))) has a bug :). Can you find it? Answer: It counts the null list ‘() as an atom. E.g., (atomcount2 ‘()) will return 1. Spring 16 CSCI 4430, A Milanova/BG Ryder

Exercise (define (atomcount2 s) (cond ((atom? s) 1) (else (apply + (map atomcount2 s))))) Now, let us write flatten2 using map (flatten2 ‘(1 ((2) 3) (((3) (2) 1)))) yields ( ) (define (flatten2 s) … Hint: you can use (apply append (… Spring 16 CSCI 4430, A Milanova/BG Ryder 22

23 foldr Higher-order function that “folds” (“reduces”) the elements of a list into one, from right-to-left Takes 3 arguments: a binary operation op, a list lis, and initial value id. It “folds” (“reduces”) lis (define (foldr op lis id) (if (null? lis) id (op (car lis) (foldr op (cdr lis) id)) )) (foldr + ‘( ) 0) yields 60 it is (10 + (20 + (30 + 0))) (foldr - ‘( ) 0) yields ?

foldr (foldr op lis id) ( e1 … en-1 en ) id ( e1 … en-1 ) res1 … ( e1 ) resn-1 resn Spring 16 CSCI 4430, A Milanova/BG Ryder 24

Spring 16 CSCI 4430, A Milanova/BG Ryder 25 Exercise What does (foldr append ‘((1 2) (3 4)) ‘()) yield? Recall that append appends two lists: (append ‘(1 2) ‘((3) (4 5))) yields (1 2 (3) (4 5)) Now, define a function len2 that computes the length of a list using foldr (define (len2 lis) (foldr …

foldr (define (f lis) (foldr (lambda (x y) (+ 1 y)) lis 0)) ( a b c ) 0 ( a b ) 1 ( a ) 2 3 Spring 16 CSCI 4430, A Milanova/BG Ryder 26 List element Partial result

Spring 16 CSCI 4430, A Milanova/BG Ryder 27 foldr foldr is right-associative E.g., (foldr + ‘(1 2 3) 0) is (1 + (2 + (3 + 0))) Partial results are calculated in order down the else-branch (define (foldr op lis id) (if (null? lis) id (op (car lis) (foldr op (cdr lis) id)))) ()

Spring 16 CSCI 4430, A Milanova/BG Ryder 28 foldl foldl is left-associative and (as we shall see) more efficient than foldr E.g., (foldl + ‘(1 2 3) 0) is (((0 + 1) + 2) + 3) Partial results are accumulated in id (define (foldl op lis id) (if (null? lis) id (foldl op (cdr lis) (op id (car lis))))) ()()

foldl (foldl op lis id) id ( e1 e2 e3 … en ) id1 ( e2 e3 … en ) id2 ( e3 … en ) … idn-1 ( en ) idn Spring 16 CSCI 4430, A Milanova/BG Ryder 29

Spring 16 CSCI 4430, A Milanova/BG Ryder 30 Exercise Define a function rev computing the reverse of a list using foldl E.g., (rev ‘(a b c)) yields (c b a) (define (rev lis) (foldl (lambda (x y) (… (define (foldl op lis id) (if (null? lis) id (foldl op (cdr lis) (op id (car lis)))))

foldl (define (f lis) (foldl (lambda (x y) (cons y x)) lis ‘())) () ( a b c ) (a) ( b c ) (b a) ( c ) (c b a) () Spring 16 CSCI 4430, A Milanova/BG Ryder 31 Next element Partial result

Exercise 32 (define (foldr op lis id) (if (null? lis) id (op (car lis) (foldr op (cdr lis) id)) )) (define (foldl op lis id) (if (null? lis) id (foldl op (cdr lis) (op id (car lis)))) ) Write len, which computes the length of the list, using foldl Write rev, which reverses the list, using foldr

Spring 16 CSCI 4430, A Milanova/BG Ryder 33 Can you write the contract for foldl ? (define (foldl op lis id) (if (null? lis) id (foldl op (cdr lis) (op id (car lis))))) ;; Contract: ;; foldl : (‘b * ‘a -> ‘b ) * (list ‘a) * ‘b -> ’b Exercise

Spring 16 CSCI 4430, A Milanova/BG Ryder 34 How about the contract for foldr ? (define (foldr op lis id) (if (null? lis) id (op (car lis) (foldr op (cdr lis) id)))) ;; Contract: ;; foldr : (‘a * ‘b -> ‘b ) * (list ‘a) * ‘b -> ’b Exercise