0
我正在學習ocaml,並且我正在嘗試編寫簡單的函數,它打印出第一個作爲參數int的第一個。錯誤:未綁定值需要
我已經寫了:
let rec take(number, lista)=
let rec take_acc(number, lista, acc)=
match number with
| 0 -> []
| number < 0 -> []
| number > lista.length -> lista
| number < lista.length -> take_acc(number-1, lista.tl, [email protected]);;
take_acc(number, lista, [])
let listas = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: [];;
take(2,listas);;
的一點是,上面給出的代碼給我錯誤:
Error: Unbound value take
我做錯了嗎?
的一點是,此代碼:
let xxl = 11 :: 33 :: 54 :: 74 :: [];;
let rec take2 (ile,xxx) =
if ile=0 || ile<0 then []
else
if ile>(List.length xxl) then take2(ile-1,xxx)
else
List.hd xxx :: take2(ile-1,List.tl xxx);;
哪裏是區別beetwen這兩個程序?
編輯: 由於傑弗里斯科菲爾德的建議,我已經寫了這樣的事情:
let rec take2(ilosc, lista) =
let rec take_acc(ilosc, lista, acc) =
if ilosc = 0 || ilosc < 0 then []
else
if ilosc > List.length lista
then lista
else
take_acc(ilosc-1, lista.tl, [email protected]);;
let listas = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: [];;
take2(2,listas);;
還是一樣。
謝謝。我編輯了我的答案。你能幫我評論一下嗎? – Ojmeny 2015-03-18 22:23:58
這在語法上仍然不正確。我建議你將語法正確性作爲你的第一個目標。 – 2015-03-18 22:38:47