ocaml

How to implement a dictionary as a function in OCaml?

时间秒杀一切 提交于 2019-12-21 03:33:35
问题 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. 回答1: I think the point of this exercise is to

OCaml |> operator

只谈情不闲聊 提交于 2019-12-21 03:22:11
问题 Could someone explain what the |> operator does? This code was taken from the reference here: let m = PairsMap.(empty |> add (0,1) "hello" |> add (1,0) "world") I can see what it does, but I wouldn't know how to apply the |> operator otherwise. For that matter, I have no idea what the Module.() syntax is doing either. An explanation on that would be nice too. 回答1: Module.(e) is equivalent to let open Module in e . It is a shorthand syntax to introduce things in scope. The operator |> is

llvm OCaml bindings

旧巷老猫 提交于 2019-12-21 03:13:39
问题 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.

Or-patterns in Haskell

此生再无相见时 提交于 2019-12-21 03:13:35
问题 In OCaml, I was used to writing code which looked like: let combine o1 o2 = match o1, o2 with | Valid, Invalid | Invalid, Valid -> Invalid | _ -> ... I didn't find a way to write or-patterns in Haskell and I really miss it. Does anyone have a solution? 回答1: I don't think this is possible in haskell. There are however, a few alternatives: Factor out the common code with a where binding This doesn't make much sense in your example, but is useful if you have more code in the body of the case

First class modules in OCaml 3.12: What kinds of things will they make easier (or possible)?

和自甴很熟 提交于 2019-12-20 11:47:09
问题 I've heard that "first class modules" are coming in OCaml 3.12. What advantages will they offer? What kids of things will be easier? What problem are they trying to solve? A simple example would suffice. 回答1: It's only one possible applications, but first class modules make it easy to encode existential types, with basically a module packing an existential type and a value using this type). For example, See Alain Frisch work on Dynamic types (code taken from Alain Frisch work on dyntypes :

Machine learning in OCaml or Haskell?

天涯浪子 提交于 2019-12-20 07:56:56
问题 I'm hoping to use either Haskell or OCaml on a new project because R is too slow. I need to be able to use support vectory machines, ideally separating out each execution to run in parallel. I want to use a functional language and I have the feeling that these two are the best so far as performance and elegance are concerned (I like Clojure, but it wasn't as fast in a short test). I am leaning towards OCaml because there appears to be more support for integration with other languages so it

Strange module loading issue in OCaml

折月煮酒 提交于 2019-12-20 03:52:50
问题 I have two files: myUnionFind.ml and myUnionFind_test.ml . Both files are in the same directory . myUnionFind.ml open Batteries module type MyUnionFindSig = sig type union_find val print_array : 'a array -> unit val create_union : int -> union_find val union_weighted : union_find -> int -> int -> unit val is_connected_weighted : union_find -> int -> int -> bool end;; module MyUnionFind : MyUnionFindSig = struct let print_array ary = print_endline (BatPervasives.dump ary);; type union_find =

How to combine equal sequence elements (functional programming)?

妖精的绣舞 提交于 2019-12-20 02:44:31
问题 I want to write a function that takes in a sequence <1,1,2,2,3> and returns the sequence with equal elements grouped like <<1,1>, <2,2>, <3>>. I'm using sequences, not lists, but some of the functions are similar. Some of the functions I am thinking of using are map, reduce, tabulate, filter, append etc.. Reduce takes in an associative function and returns the sequence that is "reduced" by that operator. So, reduce op+ 0 <1,2,3> = 6. My first thought was to use map to raise the sequence by

Using Core.Std.List.fold_left without label

你说的曾经没有我的故事 提交于 2019-12-20 02:37:05
问题 I am experimenting with Core's List.fold_left . # List.fold_left;; - : 'a Core.Std.List.t -> init:'b -> f:('b -> 'a -> 'b) -> 'b = <fun> It works fine when I specify the labels: # List.fold_left [1;2;3] ~init:0 ~f:(+);; - : int = 6 But I get a different result when I do not specify the labels: # List.fold_left [1;2;3] 0 (+);; - : init:(int -> (int -> int -> int) -> '_a) -> f:((int -> (int -> int -> int) -> '_a) -> int -> int -> (int -> int -> int) -> '_a) -> '_a = <fun> And other partial

Printing stack traces

感情迁移 提交于 2019-12-20 02:18:06
问题 I have a very short test file: let print_backtrace () = try raise Not_found with Not_found -> Printexc.print_backtrace stdout;; let f () = print_backtrace (); Printf.printf "this is to make f non-tail-recursive\n";; f (); I compile and run: % ocamlc -g test.ml % OCAMLRUNPARAM=b ./a.out Raised at file "test.ml", line 1, characters 35-44 this is to make f non-tail-recursive Why isn't f listed in the stack trace? How can I write a function that will print a stack trace of the location it's