CS 342 Lecture -*- Outline -*- * Introduction to Scheme and functional programming ** paradigms, aspects of programming langauges (in the 1992 version of the class, where we did Scheme after Smalltalk and Prolog) ** already seen all the major paradigms (styles): functional (LISP, and will see again in Scheme) Obj-oriented (CLU, Smalltalk) logic based (Prolog) other aspects of the languages we've studied? imperative (assignment, mutation) Chapter 1, CLU, Smalltalk vs. applicative (no mutation or assignment) pure LISP, Prolog, Scheme procedural (tell how to do everything) all except Prolog vs. declarative Prolog abstract data type support: CLU, Smalltalk message passing, inheritance: Smalltalk larger values: LISP, (CLU and Smalltalk? Prolog?), Scheme but *not* chapter 1 Now we return to the world of list processing (LISP) and take up the largest values of all: functions. *** Scheme: functions are the largest values graph of a function = a set of ordered pairs, can be infinite! rule for constructing any part so functions are the ultimate data structure that is, can use functions to simulate all other data structures (perhaps not as fast...) functions also the ultimate control structure can use functions to simulate all other control structs ** programming lessons *** already seen LISP: how to write recursive functions base, inductive case make program structure match structure of data CLU: the utility of data abstraction information hiding (keep rep details in their place) think about types Prolog: run-time efficiency isn't the only thing want to write programs efficiently too constructive definitions can be turned into programs Smalltalk: the power of polymorphism (message passing and subclasses) want to define a general vocabulary for organizing ADTs *** Scheme: never write the same thing twice a motto for programmers applications: the subroutine abstract algorithm the abstract data type abstract data inheritance abstract data defn. polymorphism (message passing) abstract algorithm suppose we write quite similar algorithms many times: program (in Scheme syntax): copy (a list) length append highlight the common parts "test for null, do something with the car recurse on the cdr, put the results together somehow" also look at the other flat-recursive programs in chapter 1: remove-multiples, sieve (p. 31) insert, assoc (page 32) gatherprop (page 33) member?, union (page 34) enqueue (p. 37) inter, diff, include-rows, nth (p. 43) etc. want to be able to define just one function that can do all of this want a general tool for making flat-recursive functions 1. want to write it down once 2. think about it's correctness once and for all 3. use it thereafter the combine function on page 102 (which will explain later) is that one function. could just add "combine" to the language, but it's specialized for lists what about other types of data? (sets, bags, arrays, ...) want one general tool building primitive instead of many datatype specific non-tool primitives ** Scheme vs. LISP expressions essentially the same, but include lambda now look at examples on page 96-97, look at syntax on page 97 define replaced can have expressions (variables, applications) in operator position best if don't use set to change values of variables best if ignore while loop