lec8 Modules and Signatures

lec8 Modules and Signatures - 1 CSE 130 : Fall 2006...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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

This note was uploaded on 02/14/2008 for the course CSE 130 taught by Professor Jhala during the Fall '06 term at UCSD.

Page1 / 4

lec8 Modules and Signatures - 1 CSE 130 : Fall 2006...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online