宿題再び
let rec downto0 n = if n = 0 then [0] else n :: downto0 (n-1);; let rec zip list1 list2 = match list1,list2 with [],_ -> [] | _,[] -> [] | (x::xs),(y::ys) -> (x,y)::zip xs ys;; let sqrt_int x = int_of_float (sqrt (float_of_int x));; let squares r = List.filter (fun (x,y)-> x*x + y*y = r) (pair (sqrt_int r));; let rec pair n = if n == 0 then [] else (List.map (fun x-> (n,x)) (downto0 (n-1))) @ pair (n-1);;
動くには動くんだけど、ちょっと遅すぎる。ちぇ、やっぱfold_right
を使ったバージョンで行くしかないか。