「フィボナッチ数/F Sharp」の版間の差分

imported>Administrator
imported>Administrator
 
(同じ利用者による、間の1版が非表示)
5行目: 5行目:
let rec fib x =
let rec fib x =
     match x with
     match x with
     | x when x <= 0 -> 0
     | x when x <= 0I -> 0I
     | 1 -> 1
     | 1I -> 1I
     | 2 -> 1
     | 2I -> 1I
     | x -> fib (x - 1) + fib (x - 2)
     | x -> fib (x - 1I) + fib (x - 2I)
   
   
[0 .. 16] |> Seq.iter (fun x -> printfn "fib(%O) = %O" x (fib x));;
[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>