Lecture5.6 - 1 10/3/06 From text by R. Norman &...

Info iconThis preview shows pages 1–3. 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 10/3/06 From text by R. Norman & al. 1 DPL-5: Scheme in ML We now turn to a "large" program written in ML. It is NOT really large, just large enough to implement the Scheme interpreter. It makes use of many of the features of ML, and, since several subsequent exercises will require modifying interpreters written in ML, this seems like the least painful way to begin applying what was, supposedly, learned about ML. It should also give you more time to practice ML 10/3/06 From text by R. Norman & al. 2 DPL-5: Scheme in ML To avoid having to do it later, prepare a le with contents (* Libraries that need to be loaded *) load "IO"; load "TextIO"; load "CommandLine"; load "StringCvt"; load "Int"; load "List"; load "ListPair"; Save it as modules.sml , and execute use "modules.sml"; as the rst command of the mosml interpreter. Then use "mlscheme.sml"; 10/3/06 From text by R. Norman & al. 3 DPL-5: Scheme in ML - names and environments. What is a name ?- type name = string; > type name = string What is an environment ?- type 'a env = (name * 'a) list; > type 'a env = (string * 'a) list What is the empty environment ?- val emptyEnv = ; > val 'a emptyEnv = : 'a list- val _ = op emptyEnv : 'a env; 10/3/06 From text by R. Norman & al. 4 DPL-5: Scheme in ML - names and environments. Find a binding for a name in an environment - an environment is just a list of (name, value) two-tuples (call them pairs):- exception NotFound of name; (* what if not there? Allow for a message to be sent back *) > exn NotFound = fn : string -> exn- fun find (name, ) = raise NotFound name | find (name, (n, v)::tail) = if name = n then v else find(name, tail); > val 'a find = fn : string * (string * 'a) list -> 'a 10/3/06 From text by R. Norman & al. 5 DPL-5: Scheme in ML - names and environments. Install a binding for a name in an environment .- fun bind(name, v, rho) = (name, v)::rho; > val ('a, 'b) bind = fn : 'a * 'b * ('a * 'b) list -> ('a * 'b) list Install corresponding lists of names and values in an enviroment .- exception BindListLength; (* mismatch in lists *) > exn BindListLength = BindListLength : exn- fun bindList(n::vars, v::vals, rho) = bindList(vars, vals, bind(n, v, rho)) | bindList(, , rho) = rho | bindList _ = raise BindListLength; > val ('a, 'b) bindList = fn : 'a list * 'b list * ('a * 'b) list -> ('a * 'b) list 10/3/06 From text by R. Norman & al. 6 DPL-5: Scheme in ML - names and environments. Examples:- val testRho = bindList(["a", "b", "c"], [1, 2, 3], ); > val testRho = [("c", 3), ("b", 2), ("a", 1)] : (string * int) list- find("a", testRho); > val it = 1 : int- find("d", testRho); ! Uncaught exception: ! NotFound "d"- bindList(["d"], [4, 5], testRho); ! Uncaught exception: ! BindListLength 2 10/3/06 From text by R. Norman & al. 7 DPL-5: Scheme in ML - names and environments....
View Full Document

This note was uploaded on 02/13/2012 for the course CS 91.531 taught by Professor Giam during the Fall '09 term at UMass Lowell.

Page1 / 15

Lecture5.6 - 1 10/3/06 From text by R. Norman &...

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

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