Skip to content

Commit

Permalink
Completed hw7 excersizes 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
roca committed Mar 18, 2013
1 parent f1ce230 commit b410106
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions sml_ruby/hw7.sml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ datatype geom_exp =
| Intersect of geom_exp * geom_exp (* intersection expression *)
| Let of string * geom_exp * geom_exp (* let s = e1 in e2 *)
| Var of string
| Shift of real * real * geom_exp
(* CHANGE add shifts for expressions of the form Shift(deltaX, deltaY, exp *)

exception BadProgram of string
Expand Down Expand Up @@ -181,6 +182,15 @@ fun intersect (v1,v2) =
* line segments are not actually points (endpoints not real close)
* lines segment have left (or, if vertical, bottom) coordinate first
*)
fun reposition (deltaX,deltaY,e) =
case e of
NoPoints => e (* first 5 cases are all values, so no computation *)
| Point (x,y) => Point(x+deltaX,y+deltaY)
| Line (m,b) => Line (m,(b+deltaY)-(m*deltaX))
| VerticalLine x => VerticalLine (x+deltaX)
| LineSegment (x1,y1,x2,y2) => LineSegment (x1+deltaX,y1+deltaY,x2+deltaX,y2+deltaY)



fun eval_prog (e,env) =
case e of
Expand All @@ -190,11 +200,31 @@ fun eval_prog (e,env) =
| VerticalLine _ => e
| LineSegment _ => e
| Var s =>
(case List.find (fn (s2,v) => s=s2) env of
NONE => raise BadProgram("var not found: " ^ s)
| SOME (_,v) => v)
(case List.find (fn (s2,v) => s=s2) env of
NONE => raise BadProgram("var not found: " ^ s)
| SOME (_,v) => v)
| Let(s,e1,e2) => eval_prog (e2, ((s, eval_prog(e1,env)) :: env))
| Intersect(e1,e2) => intersect(eval_prog(e1,env), eval_prog(e2, env))

(* CHANGE: Add a case for Shift expressions *)
| Shift(deltaX,deltaY,e) =>
(case e of
Var s => reposition (deltaX,deltaY,eval_prog(e,env))
| _ => reposition (deltaX,deltaY,e))


(* CHANGE: Add function preprocess_prog of type geom_exp -> geom_exp *)
fun preprocess_prog (e) =
case e of
NoPoints => e
| Point _ => e
| Line _ => e
| VerticalLine _ => e
| LineSegment(x1,y1,x2,y2) =>
if real_close_point (x1,y1) (x2,y2)
then Point (x1,y1)
else if x1 <= x2 andalso y1 <= y2
then e
else LineSegment(x2,y2,x1,y1)
| Var s => e
| Shift(deltaX,deltaY,e) => reposition (deltaX,deltaY,e)

0 comments on commit b410106

Please sign in to comment.