ocaml

How to implement a dictionary as a function in OCaml?

烂漫一生 提交于 2019-12-03 10:40:28
I am learning Jason Hickey's Introduction to Objective Caml . Here is an exercise I don't have any clue First of all, what does it mean to implement a dictionary as a function ? How can I image that? Do we need any array or something like that? Apparently, we can't have array in this exercise, because array hasn't been introduced yet in Chapter 3 . But How do I do it without some storage? So I don't know how to do it, I wish some hints and guides. I think the point of this exercise is to get you to use closures. For example, consider the following pair of OCaml functions in a file fun-dict.ml

What's the reason of 'let rec' for impure functional language OCaml?

时间秒杀一切 提交于 2019-12-03 10:30:01
问题 In the book Real World OCaml, the authors put why OCaml uses let rec for defining recursive functions. OCaml distinguishes between nonrecursive definitions (using let) and recursive definitions (using let rec) largely for technical reasons: the type-inference algorithm needs to know when a set of function definitions are mutually recursive, and for reasons that don't apply to a pure language like Haskell, these have to be marked explicitly by the programmer. What are the technical reasons

ocaml pretty printer (code formatter)

瘦欲@ 提交于 2019-12-03 10:29:35
I am looking for a code formatter or pretty printer for ocaml. Something like gofmt for the go programming language. It should preferably preserve comments. I am correcting hand-ins and some of the code is formatted in a way that makes it very hard to read. if you don't care about comments, you can use camlp4: camlp4 <file> -parser o -printer o > <new-file> Or you can use external indenter tools, as ocp-indent . 来源: https://stackoverflow.com/questions/12798723/ocaml-pretty-printer-code-formatter

OCaml Printf.sprintf

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Why does this behavior occur? # Printf.sprintf ("Foo %d %s") 2 "bar";; - : string = "Foo 2 bar" # Printf.sprintf ("Foo %d" ^ " %s") 2 "bar";; Printf.sprintf ("Foo %d" ^ " %s") 2 "bar";; Error: This expression has type string but an expression was expected of type ('a -> 'b -> 'c, unit, string) format = ('a -> 'b -> 'c, unit, string, string, string, string) format6 I would expect that the string concatenation would be evaluated first, so everything will proceed as normal. Does this have to do with the type system trickery that Printf employs?

How to use -thread compiler flag with ocamlbuild?

做~自己de王妃 提交于 2019-12-03 09:53:03
I am using Jane Street 's async_core by adding package(async_core) in _tags . When I use ocamlbuild -use-ocamlfind -I src test/test_airport.native , it gives me the following error: camlfind ocamlopt -linkpkg -package async_core -package unix -package netclient -package mongo -package xml-light src/airport.cmx test/test_airport.cmx -o test/test_airport.native ocamlfind: Error from package `threads': Missing -thread or -vmthread switch I googled it and here is what I got http://caml.inria.fr/pub/docs/manual-ocaml-4.00/manual039.html It says: Programs that use system threads must be linked as

llvm OCaml bindings

时光总嘲笑我的痴心妄想 提交于 2019-12-03 09:28:21
I'm working on llvm OCaml bindings. I installed llvm package through opam ( opam install llvm ), when I use llvm in utop, I get the following error: #require "llvm";; Error: The external function 'llvm_global_succ' is not available. The opam llvm version is 3.2. I also tried building llvm3.3 from the official site ( ./configure --with-ocaml-libdir='ocamlc -where' ), the build was successful (all the llvm command-line tools are working), but I got the same error in utop. I'm on Mac OS 10.7.5. Edit: I solved it.. ocamlmktop -o llvmtop llvm.cma -cc g++ Then launch llvmtop , you can use llvm

OCaml warning 31, compiler-libs, and ppx

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I'm porting my application from OCaml 4.02.3 to 4.03.0. Say you have the following in lexer.ml : type t = T [@ @deriving sexp ] let () = sexp_of_t |> ignore ; print_endline "hai" I'm trying to run it as following: ocamlbuild - use - ocamlfind - pkg ppx_sexp_conv - cflags '-w @a-4-31' lexer . byte -- But I'm getting the following error: Warning 31 : files lexer . cmo and / Users / vladimir /. opam / 4.03.0+flambda / lib / ocaml / compiler - libs / ocamlcommon . cma ( Lexer ) both define a module named Lexer File "_none_" , line 1 :

What is a principal type?

﹥>﹥吖頭↗ 提交于 2019-12-03 08:42:59
问题 The OCaml compiler has a "-principal" option and the term "principal type" is sometimes mentioned in the mailing list. What exactly does it mean? The definition in Wikipedia is recursive, as it assumes the reader is already familiar with the notion. 回答1: The process of type inference is the fact of guessing, given an user-written program, what the type of this program is. In general, there may be several correct types for a given program. For example, the program fun x -> x can be given the

Ocaml: Lazy Lists

匿名 (未验证) 提交于 2019-12-03 08:41:19
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: How can I make a lazy list representing a sequence of doubling numbers? Example: 1 2 4 8 16 32 回答1: Using streams: let f x = Stream . from ( fun n -> Some ( x * int_of_float ( 2.0 ** float_of_int n ))) or let f x = let next = ref x in Stream . from ( fun _ -> let y = ! next in next := 2 * y ; Some y ) Using a custom lazy_list type: type 'a lazy_list = | Nil | Cons of ' a * 'a lazy_list lazy_t let rec f x = lazy (Cons (x, f (2*x))) 回答2: The great blog enfranchised mind has a great article on this topic: http://enfranchisedmind.com

What are type quantifiers?

久未见 提交于 2019-12-03 08:33:51
问题 Many statically typed languages have parametric polymorphism. For example in C# one can define: T Foo<T>(T x){ return x; } In a call site you can do: int y = Foo<int>(3); These types are also sometimes written like this: Foo :: forall T. T -> T I have heard people say "forall is like lambda-abstraction at the type level". So Foo is a function that takes a type (for example int), and produces a value (for example a function of type int -> int). Many languages infer the type parameter, so that