This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 1 CSE 130 : Fall 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 8: Modules and Signatures Previously: Polymorphism enables Reuse Can reuse generic functions: map :a * b b * a filter : (a bool * a list) a list rev : a list a list length : a list int swap : a * b b * a sort: (a a bool * a list) a list compose: (a b * c b) c a If function (algorithm) is independent of type, can reuse code for all types ! Today: Divide-Conquer Programming Weve seen very small programs so far 4 yr education chopped into quarters Typical software is thousands, millions LOC How can a PL help to manage complexity ? Only one technique humans know about Rational Numbers Say you want a software package that uses rational numbers heavily Remember rationals ? Ratio of two integers: m/n Well, seems simple enough: Use a pair of integers to represent rationals! OCaml type: int * int Rational Numbers Well, every pair of isnt a rational (5,0) ? 2nd elem denominator must not be zero How do we print a rational ? When are two rationals equal ? (6,4) and (30,20) ? Need to normalize rationals But need to fix to_string Many functions related to type Pack into a module Types, Values, Exceptions bound in Name are : Name.t, Name.x , Name.f Bind module to diff name module Name = struct bindings end module NewName = Rational;; NewName.add;; NewName.to_string;; 2...
View Full Document