「フィボナッチ数/F Sharp」の版間の差分
< フィボナッチ数
ナビゲーションに移動
検索に移動
imported>Administrator 細 (→記述例 1) |
imported>Administrator 細 (→記述例2) |
||
(同じ利用者による、間の1版が非表示) | |||
5行目: | 5行目: | ||
let rec fib x = | let rec fib x = | ||
match x with | match x with | ||
− | | x when x <= | + | | x when x <= 0I -> 0I |
− | | | + | | 1I -> 1I |
− | | | + | | 2I -> 1I |
− | | x -> fib (x - | + | | x -> fib (x - 1I) + fib (x - 2I) |
− | [ | + | [0I .. 15I] |> Seq.iter (fun x -> printfn "fib(%O) = %O" x (fib x));; |
+ | </source> | ||
+ | |||
+ | == 記述例2 == | ||
+ | <source lang="fsharp"> | ||
+ | let fib x = | ||
+ | let rec loop acc1 acc2 = | ||
+ | function | ||
+ | | x when x <= 0I -> acc1 | ||
+ | | x -> loop acc2 (acc1 + acc2) (x - 1I) | ||
+ | loop 0I 1I x | ||
+ | |||
+ | [0I .. 15I] |> Seq.iter (fun x -> printfn "fib(%O) = %O" x (fib x));; | ||
</source> | </source> | ||
2012年4月26日 (木) 14:26時点における最新版
記述例 1[編集 | ソースを編集]
let rec fib x =
match x with
| x when x <= 0I -> 0I
| 1I -> 1I
| 2I -> 1I
| x -> fib (x - 1I) + fib (x - 2I)
[0I .. 15I] |> Seq.iter (fun x -> printfn "fib(%O) = %O" x (fib x));;
記述例2[編集 | ソースを編集]
let fib x =
let rec loop acc1 acc2 =
function
| x when x <= 0I -> acc1
| x -> loop acc2 (acc1 + acc2) (x - 1I)
loop 0I 1I x
[0I .. 15I] |> Seq.iter (fun x -> printfn "fib(%O) = %O" x (fib x));;
関連項目[編集 | ソースを編集]
参考文献[編集 | ソースを編集]