(* Works in SML/NJ on omega: /opt/sml/bin/sml or at http://www.smlserver.org/smltojs_prompt/ *) fun gcd2(a,b)= if a=b then a else if a>b then gcd2(a-b,b) else gcd2(b-a,a);; (* This version incorrectly uses /. Caught by type inferencing fun gcdExtHelp((u1,u2,u3),(v1,v2,v3))= if v3=0 then (u1,u2,u3) else let val q=u3/v3 in gcdExtHelp((v1,v2,v3),(u1-v1*q,u2-v2*q,u3-v3*q)) end;; *) (* This version correctly uses div. Type inferencing makes everything an int.*) fun gcdExtHelp((u1,u2,u3),(v1,v2,v3))= if v3=0 then (u1,u2,u3) else let val q=u3 div v3 in gcdExtHelp((v1,v2,v3),(u1-v1*q,u2-v2*q,u3-v3*q)) end;; fun gcdExt(u,v)=gcdExtHelp((1,0,u),(0,1,v));; fun both(a,b)= let val (u1,u2,u3)=gcdExt(a,b) in "gcd of " ^ Int.toString(a) ^ " and " ^ Int.toString(b) ^ " is " ^ Int.toString(gcd2(a,b)) ^ " " ^ Int.toString(a) ^ "*" ^ Int.toString(u1) ^ " + " ^ Int.toString(b) ^ "*" ^ Int.toString(u2) ^ " = " ^ Int.toString(u3) ^ " = " ^ "gcd(" ^ Int.toString(a) ^ "," ^ Int.toString(b) ^ ")" ^ "\n" end;; print(both(1870,1045));; print(both(210,231));;