(define log2
(lambda (n) (if (= n 1) 0 (+ 1 (log2 (quotient (+ n 1) 2))))))
我想學習如何在OCaml中創建一個函數,到目前爲止我已經想出了這個。將Log2球拍功能改寫成OCaml功能?
let rem x y = (* I found this helper function from google*)
let rec aux acc i n =
if i=n then acc else
if acc+1=y then aux 0 (i+1) n else
aux (acc+1) (i+1) n in
aux 0 0 x;;
let rec log2 x =
match x with
| 1 -> 0
| 1 + log2 (rem x+1 2);;
我找不出我使用的最後一個表達式。我想知道添加1的方法,並調用log2函數「 + log2(rem x + 1 2);;」
您需要將'x + 1'加括號。 –
@JeffreyScofield,你是對的。 – Kakadu
你是對的我需要使用商不餘數。 – user2884707bond