This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: 1 10/3/06 1 DPL4-a: a "largish program" The "SOLVER" in ML. We transliterate from μ Scheme - hopefully this will give us a way to transition from one language to the other with a bit more ease… 10/3/06 2 DPL4-a: a "largish program" We start with the alist functions, with some care about the different rules for identiFer formation. (define sub-alist? (al1 al2) (not (exists? (lambda (pair) (not (equal? (cadr pair) (find (car pair) al2)))) al1))) We have to make a number of decisions, since ML provides a much richer built-in family of objects - for example a pair could be a 2-tuple. Having made this decision, we can start with the implementation of find . Rather than try to use ML as much as possible, we transliterate from Scheme (both good and bad…) 10/3/06 3 DPL4-a: a "largish program" Not quite -- in Scheme find can return nil or a value of any type - this is NOT possible in ML. We can solve the problem in (at least) one of two ways: use an exception , or use an OPTION . exception NotFound; fun find name = raise NotFound | find name ((x, y)::z) = if x = name then y else find name z; 10/3/06 4 DPL4-a: a "largish program" Since bind must always return an environment, the raising of the NotFound exception must force the evaluation of the right branch of the if : we change NotFound into a false . fun bind var value envir = if ((find var envir) = value) handle NotFound => false then envir else ((var, value)::envir) (* TEST- bind (variableOf (SYMBOL("x"))) (satisfyingValue (SYMBOL("x"))) [("x", false)];...
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.
- Fall '09