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

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

F28PL1 Programming Languages Lecture 14: Standard ML 4.
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.
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.
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.
6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
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.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
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.
Chapter 15: Functional Programming Languages
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
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
Chapter 9: Functional Programming in a Typed Language.
Functional Programming in Scheme and Lisp.
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.
Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
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 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming: Lisp MacLennan Chapter 10.
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.
Recursion Higher Order Functions CSCE 314 Spring 2016.
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 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
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.
Functional Programming
CS 550 Programming Languages Jeremy Johnson
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Lecture 7: List Recursion CS200: Computer Science
6.001: Structure and Interpretation of Computer Programs
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
Introduction to Functional Programming in Racket
6.001 SICP Data abstractions
FP Foundations, Scheme In Text: Chapter 14.
Data abstraction, revisited
Lecture #8 מבוא מורחב.
Lecture #9 מבוא מורחב.
Introduction to Functional Programming in Racket
Announcements Quiz 5 HW6 due October 23
Lecture #7 מבוא מורחב.
List and list operations (continue).
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
Common Lisp II.
CS 403: Programming Languages
Programming Languages
Lisp.
Lecture 8: Recursing Lists CS150: Computer Science
Presentation transcript:

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

©2004 Joel Jones Outline Questions S-expressions µ-Scheme (cont.) Reading for next time

©2004 Joel Jones S-expressions s-expression – a symbol, a number, or a list (S 1, …, S n ) of zero or more S-expressions nil list – list of zero elements ()

©2004 Joel Jones Operations on S-Expressions: Review –car: first element of list or ‘() –cdr: everything in list except first element –cons: if S = (S 1,…,S n ), then (cons S’ S) is (S’ S 1,…,S n ) Predicates –=: returns #t if equal, #f otherwise, for non-lists and empty lists –number?, symbol?, list?, null?: #t if argument of type, #f otherwise – : #t if both numbers and condition holds, #f otherwise Math –+, -, *, /: like impcore

©2004 Joel Jones Syntax (changes from impcore) value ::= integer | quoted-const value-op ::= + | - | * | / | = | | cons | car | cdr | number? | symbol? | list? | null? | print quoted-const ::= ‘ S-expression S-expression ::= integer | symbol | ( S- expression* ) symbol ::= name

©2004 Joel Jones Example of a list using function (define length (l) (if (null? l) 0 (+ 1 (length (cdr l)))))

©2004 Joel Jones Let’s play at the board again (No Books!) (list1 x) - list of one element, x (list2 x y) - list of two elements, x and y (list3 x y z) - list of three elements, x, y, and z (atom? x) - #t if null, number, or symbol, #f otherwise (equal l1 l2) - #t if two equal atoms or equal lists, #f otherwise. Two lists are equal if same length and corresponding elements are equal.

©2004 Joel Jones Larger LISP Example Calculate prime numbers less than n using Sieve of Eratosthenes.

©2004 Joel Jones Let’s Play at the Board (but your books can’t play) Insertion sort–given a list of n elements, sort the last n-1 recursively, then insert the first in its proper position. (define insert (x l) … (define insertion-sort (l) …

©2004 Joel Jones Association Lists association list(a-list)–a list of the form ((k 1 a 1 ), …, (k m a m )), where the k i are symbols (called keys) and the a i are attributes. retrieve data: (define assoc (x alist) (if (null? alist) ‘() (if (= x (caar alist)) (cadar alist) (assoc x (cdr alist)))))

©2004 Joel Jones Association Lists (cont.) add data (define mkassoc (x y alist) (if (null? alist) (list1 (list2 x y)) (if (= x (caar alist) (cons (list2 x y) (cdr alist)) (cons (car alist) (mkassoc x y (cdr alist))))))

©2004 Joel Jones Let’s play at the board again (No Books!) Property lists–a-list where attribute is an attribute list Examples –(set fruits ‘((apple ((texture crunch))) (banana ((color yellow))))) –(getprop ‘apple ‘texture fruits) crunchy Write (getprop x p plist), where x is the individual, p is the property and plist is the property list

©2004 Joel Jones Let’s play at the board again (No Books!) (putprop x p y plist) give individual x value y for property p in plist –(set fruits (putprop ‘apple ‘color ‘red fruits)) ->((apple ((texture crunchy)(color red)))(banana ((color yellow))))

©2004 Joel Jones µ-Scheme 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

©2004 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

©2004 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

©2004 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)))

©2004 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

©2004 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)))))

©2004 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?

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

©2004 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

©2004 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) …)

©2004 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)

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