2010-11-28 85 views
1

我想解決我的代碼多個if-else循環。嵌套如果-else循環錯誤 - ocaml

我以前的代碼爲:

let rec appendtolist n list b = 
    let f x = 
     if (b == 0) then x 
     else (append (appendtocode n (List.hd list)) (appendtolist n (List.tl list) (b-1))) 
    in 
     f list 
    ;; 

修改後的代碼與嵌套循環:

let rec appendtolist n list b = 
    let f x = 
     if b < 0 then x 
     else if (b == 0) then appendtocode n (List.hd list) (b-1) 
     else appendtocode n (List.hd list) :: appendtolist n (List.tl list) (b-1) 
    in 
     f list 
    ;; 

但我得到這個錯誤:

This function is applied to too many arguments, maybe you forgot a `;' 

我的代碼似乎是語法正確。這是在OCaml中實現嵌套循環的正確方法嗎? 我接着找到了一個在線查找if-elseif循環的例子,它工作正常。

我需要最終輸出x這是在該函數中所有遞歸調用appendtocodeappendtolist後形成的列表。

我在哪裏錯了?

請指導。

謝謝。

回答

1

在你的第一個代碼示例你打電話appendtocode這樣的:

appendtocode n (List.hd list) 

所以我認爲appendtocode是服用2個參數的函數。所以在這裏

appendtocode n (List.hd list) (b-1) 

你有3個參數調用它:

在第二個你調用它像這樣。由於它只需要兩個參數,因此會收到一條錯誤消息,告訴您您調用的參數太多。

PS:如果語句不是循環。

+0

嗨。謝謝。沒有意識到語法錯誤。我將這個問題轉貼到新帖子中。而不是在這裏覆蓋它。再次感謝。 – JJunior 2010-11-28 21:26:30