Drkcore

09 07 2009 OCaml Tweet

今日のOCaml

文法事項 -OCaml-より

let rec fact = function
      0 -> 1
    | n -> n * fact (n-1);;

これは、

let rec fact' =
  (fun x -> match x with
    0 -> 1
  | n -> n * fact' (n-1));;

さて、Objective Caml入門で

同じ名前の関数を宣言することで隠すのが,Objective Camlプログラミングの常套テクニックとして使われる.

とあって、

# let rec fact (n, res) = if n = 1 then res else fact (n - 1, res * n);;
val fact : int * int -> int = <fun>
# let fact n = fact (n, 1);;
val fact : int -> int = <fun>

あーc++のオーバーロードみたいなもんか?と思ったんだけどどうも違うらしい。

# let f (x,y) = x + y;;
val f : int * int -> int = <fun>
# let f x y = x +. y;;
val f : float -> float -> float = <fun>

こんなのは動かない。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021