似乎我一遍又一遍地使用我希望作为函数抽象的模式.模式背后的想法是,我可能有一些东西,如果没有,我可以尝试生产它.这是我对命名感兴趣的函数的一些OCaml代码,但问题不是OCaml特定的.我找了一个Haskell先例,但我没有在Data.Maybe模块中看到这样的函数,并且hoogle也没有帮助:http://www.haskell.org/hoogle/? human = Maybe + b + - %3E +%28a + - %3E +可能+ b%29 + - %3E + a + - %3E +可能+ b.
let my_function a f arg = match a with
| None -> f arg
| Some _ -> a
Run Code Online (Sandbox Code Playgroud)
这几乎就像有一个默认的潜在价值,但如果我们已经有一个值,它就不需要生成默认值.
编辑:
我需要这种类型的原因是我有一个要解决的组合问题和一组解决它的启发式(比如h1和h2).h1比h2快.但是,这些启发式方法都不能保证找到解决方案.所以我将它们链接起来并按顺序尝试.就像是
match my_function (h1 problem) h2 problem with
| None -> "bad luck"
| Some solution -> "hurray"
Run Code Online (Sandbox Code Playgroud) 我有两个文件:gadt1.ml和gadt2.ml,第二个取决于第一个。
gadt1.ml:
type never
type _ t1 = A1 : never t1 | B1 : bool t1
type _ t2 = A2 : string t2 | B2 : bool t2
let get1 : bool t1 -> bool = function B1 -> true
let get2 : bool t2 -> bool = function B2 -> true
Run Code Online (Sandbox Code Playgroud)
gadt2.ml:
let get1 : bool Gadt1.t1 -> bool = function Gadt.B1 -> true
let get2 : bool Gadt1.t2 -> bool = function Gadt.B2 -> true
Run Code Online (Sandbox Code Playgroud)
当我使用ocaml …
我base-unix在OCaml opam存储库中看到了这个包.它被描述为"与OCaml编译器一起分发的Unix库".与OCaml编译器一起分发的一些其他库没有自己的包.例如,没有库的base-str包str.
为什么会有这样的差异?据我所知,在使用ocamlfind或编译时,它们都需要在命令行中指定为包或库ocamlbuild.