Ocaml 奮戦
Ocamlをさわってみた
> let rec pow x y = if y=1 then x else (pow x (y-1)) * x ;; val pow : int -> int -> int => pow 2,1;; - : (int -> int) * int = ( , 1)
なんか変な結果がでる...
if y=1 then x else (pow x (y-1)) * x ;;
は
(if y=1 then x else (pow x (y-1))) * x ;;
と同等らしくてショック
> let rec pow x y = if y=1 then x else *1 * x) ;; > pow 2,1024;; - : int =1024
が正しそうだ
ほかにも
> let rec add x y = x+y;; val add : int -> int -> int => ( add 3 2*4);; - : int = 20
これって5*4=20 より5+(2*4)=11のほうが普通ぽくない?
*1:pow x (y-1