Exam1F05Hints

# Exam1F05Hints - Design of Programming Languages - Fall 2005...

This preview shows pages 1–3. Sign up to view the full content.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Design of Programming Languages - Fall 2005 First Exam : Hints and solutions 1. (10 pts) ML and types. Consider the types, data and code exception emptyGraph of string; type vertex = {ID: int, MARK: bool ref} ref; type edge = vertex * vertex; type graph = {vertices : vertex list, edges : edge list}; fun mkGraph (, es) = raise emptyGraph(&quot;Empty Graph: no vertices given&quot;) | mkGraph (vs, es) = {vertices= vs, edges= es}; val v1 = ref {ID=1, MARK= ref false}; val v2 = ref {ID=2, MARK= ref false}; val v3 = ref {ID=3, MARK= ref false}; val v4 = ref {ID=4, MARK= ref false}; val v5 = ref {ID=5, MARK= ref false}; val v6 = ref {ID=6, MARK= ref false}; val vs = [v1,v2,v3,v4,v5,v6]; val es = [(v1,v2),(v1,v4),(v2,v3),(v5, v2),(v3,v4),(v4,v5),(v5, v1),(v5, v4)]; val g1 = mkGraph(vs, es); fun filter f = | filter f (x::y) = if (f x) then x::(filter f y) else filter f y; fun DFT (g:graph, start:vertex) = let val ga as {vertices = vs, edges = es} = g fun unMark = true | unMark ((x:vertex)::xs) = (#MARK(!x) := false; unMark xs) fun outEdges v = filter (fn (v1, v2) =&gt; (!v1 = !v)) es fun auxDFT st = if (not (!(#MARK(!st)))) then (#MARK(!st):= true; st::(foldr (op @) (map (fn x =&gt; auxDFT x) (map #2 (outEdges st))))) else in (unMark vs; auxDFT start) 1 end; (8 pts) Comment the code and data above. The major point here was to write down what the code does, in particular outEdges and auxDFT . I would suggest thinking about the functions and, possibly experimenting with running the code. Many people just transliterated the code - without discussing the functionality at all (e.g., what does foldl DO ?. What is really happening in st::(foldr (op @) (map (fn x =&gt; auxDFT x) (map #2 (outEdges st)))) (2 pts) Why do we need to use reference types? One reason (the MARK field) is that the algorithm depends on marking the vertices visited (there are other algorithms that do not - can you think of one?). The other one (vertices are ref objects), comes from both efficiency (the by-value discipline copies the locations but not the contents) and the desire (usually need - you would use the result of the DFT for some other purposes, like topological sorting) to keep the changed values on the originals, so you can distinguish them from vertices that...
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 / 5

Exam1F05Hints - Design of Programming Languages - Fall 2005...

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

View Full Document
Ask a homework question - tutors are online