首页 > 解决方案 > 如果在 Ocaml 中有很多条件

问题描述

我是初学者,oCaml我对以下功能有错误:

let rec determinant n m1 = 
    if n <= 2 then 
        detMat2 m1
    else 
        let mat = Array.make_matrix (n-1) (n-1) 0 in 
            for ligne = 0 to (n-1) do
                for colonne = 0 to (n-1) do
                    for i = 0 to (n-1) do
                        for j = 0 to (n-1) do
                            if i != (n-1) && j != (n-1) then 
                                else if (i != ligne && j != colonne) then
                                    mat.(i).(j) <- m1.(ligne).(colonne)
                                else if i != ligne && j = colonne then 
                                    mat.(i).(j) <- m1.(ligne).(colonne+1)
                                else if i = ligne && j != colonne then
                                    mat.(i).(j) <- m1.(ligne+1).(colonne)
                                else if i = ligne && j = colonne then 
                                    mat.(i).(j) <- m1.(ligne+1).(colonne+1)                     
                        done                
                    done    
                done
            done;
    determinant (n-1) mat;;

我收到以下错误:

File "s2.ml", line 65, characters 9-13:
Error: Syntax error

谢谢你的帮助 !

标签: ocaml

解决方案


由于您向我们展示的代码摘录中的行数少于 65 行,因此错误消息显然不是您仅从该代码中得到的错误消息。请花时间创建一个mcve

也就是说,紧跟最内层循环的then分支是空的。您不能在 OCaml 中这样做:如果无事可做,则应通过返回(类型的唯一值)明确说明,如iffor()unit

if i != (n-1) && j != (n-1) then ()
else (* do the rest *)

推荐阅读