OCaml 问题循环循环



我正在编写程序来计算贝尔数,这是我在OCaml的第一个大程序。我想在循环中使用循环 While 时,但存在语法错误。请更正。谢谢。

我正在使用网站 http://try.ocamlpro.com/

let rec factorial n =   
if n < 2 
   then 1
else 
   n * factorial(n-1)
let rec newton n k =
factorial n / (factorial k * factorial (n-k))
let bell = [|1;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0|]
let i = ref 2
let k = ref 0
let x = ref 0
let z = ref 0
let s = ref 0

在这里,您需要选择要计算的数字,例如4

let n = ref 4
if !n != 0 || !n != 1 then
    while !i <= !n do   
         while !k <= (!i-1) do
                x := newton (!i-1) !k;
                s := !s + (!x * bell.(!k));
                k := !k + 1 ;
                z := !k + 1
             done   
      s:=0;
      i:= !i + 1;
   done
else 
bell.(!n)<-1
  • 应该使用 Num 来计算贝尔数字,但我首先我想让程序在 int 上工作

您可以尝试在 1st done 之后添加;

最新更新