Download Programming Languages and Techniques ... and more Exercises Algorithms and Programming in PDF only on Docsity!
Programming Languages
and Techniques
(CIS120)
Lecture 39
Semester Recap
Announcements
HW9: Game – Due tonight at 11:59pm
Final Exam:
Tuesday, December 17 th 6:00-8:00 PM
4 Locations:
Meyerson B1 Last Names A - L
Towne 100 Last Names M - Sh
Skirkanich Auditorium Last Names Si – W
Moore 216 Last Names X - Z
Makeup exam offered Weds. December 18 th
required by registrar for exam conflicts
time: noon – 2:00PM
sign up form on course web page
CIS 120 Recap
From Day 1
CIS 120 is a course in program design
Practical skills:
ability to write larger (~1000 lines) programs
increased independence ("working without a recipe")
test-driven development, principled debugging
Conceptual foundations:
common data structures and algorithms
several different programming idioms
focus on modularity and compositionality
derived from first principles throughout
It will be fun!
Promise: A challenging
but rewarding course.
CIS 120 Concepts
13 concepts in 38 lectures
Interface vs. Implementation
Concept: Type abstraction hides the actual implementation of a data structure, describes a data structure by its interface (what it does vs. how it is represented), supports reasoning with invariants
Examples: Set/Map interface (HW3), queues in OCaml and Java, encapsulation and access control
Why?
Flexibility: Can change the implementation without modifying clients
Correctness: Can preserve invariants about the implementation Invariants are a crucial tool for reasoning about data structures: 1. Establish the invariants when you create the structure. 2. Preserve the invariants when you modify the structure. 1 3 0 abstract view 1 0 3 < > concrete representation BST:
Testing
Concept: Write tests before coding
"test first" methodology
Examples:
Simple assertions for declarative programs (or subprograms)
Longer (and more) tests for stateful programs / subprograms
Informal tests for GUIs (can be automated through tools)
Why?
Tests clarify the specification of the problem
Helps you understand the invariants
Thinking about tests informs the implementation
Tests help with extending and refactoring code later
Industry practice; useful for coordinating teams
Persistent data structures
Concept: Store data in persistent, immutable structures; implement computation as transformations of those structures
Examples: immutable lists and trees in OCaml (HW 1/2/3), images, Strings, Streams in Java (HW 7/9)
Why?
Simple model of computation, similar to mathematics
Simple interface: Don't have to reason about aliasing (no implicit communication between various parts of the program, all interfaces are explicit)
Recursion amenable to mathematical analysis (CIS 160/121)
Plays well with parallelism Recursion is the natural way of computing a function f(t) when t belongs to an inductive data type:
Determine the value of f for the base case(s).
Compute f for larger cases by combining the results of recursively calling f on smaller cases.
Same idea as mathematical induction (a la CIS 160)
Concept: Tree Structured data
Lists (i.e. “unary” trees)
Simple binary trees
Trees with invariants: e.g. binary search trees
Widget trees: screen layout + event routing
Swing components
Why? Trees are ubiquitous in CS! - file system organization - languages, compilers - domain name hierarchy www.google.com let rec length (l:int list) : int = begin match l with | [] - > 0 | _::tl - > 1 + length(tl) end
Types, Generics, and Subtyping
Concept: Static type systems prevent many errors. Every expression has a static type, and OCaml/Java use the types to rule out buggy programs. Generics and subtyping make types more flexible and allow for better code reuse.
Why?
Easier to fix problems indicated by a type error than to write a test case and then figure out why the test case fails
Promotes refactoring: type checking ensures that basic invariants about the program are maintained let rec contains (x:’a) (l:’a list) : bool = begin match l with | [] - > false | h::tl - > x = a || (contains x tl) end
Mutable data
Concept: Some data structures are ephemeral : computations mutate them over time