CS 603: Programming Language Organization Lecture 9 Spring 2003 Department of Computer Science University of Alabama Joel Jones.

Slides:



Advertisements
Similar presentations
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
ISBN Chapter 15 Functional Programming Languages.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 18: Think Globally, Mutate Locally.
14-October-2002cse Lists © 2002 University of Washington1 Lists CSE 413, Autumn 2002 Programming Languages
Chapter 9: Functional Programming in a Typed Language.
CS 603: Programming Language Organization Lecture 7 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
Chapter Fifteen: Functional Programming Languages Lesson 12.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
ISBN Chapter 15 Functional Programming Languages.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
CS 603: Programming Language Organization Lecture 8 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
CS 403: Programming Languages Lecture 12 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CS 603: Programming Language Organization Lecture 1 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
Second Order Programming Intro2CS – week 10B 1. Functions are values too 2.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
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.
CS 603: Programming Language Organization Lecture 6 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
CSE 3302 Programming Languages Chengkai Li Spring 2008 Functional Programming Language: Haskell (cont’d) Lecture 20 – Functional Programming, Spring 2008.
Additional Scheme examples
Functional Programming
CS 550 Programming Languages Jeremy Johnson
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
PPL Lazy Lists.
(defmacro (delay x) (list 'lambda () x))
Lecture 7: List Recursion CS200: Computer Science
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.
Closures and Streams cs784(Prasad) L11Clos
CS 603: Programming Language Organization
Introduction to Functional Programming in Racket
Nondeterministic Evaluation
6.001 SICP Data abstractions
Mini Language Interpreter Programming Languages (CS 550)
Lecture 18.
Lecture #6 מבוא מורחב.
Lecture #8 מבוא מורחב.
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 9: The Great Lambda Tree of Knowledge and Power
Introduction to Functional Programming in Racket
Programming Languages
Announcements Quiz 5 HW6 due October 23
List and list operations (continue).
Lecture # , , , , מבוא מורחב.
CS 403: Programming Languages
CS 403: Programming Languages
*Lecture based on notes from SICP
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

CS 603: Programming Language Organization Lecture 9 Spring 2003 Department of Computer Science University of Alabama Joel Jones

©2003 Joel Jones Outline Questions –Impcore functions in MP1 should return the desired results, not print them Handing in MP1 µ-Scheme (cont.) Reading for next time

©2003 Joel Jones Handing in MP1 The individual pieces are: 1. Code for exercise 4 ( fib ) 2. Code for exercise 5 ( prime, nthprime, subprimes, relprime ) 3. Code for exercise 6 ( binary ) 4. Judgments changed in the operational semantics to accommodate the change in Exerc Code for exercise 22, only the.c and.h files changed. Pieces 1, 2, 3, and 5 should be sent in as attachments. Piece 4 can be turned in either on paper or in a document ed to me.

©2003 Joel Jones µ-Scheme (cont.) Closures –Pair of lambda (function value) and environment « (lambda (y) …), {x |-> l}» –Environment maps name to mutable location ->(val counter-from (lambda (n) (lambda () (set n (+ n 1))))) ->(val ten (counter-from 10)) ->(ten) 11 ->(ten) 12

©2003 Joel Jones µ-Scheme (cont.) Closures Pair Up: Write a function (make-withdraw) that models a bank account balance, where only withdrawals are allowed ->(val make-withdraw (lambda (balance) …)) ->(val W1 (make-withdraw 100)) ->(W1 10) 90

©2003 Joel Jones Simple higher-order functions Composition –(define o (f g) (lambda (x) (f (g x)))) –(define even? (n) (= 0 (mod n 2))) –(val odd? (o not even?)) Pair Up: Write a function (to8th) using composition with square and ? that raises the input to the 8th power -> (define square(x) (* x x)) square -> (val to8th …) -> (to8th 2) 256

©2003 Joel Jones Simple higher-order functions (cont.) Currying –Taking an n-argument function and turning it into a 1-argument function returning a function expecting n-1 arguments (also curried!) –Currying binary functions (define curry (f) (lambda (x) (lambda (y) (f x y)))) (define uncurry (f) (lambda (x y) ((f x) y)))

©2003 Joel Jones Simple higher-order functions (cont.) Currying ->(val zero? ((curry =) 0)) ->(zero? 0) #t ->(val add1 ((curry +) 1)) ->(add1 4) 5 ->(val new+ (uncurry (curry +))) ->(new+ 1 4) 5

©2003 Joel Jones Higher-order functions on lists Filter – (define filter (p? l) (if (null? l) ‘() (if (p? (car l)) (cons (car l) (filter p? (cdr l))) (filter p? (cdr l))))) Exists? – (define exists? (p? l) (if (null? l) #f (if (p? (car l)) #t (exists? p? (cdr l)))))

©2003 Joel Jones Higher-order functions on lists (cont.) All? – (define all? (p? l) (if (null? l) #t (if (p? (car l)) (exists? p? (cdr l)) #f))) Map – (define map (f l) (if (null? l) ‘() (cons (f (car l)) (map f (cdr l))))) Pair Up: How are the arguments of filter, exists?, and all? different from those of map?

©2003 Joel Jones Higher-order functions on lists (cont.) Foldr Foldl

©2003 Joel Jones Higher-order functions for polymorphism Implementing sets –Constructing mono-morphic sets requires definition of equality—simple if primitive elements ->(val emptyset ‘()) ->(define member? (x s) (exists? ((curry equal? x) s)) ->(define add-element (x s) (if (member? X s) s (cons x s))) ->(define union (s1 s2) (foldl add-element s1 s2)) ->(define set-from-list (l) (foldl add-element ‘() l)) Problem is equality for complex types

©2003 Joel Jones Higher-order functions for polymorphism (cont.) Pair Up: Implement a data type for sets which uses the usual list representation. Therefore, all you have to do is implement =set? ->(=set? ‘(a b c) ‘(c b a)) #t Implement sets of sets, where the set functions are passed an eqfun for comparing sets. ->(define member? (x s eqfun) …) ->(define add-element (x s eqfun) …)

©2003 Joel Jones Higher-order functions for polymorphism (cont.) How to construct? –Add parameter to every function –Make part of “object” (set in previous example) –Make list of functions (like C++ templates)

©2003 Joel Jones Reading & Questions for Next Class Chapter 3.11–3.14