Created
March 9, 2015 05:21
-
-
Save akimacho/1775acf197a8192b7957 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 第11章 問題 *) | |
(* --- 11.1 --- *) | |
(* 自然数nは *) | |
(* - 0 0,あるいは *) | |
(* - n + 1 ひとつ小さい自然数に1を加えたもの(nが自己参照のケース) *) | |
(* という形 *) | |
(* 目的 : 自然数nまでの2乗の和を求める *) | |
(* sum_of_square : int -> int *) | |
let rec sum_of_square n = | |
if n = 0 then 0 (* 0のケース *) | |
else n * n + sum_of_square (n - 1) (* 1以上のケース *) | |
(* テスト *) | |
let test1 = sum_of_square 0 = 0 | |
let test2 = sum_of_square 1 = 1 | |
let test3 = sum_of_square 2 = 5 | |
let test4 = sum_of_square 3 = 14 | |
let test5 = sum_of_square 4 = 30 | |
# #use "sos.ml" ;; | |
val sum_of_square : int -> int = <fun> | |
val test1 : bool = true | |
val test2 : bool = true | |
val test3 : bool = true | |
val test4 : bool = true | |
val test5 : bool = true | |
(* --- 11.2 --- *) | |
(* 目的 : 数式anの第n項を求める *) | |
(* prog : int -> int *) | |
let rec prog n = | |
if n = 0 then 3 (* 0のケース *) | |
else ( 2 * (prog (n - 1)) - 1 ) (* 1以上のケース *) | |
(* テスト *) | |
let test1 = prog 0 = 3 | |
let test2 = prog 1 = 5 | |
let test3 = prog 2 = 9 | |
let test4 = prog 3 = 17 | |
let test5 = prog 4 = 33 | |
let test6 = prog 5 = 65 | |
# #use "prog.ml" ;; | |
val prog : int -> int = <fun> | |
val test1 : bool = true | |
val test2 : bool = true | |
val test3 : bool = true | |
val test4 : bool = true | |
val test5 : bool = true | |
val test6 : bool = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment