(* # # Solution by Robert McManus # *) staload "prelude/DATS/list.dats" staload _ = "prelude/DATS/list0.dats" typedef pair = (int, int) fun fprint_pair (out: FILEref, t: pair) = let val () = fprint_string (out, "(") val () = fprint_int (out, t.0) val () = fprint_string (out, ", ") val () = fprint_int (out, t.1) val () = fprint_string (out, ")") in end fun fprint_list_sep (out: FILEref, xs: list0 pair, sep: string): void = let fun loop{a:t@ype} (xs: list0 pair, i: int) : void = case+ xs of | list0_cons (x, xs) => let val () = if i > 0 then fprint_string (out, sep) val () = fprint_pair (out, x) in loop (xs, i + 1) end | list0_nil () => () // end of [loop] in loop (xs, 0) end (* ****** ****** *) extern fun factor (r: int): list0 pair implement factor (r) = let fun loop(r: int, x: int, y:int, xs: list0 pair) : list0 pair = if x < y then xs else let val xy = x*x + y*y val r2 = r*r in if xy > r2 then loop(r, x-1, y, xs) else if xy < r2 then loop(r, x, y+1, xs) else loop(r, x-1, y, list0_cons((x,y), xs)) end // end of [loop] in loop(r, r, 0, list0_nil()) end implement main () = let val xs = factor (5) in fprint_list_sep (stdout_ref, xs, "->"); println! () end // end of [main] (* ****** ****** *) (* end of [quiz1.dats] *)