ocaml

How to get the line number of an exception in OCaml without debugging symbols?

这一生的挚爱 提交于 2019-12-04 10:59:34
Is there a good way to get the line number of exception in OCaml without debugging symbols? Certainly, if we turn on debugging symbols and run with OCAMLRUNPARAM=b , we can get the backtrace. However, I don't really need the whole backtrace and I'd like a solution without debugging symbols. At the moment, we can write code like try assert false with x -> failwith (Printexc.to_string x ^ "\nMore useful message") in order to get the file and line number from assert, but this seems awkward. Is there a better way to get the file and line number of the exception? There are global symbols __FILE__

OCaml Optional Argument

吃可爱长大的小学妹 提交于 2019-12-04 10:47:38
How can I write a function in OCaml in which one or more argument are optional? let foo x y z = if(x+y > z) then true else false;; If foo do not receive the z argument it uses 0 as z . foo 3 3 2 -> true foo 3 3 10 -> false foo 2 1 -> true Is there any OCaml feature to achieve this? OCaml doesn't have optional arguments like you'd find in Java or C#. Since functions can be partially applied, optional arguments can make it hard to tell when you're done passing arguments and would like the function to be evaluated. However, OCaml does have labeled arguments with default values , which can be used

Is there a name for ADT with explicit subtyping?

天大地大妈咪最大 提交于 2019-12-04 10:39:59
I'm looking for a proper name for a data type that combines ADT with explicit subtyping. In one of my applications, I use a structure similar to ADT to represent parse trees, on which I perform recursive pattern matching. I find it rather convenient if I could combine ADT with subtyping, as demonstrated in the example below: Note: the example is written in Haskell's syntax, but this is not Haskell code. data Empty = Empty data Expr = Int Int | Add Expr AddOp Expr data OptionalExpr = | Empty // I want to make Empty a subtype of OptionalExpr | Expr // I want to make Expr a subtype of

On finding documentation

谁都会走 提交于 2019-12-04 09:25:19
So far, giving Google search strings such as "ocaml" regex "ocaml" hashtbl etc. has usually gotten me the documentation I've been looking for. One notable exception has been "ocaml" "String.Map" With this search, all I can find (documentation-wise, that is), is documentation for the String.map function of the String module (or is it "package"?). (If understand correctly, String.Map is a data structure, namely a map whose keys have type string , rather than a function.) Even more forceful Google search specs like "ocaml" intitle:"String.Map" ...failed to find the documentation I'm after. Is

What are the pros and cons of Batteries and Core? [closed]

て烟熏妆下的殇ゞ 提交于 2019-12-04 08:04:18
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . In the OCaml world at present there appear to be a number of competing extensions to the standard library, Batteries and Jane Street Core being the major ones as far as I can determine (I understand that ExtLib has been subsumed into Batteries?). What are the pros and cons of each

Why is writing a compiler in a functional language easier? [closed]

故事扮演 提交于 2019-12-04 07:41:00
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . I've been thinking of this question very long, but really couldn't find the answer on Google as well a similar question on Stackoverflow. If there is a duplicate, I'm sorry for that. A lot of people seem to say that writing compilers and other language tools in functional

Haskell, Scala, Clojure, what to choose for high performance pattern matching and concurrency [closed]

心已入冬 提交于 2019-12-04 07:35:27
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I have started work on FP recently after reading a lot of blogs and posts about advantages of FP for concurrent execution and

Hashtable of mutable variable in Ocaml

℡╲_俬逩灬. 提交于 2019-12-04 07:22:11
I need to use hashtable of mutable variable in Ocaml, but it doesn't work out. let link = Hashtbl.create 3;; let a = ref [1;2];; let b = ref [3;4];; Hashtbl.add link a b;; # Hashtbl.mem link a;; - : bool = true # a := 5::!a;; - : unit = () # Hashtbl.mem link a;; - : bool = false Is there any way to make it works? Mutable variables that may happen to have the same content can still be distinguished because they are stored at different locations in memory. They can be compared with the physical equality operator ( == ). However, OCaml doesn't provide anything better than equality, it doesn't

Using dynamic programming in Haskell? [Warning: ProjectEuler 31 solution inside]

倾然丶 夕夏残阳落幕 提交于 2019-12-04 06:39:41
In solving projecteuler.net's problem #31 [ SPOILERS AHEAD ] (counting the number of ways to make 2£ with the British coins), I wanted to use dynamic programming. I started with OCaml, and wrote the short and very efficient following programming: open Num let make_dyn_table amount coins = let t = Array.make_matrix (Array.length coins) (amount+1) (Int 1) in for i = 1 to (Array.length t) - 1 do for j = 0 to amount do if j < coins.(i) then t.(i).(j) <- t.(i-1).(j) else t.(i).(j) <- t.(i-1).(j) +/ t.(i).(j - coins.(i)) done done; t let _ = let t = make_dyn_table 200 [|1;2;5;10;20;50;100;200|] in

Re-implementing List.map in OCaml/F# with correct side effect order?

断了今生、忘了曾经 提交于 2019-12-04 06:22:40
According to this previous answer You could implement List.map like this: let rec map project = function | [] -> [] | head :: tail -> project head :: map project tail ;; but instead, it is implemented like this: let rec map project = function | [] -> [] | head :: tail -> let result = project head in result :: map project tail ;; They say that it is done this way to make sure the projection function is called in the expected order in case it has side effects, e.g. map print_int [1;2;3] ;; should print 123 , but the first implementation would print 321 . However, when I test both of them myself