ocaml

OCaml: Why I can't use this operator infix?

夙愿已清 提交于 2019-12-18 11:27:02
问题 I defined a custom equality operator (the definition is not really important so I will insert dummy stuff): let ( ~=~ ) a b = true If I try to use it infix: if a ~=~ b then 1 else 2 I get the following error: This expression is not a function; it cannot be applied . I can fix this either by renaming the operator from ~=~ to =~ or by calling it as a function: if (~=~) a b then 1 else 2 . This seems that is a general problem with operators that start with ~ . My question is why I can't use such

Know of an OCAML IDE? [closed]

余生长醉 提交于 2019-12-18 11:03:48
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . Know of an OCAML/CAML IDE? Especially one that runs on Linux? 回答1: Emacs in Caml mode, or Tuareg mode, or TypeRex mode. TypeRex adds auto-completion to Taureg in emacs - a really nice feature for people who prefer the more graphical IDE's. 回答2: There is Camelia. You can also integrate OCaml into Eclipse. Also in

Which English tutorial would you advise to learn OCaml? [closed]

女生的网名这么多〃 提交于 2019-12-18 10:41:30
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I want to advertise OCaml to beginners, and I am looking for good tutorials in English; not that you have only heard of, but that you have actually tried and found useful... 回答1: I quite like the book Developing Applications With Objective Caml -- I guess the title should be updated to mirror the 'OCaml' naming

Ocaml and Opam: unbound module Core

蓝咒 提交于 2019-12-18 10:15:27
问题 I'm trying to get an ocaml environment set up, and I've followed the instructions from appendix A of the Real World Ocaml beta. I set up opam, and installed a version of ocaml with the command $ opam switch 4.01.0dev+trunk which passed fine. I then did an $ eval `opam config env` to pull in the changes. I'm running the correct top level, as $ which ocaml outputs /home/bryan/.opam/4.01.0dev+trunk/bin/ocaml I installed the Core package from Jane street, with the command $ opam install core Both

Simple non-optimal unionfind in OCaml

谁说我不能喝 提交于 2019-12-18 09:31:45
问题 I wrote a OCaml program for union find algorithm. This algorithm I wrote is not optimal and is the simplest version. I put my OCaml code here because I am not sure whether this code is good enough or not (despite of the algorithm itself) , although this code can run without errors. This is the first time I wrote a complete working thing after I started to learn OCaml, so please help me by reviewing it. Useful suggestions will help me improving my OCaml skills. Thanks type union_find = {id_ary

How to shuffle list in O(n) in OCaml?

送分小仙女□ 提交于 2019-12-18 05:56:50
问题 It is not hard to shuffle an array in O(n), with in place swapping, How to do it for list in OCaml, with O(n)? Requirement: No array or in place usage Consider this as an interview question 回答1: Lists are immutable, and there's often a log n price to pay for working with immutable data. If you're willing to pay this cost, there's an obvious n log n approach: tag each list element with a random value, sort based on random value, remove random values. This is the way I shuffle lists in my

Is there a way to print user-defined datatypes in ocaml?

試著忘記壹切 提交于 2019-12-18 04:34:09
问题 I can't use print_endline because it requires a string, and I don't (think) I have any way to convert my very simple user-defined datatypes to strings. How can I check the values of variables of these datatypes? 回答1: In many cases, it's not hard to write your own string_of_ conversion routine. That's a simple alternative that doesn't require any extra libraries or non-standard OCaml extensions. For the courses I teach that use OCaml, this is often the simplest mechanism for students. (It

OCaml performance of exceptions

放肆的年华 提交于 2019-12-17 20:44:43
问题 I've often read that exceptions are somewhat slow and should be avoided if performance is an issue (for instance, in Java, F#, etc). Does that apply to common OCaml functions such as Hashtbl.find , which return exceptions for elements not found? In particular, if I want my application to be efficient, should I always test element membership using, for instance, Hashtable.mem before calling Hashtbl.find ? Or would the extra comparison of the mem function negatively impact performance? 回答1:

Integer exponentiation in OCaml

僤鯓⒐⒋嵵緔 提交于 2019-12-17 19:48:30
问题 Is there a function for integer exponentiation in OCaml? ** is only for floats. Although it seems to be mostly accurate, isn't there a possibility of precision errors, something like 2. ** 3. = 8. returning false sometimes? Is there a library function for integer exponentiation? I could write my own, but efficiency concerns come into that, and also I'd be surprised if there isn't such a function already. 回答1: Regarding the floating-point part of your question: OCaml calls the underlying

Implementing a tail recursive version of quicksort-like function in F#/OCaML

故事扮演 提交于 2019-12-17 18:46:01
问题 Is it possible to implement a tail recursive version of the quick sort algorithm (via the continuation pattern)? And if it is, how would one implement it? Normal (not optimized) version: let rec quicksort list = match list with | [] -> [] | element::[] -> [element] | pivot::rest -> let ``elements smaller than pivot``, ``elements larger or equal to pivot``= rest |> List.partition(fun element -> element < pivot) quicksort ``elements smaller than pivot`` @ [pivot] @ quicksort ``elements larger