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

imported>Administrator
ページの作成:「F#によるフィボナッチ数の記述例。 == 記述例 1 == <source lang="fsharp"> let rec fib x = match x with | x when x <= 0 -> failwith...」
 
imported>Administrator
 
(同じ利用者による、間の2版が非表示)
4行目: 4行目:
<source lang="fsharp">
<source lang="fsharp">
let rec fib x =
let rec fib x =
  match x with
    match x with
  | x when x <= 0 -> failwith "1以上の整数が必要です"
    | 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)
      
printfn "%d" (fib 15)
[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>