COP 4020 Lecture -*- Outline -*- * Declarative Programming Techniques (Ch 3) Based on Peter van Roy and Seif Haridi's book, "Concepts, Techniques, and Models of Computer Programming" (MIT Press, 2004), where all references that are not otherwise attributed are found. ** self-test ------------------------------------------ SELF-TEST FOR CHAPTER 3 Motivation [EvaluateModels] When is declarative programming useful? Definition [Concepts] How is declarative programming defined? Iteration [Concepts] [UseModels] Make the following iterative: fun {Product Ls} case Ls of E|Es then E*{Product Es} else 1 end end Recursion [UseModels] Suppose ::= zero | succ ( ). Write Mult: }: > that multiplies two s. Recursion [UseModels] Write DeleteFirst: }: > such that {DeleteFirst Sought Ls} returns a new list that is like Ls, but without the first occurence of Sought in Ls (if any). Recursion [UseModels] Suppose ::= boolLit( ) | intLit( ) | subExp( ) | equalExp( ) | ifExp( ) Write the following Eval: }: > such that {Eval subExp(intLit(5) intLit(4))} = intLit(1) Difference Lists [Concepts] [EvaluateModels] What are difference lists useful for? What are their limitations? Procedural abstraction [UseModels] How can you make a statement S into a procedure? Genericity [UseModels] Generalize the following two procedures by making an abstraction of them both: fun {Preorder T} case T of leaf then nil [] tree(key: L value: V left: Left right: Right) then (L#V) | {Append {Preorder Left} {Preorder Right}} end end fun {IncTree T} case T of leaf then leaf [] tree(key: L value: V left: Left right: Right) then tree(key: L value: 1+V left: {IncTree Left} right: {IncTree Right}) end end Use the generalization to write Preorder and IncTree. Instantiation [Concepts] [UseModels] Write a curried version of foldTree. Use that to write Preorder and IncTree. ADTs [Concepts] How is an ADT different from a data type specification? Data abstraction [Concepts] What is an invariant property of data? Security [Concepts] [EvaluateModels] What kinds of problems could happen if an ADT is implemented as a collection of functions? Encapsulation [Concepts] [EvaluateModels] How does encapsulation relate to these problems? How does the name mechanism preserve encapsulation? Read only views [Concepts] [EvaluateModels] Why are read-only views needed in the declarative model? Development [UseModels] How would you develop a small application? Modules [Concepts] [MapToLanguages] What is a module? What is it like in C, C++, and Java? Functors [Concepts] [MapToLanguages] What is a functor? What is it like in C, C++, and Java? Linking [Concepts] [UseModels] How are modules linked? ------------------------------------------