CS 403: Programming Languages Lecture 12 Fall 2003 Department of Computer Science University of Alabama Joel Jones
Lecture 12©2003 Joel Jones2 Overview Announcements Higher-order functions on lists XSLT Higher order functions for polymorphism
Lecture 12©2003 Joel Jones3 Science and Engineering Institutes Summer 2004 Become a globally-engaged researcher. Spend eight weeks conducting research and experiencing life in Japan, Korea, Taiwan, China, Australia For U.S. Graduate Students Sponsor: U.S. National Science Foundation Application deadline: December 23,
Lecture 12©2003 Joel Jones4 Higher-order functions on lists (cont.) Foldl Pair Up: Write the definitions of foldl
Lecture 12©2003 Joel Jones5 But how is this useful? See Handout sum product exists alltrue maximum minimum
Lecture 12©2003 Joel Jones6 Pair Up: Write the definitions for the map function using foldl Let’s try this technique map f L – apply f to each element of L and return a list of the results map: ( ) x list list Example: (map even? ‘(1 2 3)) = (#f #t #f)
Lecture 12©2003 Joel Jones7 XSLT as a Functional Programming Language XML—eXtensible Markup Language XSL—eXtensible Stylesheet Language XSLT—eXtensible Stylesheet Language Transformations A W3C standard for transforming XML documents into other XML documents or other formats. This was conceived as part of XSL but has been found to have wider applications. See handout
Lecture 12©2003 Joel Jones8 Higher-order functions for polymorphism Implementing sets Constructing mono-morphic sets requires definition of equality—simple if primitive elements ->(val 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 12©2003 Joel Jones9 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) …)
Lecture 12©2003 Joel Jones10 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)