CS 403: Programming Languages

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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.
Fall 2008Programming Development Techniques 1 Topic 19 Mutable Data Objects Section 3.3.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Data Abstraction… The truth comes out…. What we’re doing today… Abstraction ADT: Dotted Pair ADT: List Box and Pointer List Recursion Deep List Recursion.
6.001 SICP SICP – October Introduction Trevor Darrell 32-D512 Office Hour: W web page:
Quiz: Box and Pointer fun! (cons (cons (cons ‘hey (cons ‘there nil)) nil) (cons ‘wow nil)) (list ‘boo (append (list ‘hoo ‘hoo) (cons ‘see ‘me)))
Cs1120 Fall 2009 David Evans Lecture 20: Programming with State.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
David Evans Class 13: Quicksort, Problems and Procedures CS150: Computer Science University of Virginia Computer Science.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
CS 403: Programming Languages Lecture 6 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
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
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
David Evans CS200: Computer Science University of Virginia Computer Science Class 17: Mutation M. C. Escher, Day and Night.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
CS 614: Theory and Construction of Compilers Lecture 7 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
Abstraction: Procedures as Parameters CMSC Introduction to Computer Programming October 14, 2002.
David Evans CS200: Computer Science University of Virginia Computer Science Class 16: Mutation M. C. Escher, Day and Night.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 19: Environments.
CS 403: Programming Languages Lecture 12 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CS 603: Programming Language Organization Lecture 9 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 4: Programming with Data.
CS 614: Theory and Construction of Compilers Lecture 8 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
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.
David Evans CS200: Computer Science University of Virginia Computer Science Lecture 8: Cons car cdr sdr wdr.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Lecture 4: Metacircles Eval Apply David Evans
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
6.001 SICP Object Oriented Programming
Lecture 7: List Recursion CS200: Computer Science
CS 614: Theory and Construction of Compilers
Racket CSC270 Pepper major portions credited to
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.
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
6.001 SICP Data abstractions
Lecture 8: Recursion Practice CS200: Computer Science
Lecture 6: Programming with Data CS150: Computer Science
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
Lecture 11: All Sorts CS200: Computer Science University of Virginia
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Data abstraction, revisited
Lecture 16: Tables and OOP
Lecture 26: The Metacircular Evaluator Eval Apply
Streams, Delayed Evaluation and a Normal Order Interpreter
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
Cs1120 Fall 2009 David Evans Lecture 10: Fracturing Fractals cs1120 Fall 2009 David Evans
Lecture 13: Assignment and the Environment Model (EM)
Lecture 15: Quicker Sorting CS150: Computer Science
Lecture 11: Sorting Grounds and Bubbles
Lecture 8: Recursing Lists CS150: Computer Science
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

CS 403: Programming Languages Lecture 14 Fall 2003 Department of Computer Science University of Alabama Joel Jones

Overview Higher order functions for polymorphism Lecture 14 ©2003 Joel Jones

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

HOF for Polymorphism: Parameter per Function (define member? (lambda (x s eqfun) (exists? ((curry eqfun) x) s))) (define add-element (lambda (x s eqfun) (if (member? x s eqfun) s (cons s x)))) So uses will look like: (member? X s equal?) or (add-element x s =alist?) This part is really difficult for the students. I need to have them read ahead of time and do labs. Look at the Little Schemer to see if they have any helpful exercises. Lecture 14 ©2003 Joel Jones

Association Lists (define bind (lambda (x y alist) (if (null? alist) (list1 (list2 x y)) (if (equal? x (caar alist)) (cons (list2 x y) (cdr alist)) (cons (car alist) (bind x y (cdr alist)))))) (define find (lambda (x alist) (if (null? alist) ‘() (car (cdar alist)) (find x (cdr alist)))))) Lecture 14 ©2003 Joel Jones

HOF for Polymorphism: Part of “object” ->(define mk-set (lambda (eqfun elements) (cons eqfun elements))) ->(define eqfun-of (lambda (set) (car set))) ->(define elements-of (lambda (set) (cdr set))) ->(define emptyset (lambda (eqfun) (mk-set eqfun ‘()))) Lecture 14 ©2003 Joel Jones

HOF for Polymorphism: Part of “object” Pair Up: • What was type of the function “exists?” ? (define exists? (lambda (p? L)…) (define member? (lambda (x s) (exists? (curry (eqfun-of s)) x) (elements-of s)))) (define add-element? (lambda (x s) (if (member? x s) s (mk-set (eqfun-of s) (cons x (elements-of s)))))) Lecture 14 ©2003 Joel Jones

HOF for Polymorphism: Part of “object” So uses will look like: (define alist-empty (emptyset =alist?)) (define s (add-element ‘((U Thant) (I Ching)) alist-empty) returns (<procedure> ((U Thang) (I Ching))) (define s (add-element ‘((Hello Dolly)))) (<procedure> ((Hello Dolly) (U Thang) (I Ching))) (member? ‘((I Ching))) #t Advantage is that only constructor (here, emptyset) requires extra parameter Lecture 14 ©2003 Joel Jones

Sorting (define insert (lambda (x l) (if (null? l) (list1 x) (if (< x (car l)) (cons x l) (cons (car l) (insert x (cdr l))))))) (define insertion-sort (lambda (l) (if (null? l) ‘() (insert (car l) (sort (cdr l)))))) (insertion-sort ‘(4 3 2 6 8 5)) -> (2 3 4 5 6 8) Lecture 14 ©2003 Joel Jones