====================================================================== 2005年度「計算機ソフトウェア工学」 ML入門 練習問題 ====================================================================== 通常の方法で起動した状態のocamlにおいて、次の式の値は何になるか。ただ し、何らかのエラーになったり評価が停止しないときは×と書け。 1. 1 + 2 2. 1.2 +. 3.0 *. 4.0 3. "abc" + 3.14 4. let pi = 3.14 in 2 * 5 * pi 5. let x = 1 in let x = x + x in x * x 6. let x = "1" in let x = 3 in x * x 7. let x = 2 * x - 3 in x + 7 8. let x = let x = 1 + 2 in x + 3 in x * 4 9. let x = let y = 1 + 2 in y + 3 in x * y 10. let x = let y = 1 + 2 in y + 3 in let y = x in x * y 11. let f x = x *. x *. 3.14 in f 10 12. let d x y = sqrt (x *. x +. y *. y) in d 3.0 4.0 13. let f x = x + x in let g x = x * x in f (g 3) 14. let f x = x + x in let f x = x * f (x - 1) in f 3 15. let f x = x * f (x - 1) in f 3 16. let rec f x = x * f (x - 1) in f 3 17. let rec f x = if x = 0 then 1 else x * f (x - 1) in f 3 18. let rec f x = if x = 0 then 1 else x * f (x - 1) in f (-3) 19. let f y = x + y in let x = 7 in f x 20. let x = 7 in let f y = x + y in f x 21. let x = 7 in let f y = x + y in let x = 3 in f x 22. let f x y = x * x + y in let g = f 3 in g 4 - g 5 23. let f (x, y) = x * x + y in let g = f 3 in g 4 - g 5 24. let f (x, y) = x * x + y in let g y = f (3, y) in g 4 - g 5 25. let f x y = x * x + y in let g y = f 3 y in g 4 - g 5 26. let f x y = x * x + y in let g y = f (3, y) in g 4 - g 5 27. (fun x y -> x + y * 2) 3 5 28. (fun (x, y) -> x + y * 2) 3 5 29. let f = fun x y -> x + y * 2 in f (3, 5) 30. let f g x = g (g x) in let h y = y * 2 in f h 3 31. let f g x = g (g x) in let h y = y * 2 in f h 3.0 32. let f g x = g (g x) in f (fun y -> y * 2) 3 33. (+) 1 2.3 34. (+.) 1.2 3.45 35. let f g = fun (x, y) -> g y x in f (-) (3, 7) 36. let f g = fun x y -> g y x in f (-) 3 7 37. let f g = fun x -> fun y -> g y x in f (-) 3 7 38. let f g = fun x y -> g y x in f (-) (3, 7) 39. let f = fun g x y -> g y x in f (-) 3 7 40. let f g x y = g y x in f (-) 3 7 以下では、あらかじめ type int_tree = ILeaf of int | INode of int_tree * int_tree type float_tree = FLeaf of float | FNode of float_tree * float_tree type 'a tree = Leaf of 'a | Node of 'a tree * 'a tree と定義されていることにする。 41. INode(123 + 456, 789) 42. FNode(FLeaf 1.23, FLeaf (4.5 +. 6.7)) 43. Node(Leaf (12 + 3), Leaf 4.5) 44. Node(Node(Leaf 3, Leaf 5), Leaf 7) 45. FLeaf(1.23, 4.5) 46. let t = Node(Node(Leaf 2, Leaf 3), Leaf 5) in match t with Leaf x -> x | Node(y, z) -> y + z 47. let rec f t = match t with Leaf x -> x | Node(l, r) -> f r ^ " " ^ f l in f (Node(Node(Leaf "this", Leaf "a"), Node(Leaf "is", Leaf "pen"))) 48. let rec f t = match t with Leaf x -> x | Node(y, z) -> y ^ z in f (Node(Node(Leaf "this", Leaf "a"), Node(Leaf "is", Leaf "pen"))) 49. let rec f t = match t with Leaf x -> x | Node(l, r) -> f r ^ " " ^ f l in f (Node(Node(Leaf 2, Leaf 3), Node(Leaf 5, Leaf 7))) 50. let rec f t = match t with ILeaf x -> x | INode(l, r) -> f r + f l in f (Node(Node(Leaf 2, Leaf 3), Node(Leaf 5, Leaf 7)))