LoginSignup
2
2

More than 5 years have passed since last update.

OCamlでprintfを少し改造する

Posted at
# error "`%s' is defined as function\n" "foo"
error: `foo' is defined as function
- : unit = ()

みたいなerror関数は次のように定義できる:

let error fmt = Printf.printf ("error: " ^^ fmt) ;;

結構悩んでしまったが、簡単なことだった。

補足

^^はPervasivesモジュールの関数でformat6を連結する働きをする。

# "%d" ^^ "%s" ;;
- : (int -> string -> '_a, '_b, '_c, 'd, '_d, '_a) format6 = <abstr>

format6というのが出てきたが、もともとformatはformat4のエイリアスで、さらにformat4はformat6のエイリアスになっている。

type ('a, 'b, 'c, 'd) format4 = ('a, 'b, 'c, 'c, 'c, 'd) format6
type ('a, 'b, 'c) format = ('a, 'b, 'c, 'c) format4

特に何も考えずformatを繋げてるという理解でいいんではないだろうか……

参考

OCaml 標準ライブラリ探訪 #3.0: Printf: 便利だけどいろいろ謎のある奴
Module Pervasives

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2