5 October 2001IFIP WG 2.31 preparation start drscheme set language level to Beginner open in Presentations –length1.ss –planets0.ss –lambda.ss –convert.ss add teachpack convert.ss, then throw out (easy to find)
5 October 2001IFIP WG 2.32 Why do They Still Teach Scheme When Haskell is So Much Better? Matthias Felleisen (PLT) Northeastern University Rice University Houston, Texas
5 October 2001IFIP WG 2.33 What does the question mean? Programmers in industry? College students in senior year? –need buzz words/current technology College students in first year? –need systematic design skills
5 October 2001IFIP WG 2.34 Haskell vs Scheme: Two Different Universes conventional syntax type system –partitions call-by-need controlled effects parenthesized, prefix unitype system –predicates call-by-value full power of effects
5 October 2001IFIP WG 2.35 Haskell and Scheme: One Approach to Teaching layered languages higher-order functions design via classes of values emphasize composition over effects views of PLT not necessarily the general Scheme community
5 October 2001IFIP WG 2.36 Haskell loves Scheme, Scheme loves Haskell :-) The Commonalties outweigh the differences. So whats the true question?
5 October 2001IFIP WG 2.37 Why do They Still Teach Scheme or Haskell When Java and C# are So Fashionable ?
5 October 2001IFIP WG 2.38 Outline answer both questions PLTs TeachScheme! perspective national and international efforts to expose college and high school teachers to program design TeachHaskell?
5 October 2001IFIP WG 2.39 PLTs TeachScheme! Program Context: introductory programming at college and high school level introduce two important ideas: –program design –model(s) of computation write significant code –reaches non-majors as well as majors
5 October 2001IFIP WG PLTs TeachScheme! Program Scheme: simple syntax, simple semantics DrScheme: a PDE for beginners How to Design Programs (MIT, 2001) –the structure and organization of programs (systematic program design)
5 October 2001IFIP WG Part I: The Programming Language
5 October 2001IFIP WG Programming Language: Scheme Schemes notation is simple: –(, operation, operands, ) Schemes semantics is easy: –its just the rules of mathematics: 1+1 = 2 f(12) = 12 * = … With Scheme, we can focus on ideas
5 October 2001IFIP WG Programming Language: Scheme Again simple syntax simple semantics powerful PE rich language its a lie! more lies! do you believe this? so where are the GUIs?
5 October 2001IFIP WG length1 returns 0, no matter what input algebraic syntax
5 October 2001IFIP WG more algebraic syntax an error message concerning procedures, whatever those things are
5 October 2001IFIP WG Syntax is a Problem simple notational mistakes produce strange results -- without warning simple notational mistakes produce error messages beyond the students knowledge even in Scheme there are just too many features for focus on design
5 October 2001IFIP WG Programming Languages: Not One, Many language 1: first-order functional PL –functions and structures –simple conditional expressions language 2: higher-order functional PL –local function definitions –higher-order functions language 3: functions and effects –set! and structure mutation
5 October 2001IFIP WG Beginning Student Scheme (define (f x) (+ (* 5 (square x)) (* 3 x) 27)) (define-struct dollar (amount)) (define-struct planet (name picture)) ;; sign : RealNumber Symbol (define (sign x) (cond [(> x 0) +] [(= x 0) =] [(< x 0) –])))
5 October 2001IFIP WG Intermediate Student Scheme ;; h : Num (listof Num) (listof Num) (define (h D alon) (local ((define (h-aux l) (cond [(empty? l) empty] [else (cons (+ (first s) D) (h-aux (rest s)))]))) (h-aux alon))) ;; d/dx : (Num Num) (Num Num) (define (d/dx f) (local ((define (f-prime x) (/ (- (f (+ x eps)) (f (- x eps))) 2 eps)) f-prime))
5 October 2001IFIP WG Advanced Student Scheme (define counter 0) (define (bump) (set! bump (+ bump 1))) (define (zero-all boxes) (cond [(empty? boxes) (void)] [else (begin (set-box! (first boxes) 0) (zero-all (rest boxes)))]))
5 October 2001IFIP WG Lesson on Programming Languages arrange programming language in pedagogic layers … put students into a knowledge- appropriate context … focus on design ideas relative to this context
5 October 2001IFIP WG Part II: The Programming Environment
5 October 2001IFIP WG The Programming Environment one PE for all languages –supplemental code that does not conform to the language level interactive – ensure model-view separation (MVC) exploration of –program structure –program evaluation
5 October 2001IFIP WG DrScheme: Demo language level: catching simple errors pictures as values lexical structure, alpha renaming evaluation as algebraic reduction teachpacks: model-view separation –more in a moment …
5 October 2001IFIP WG Part III: Program Design Methods
5 October 2001IFIP WG Program Design for Beginners foster basic good habits in beginners emphasize systematic design of programs deemphasize input/output
5 October 2001IFIP WG Design Recipes to be designed inout How do we wire the program to the rest of the world?
5 October 2001IFIP WG Design Recipes to be designed inout read-from-console/compute/print file I/o graphical user interface (GUI) common gateway interface (CGI) etc.
5 October 2001IFIP WG Design Recipes to be designed inout How do we wire the program to the rest of the world? IMPERATIVE: Teach Model-View Separation
5 October 2001IFIP WG Design Recipes understand classes of data –representation of (external) information as data in your favorite language understand program as function –that is triggered by events –that consumes/produces data most important: connect class definition and function definition
5 October 2001IFIP WG The Basic Design Recipe data analysis and class definition contract, purpose statement, header in-out (effect) examples function template function definition testing, test suite development
5 October 2001IFIP WG From Class Definitions to Function Templates the structure class definitions the structure of function templates
5 October 2001IFIP WG Design Recipes: Class Definitions use rigorous language, not formalism naïve set theory –basic sets: numbers, chars, booleans –intervals –(labeled) products, that is, structures –(tagged) unions –self-references –mutual references –vectors (natural numbers)
5 October 2001IFIP WG Design Recipes: Class Definitions (2) (define-struct spider (name size legs)) A spider is a structure: (make-spider symbol number number)
5 October 2001IFIP WG Design Recipes: Class Definitions (3) A zoo animal is either a spider an elephant a giraffe a mouse … Each of these classes of animals has its own definition
5 October 2001IFIP WG Design Recipes: Class Definitions (4) A list of zoo animals is either empty (cons animal a-list-of-zoo-animals) Lets make examples: empty (by definition) (cons (make-spider Asterix 1 6) empty) (cons (make-spider Obelix 99 6) (cons … …))
5 October 2001IFIP WG Design Recipes: Class Definitions (5) (define-struct child (name father mother)) A family tree is either unknown (make-child symbol a-family-tree a-family-tree-2) Many, if not most, interesting class definitions are self-referential.
5 October 2001IFIP WG Design Recipes: Function Templates (Structure) a function template reflects the structure of the class definitions this match helps –designers guide the process –readers comprehend –teachers diagnose weaknesses –modifiers/maintainers analyze or change
5 October 2001IFIP WG Design Recipes: Templates (2) is it a basic class? is it a union? is it a structure? is it self-referential? domain knowledge case analysis extract field values annotate for recursion
5 October 2001IFIP WG Design Recipes: Templates (3) A list of zoo animals is either empty (cons animal a-list-of-zoo-animals) ;; fun-for-zoo : list-of-zoo-animals -> ??? (define (fun-for-zoo a-loZA) … ) is it a union?
5 October 2001IFIP WG Design Recipes: Templates (4) A list of zoo animals is either empty (cons animal a-list-of-zoo-animals) ;; fun-for-zoo : list-of-zoo-animals -> ??? (define (fun-for-zoo a-loZA) (cond [ > > ] [ > > ])) what are the sub-classes
5 October 2001IFIP WG Design Recipes: Templates (5) A list of zoo animals is either empty (cons animal a-list-of-zoo-animals) ;; fun-for-zoo : list-of-zoo-animals -> ??? (define (fun-for-zoo a-loZA) (cond [ (empty? a-loZA) > ] [ (cons? a-loZA) > ])) are any of the potential inputs structures?
5 October 2001IFIP WG Design Recipes: Templates (6) A list of zoo animals is either empty (cons animal a-list-of-zoo-animals) ;; fun-for-zoo : list-of-zoo-animals -> ??? (define (fun-for-zoo a-loZA) (cond [ (empty? a-loZA) … ] [ (cons? a-loZA) … (first a-loZA) … … (rest a-loZA) … ])) is the class definition self-referential?
5 October 2001IFIP WG Design Recipes: Templates (7) A list of zoo animals is either empty (cons animal a-list-of-zoo-animals) ;; fun-for-zoo : list-of-zoo-animals -> ??? (define (fun-for-zoo a-loZA) (cond [ (empty? a-loZA) … ] [ (cons? a-loZA) … (first a-loZA) … … (rest a-loZA) … ]))
5 October 2001IFIP WG Design Recipes: Defining Functions templates remind beginners of all the information that is available –which cases –which field values, argument values –which natural recursions are computed the goal of function definitions is –to compute with the available values –to combine the computed effects
5 October 2001IFIP WG Design Recipes: Overview basic data, intervals of numbers structures unions self-reference in class description mutual references generative recursion special attributes: –accumulators –effects abstraction of designs
5 October 2001IFIP WG Design Recipes: Conclusion get students used to discipline from DAY ONE use scripted question-and-answer game until they realize they can do it on their own works well as long as class definitions are standard
5 October 2001IFIP WG Part IV: From Scheme to Java Training OO Programmers
5 October 2001IFIP WG Scheme to Java: OO Computing focus: objects and method invocation basic operations: –creation –select field –mutate field select method via polymorphism structures and functions basic operations: –creation –select field –mutate field –recognize kind f(o) becomes o.f()
5 October 2001IFIP WG Scheme to Java: OO Programming develop class and interface hierarchy allocate code of function to proper subclass develop class definitions allocate code of function to proper cond-clause
5 October 2001IFIP WG Scheme to Java: Class Hierarchy A list of zoo animals is either empty (cons animal a-list-of-zoo-animals) List of zoo animals EmptyCons: animal list of zoo animals
5 October 2001IFIP WG Scheme to Java: Code Allocation ;; fun-for-zoo : list-of-zoo-animals -> ??? (define (fun-for-zoo a-loZA) (cond [ (empty? a-loZA) ] [ (cons? a-loZA) … (first a-loZA) … … (rest a-loZA) … ])) List of zoo animals Empty: Cons: animal list of zoo animals
5 October 2001IFIP WG Scheme to Java: Ketchup & Caviar abstract class List_Zoo_Animal { int fun_for_list(); } class Cons extends List_Zoo_Animal { Zoo_Animal first; List_Zoo_Animal rest; int fun_for_list() { return 1 + rest.fun_for_list(); } class Empty extends List_Zoo_Animal { int fun_for_list() { return 0; }
5 October 2001IFIP WG Scheme to Java: Ketchup & Caviar abstract class List_Zoo_Animal { int fun_for_list(); } class Cons extends List_Zoo_Animal { Zoo_Animal first; List_Zoo_Animal rest; int fun_for_list() { return 1 + rest.fun_for_list(); } class Empty extends List_Zoo_Animal { int fun_for_list() { return 0; }
5 October 2001IFIP WG Scheme to Java the design recipes work step for step for the production of OO programs the differences are notational the differences are instructive
5 October 2001IFIP WG Why not just Java first? complex notation, complex mistakes no PE supports stratified Java design recipes drown in syntax
5 October 2001IFIP WG Part V: Experiences
5 October 2001IFIP WG Experiences: Rice Constraints life-long learners accommodate industry long-time enable students to gain industry experience after two semesters no trends, no fashion oo programming components until recently: –C++ now more and more: –Java, true OOP
5 October 2001IFIP WG Experiences: The Rice Experiment beginners: no experience, up to three years of experience comp sci introduction: TeachScheme curriculum good evaluation huge growth many different teachers applied comp introduction: C/C++ curriculum weak evaluations little growth several teachers second semester: OOP, classical data structures, patterns
5 October 2001IFIP WG Experiences: The Rice Experiment Faculty with preferences for C/C++ state that students from the Scheme introduction perform better on exams and projects in second course than students from the C/C++ introduction Students with lots of experiences eventually understand how much the course adds to their basis
5 October 2001IFIP WG Experiences: Secondary Schools trained around 200 teachers/professors 120+ deployed the curriculum, reuse it better basis for second courses much higher retention rate –especially among females
5 October 2001IFIP WG Part V: Teach Haskell ?
5 October 2001IFIP WG Social Theorem 1: Beginners make mistakes. Corollary: Teaching efforts must help beginners and teachers find and help mistakes in designs, notation, and evaluations.
5 October 2001IFIP WG Social Theorem 2: To beginners, its about two things: the computer and the human. Corollary: Adding intermediate layers doesnt help.
5 October 2001IFIP WG Haskells Key Features conventional syntax types lazy data controlled effects see theorem 1 see theorem 2 yet another recipe too difficult, interesting
5 October 2001IFIP WG Working with Haskell (1) queens_kill :: Int -> [Int] -> Bool -- determine whether a queen can be placed in file lcon, -- places the preceding files of the board queesn_kill locn places = helper places 1 where helper [] _ = False helper (hd:tl) offset = lcon == hd || -- same file lcon == hd + offset || -- right diagonal lcon == hd - offset || -- left diagonal helper tl offset + 1
5 October 2001IFIP WG Working with Haskell (2) Reading file: `nq.hs`: Type checking ERROR: `nq.hs` (line 6): Bool is not an instance of class `Num` line 6: helper [] _ = False
5 October 2001IFIP WG Working with Haskell (3) queens_kill :: Int -> [Int] -> Bool -- determine whether a queen can be placed in file lcon, -- places the preceding files of the board queesn_kill locn places = helper places 1 where helper [] _ = false helper (hd:tl) offset = lcon == hd || -- same file lcon == hd + offset || -- right diagonal lcon == hd - offset || -- left diagonal helper tl offset + 1
5 October 2001IFIP WG Working with Haskell (3) queens_kill :: Int -> [Int] -> Bool -- determine whether a queen can be placed in file lcon, -- places the preceding files of the board queesn_kill locn places = helper places 1 where helper [] _ = false helper (hd:tl) offset = lcon == hd || -- same file lcon == hd + offset || -- right diagonal lcon == hd - offset || -- left diagonal helper tl (offset + 1)
5 October 2001IFIP WG Keys to TeachScheme!s Success: on to Haskell tower of languages pde support for (1) other pde support design recipes –emphasis on classes of values tower of types tower of notation pde support for both easy adapt for lazy data adapt for controlled effects
5 October 2001IFIP WG Conclusions
5 October 2001IFIP WG Conclusion training good programmers does not mean starting them on currently fashionable languages provide a strong, rigorous foundation in data-oriented, class-oriented thinking then, and only then, expose to fashion
5 October 2001IFIP WG Conclusion students wish to intern during summer OOP languages are here to stay constraints on introductory courses: –students likely to have some experience –students need to see some OOP language –non-standard students check out CS
5 October 2001IFIP WG Conclusion teach Scheme for the first semester teach an OOP language for second teaches students: –about classes of values –rigorous program development –interests non-standard students
5 October 2001IFIP WG My Ideal Scenario TeachScheme!: for speedy start (O)CAML: for types (and classes) Haskell: for reasoning about effects and lazy data Java/C#: for classes and real world Felleisen Harper Hudak Steele
5 October 2001IFIP WG The End
5 October 2001IFIP WG Credits Findler Flanagan Flatt Krishnamurthi Cartwright Clements Friedman Steckler