ocaml

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

北城余情 提交于 2019-12-02 20:07:23
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 one? Are they equivalent? Can they coexist? Does it make sense to "mix and match" or should I pick one and focus on it? Is Core widely used outside of Jane Street? If it makes a difference I am on Debian, so Windows support is not a factor for me. Thanks! Caveat: I'm one of the authors of Batteries (although I've

create a histogram OCaml

末鹿安然 提交于 2019-12-02 19:16:53
问题 My task is to create a histogram that output the number of times that an element it is in a list. Input:[2;2;2;3;4;4;1] Output[(2, 3); (2, 2); (2, 1); (3, 1); (4, 2); (4, 1); (1, 1)] Expected output : [(2, 3); (3, 1); (4, 2); (1, 1)] My code: let rec count a ls = match ls with |[] -> 0 |x::xs when x=a -> 1 + count a xs |_::xs -> count a xs let rec count a = function |[] -> 0 |x::xs when x=a -> 1 + count a xs |_::xs -> count a xs let rec histo l = match l with |[] -> [] |x :: xs -> [(x, count

Any simpler way to implement non-in-place selection sort in OCaml?

送分小仙女□ 提交于 2019-12-02 18:14:48
问题 I implemented a non-in-place version of selection sort in OCaml. let sort compare_fun l = let rec find_min l' min_l origin_l = match l' with | [] -> if min_l = [] then (min_l, l') else let min = List.hd min_l in (min_l, List.filter (fun x -> if x != min then true else false) origin_l) | x::tl -> if min_l = [] then find_min tl [x] origin_l else let c = compare_fun (List.hd min_l) x in if c = 1 then find_min tl [x] origin_l else if c = 0 then find_min tl (min_l @ [x]) origin_l else find_min tl

Functional Breadth First Search

假如想象 提交于 2019-12-02 17:34:40
Functional depth first search is lovely in directed acyclic graphs. In graphs with cycles however, how do we avoid infinite recursion? In a procedural language I would mark nodes as I hit them, but let's say I can't do that. A list of visited nodes is possible, but will be slow because using one will result in a linear search of that list before recurring. A better data structure than a list here would obviously help, but that's not the aim of the game, because I'm coding in ML - lists are king, and anything else I will have to write myself. Is there a clever way around this issue? Or will I

ocaml type missmatch unit vs list

喜欢而已 提交于 2019-12-02 17:28:28
问题 For this signature val chooser: string list * string list -> string list and this implementation let rec chooser (inputList, trueList) = match inputList with [] -> [] | iH::iT -> if (List.hd trueList)="True" then iH::(chooser iT List.tl trueList) I am getting the following error: Error: This variant expression is expected to have type unit The constructor :: does not belong to unit What am I doing wrong? 回答1: The result of if ... then with no else has to be unit , because the value will be ()

What's really more performant? Haskell or OCaml [closed]

安稳与你 提交于 2019-12-02 17:14:57
I spent the last 18 months getting the grip of functional programming, starting with learning OCaml and for some weeks now Haskell. Now I want to take the next step and implement some actual application: A simple realtime terrain editor. I've written numerous realtime terrain rendering engines, so this is a familiar topic. And the used recursive algorithms and data structures seem very fit for a functional implementation. With this being a realtime application I'm naturally looking for the best performance I can get. Now some (IMHO quite annoying) proponent of OCaml quite frequently bitches

What is the state of OCaml's parallelization abilities?

醉酒当歌 提交于 2019-12-02 17:01:29
I'm interested in using OCaml for a project, however I'm not sure about where its parallelization capabilities are anymore. Is there a message passing ability in OCaml? Is OCaml able to efficiently use more than 1 CPU? Most of what I have read on the subject was written in 2002-2006, and I haven't seen anything more recent. Thanks! huitseeker This 2009 issue of the Caml weekly news ("CWN", a digest of interesting messages from the caml list ) shows that: the official party line on threads and Ocaml hasn't changed. A notable quote: (...) in general, the whole standard library is not thread-safe

List of Functional code snippets for Procedural Programmers? [closed]

北战南征 提交于 2019-12-02 16:49:23
Sometimes I still get stuck trying to translate procedural code into functional code. Is there a list of functional idioms/snippets that are mapped to procedural idioms/snippets? Edit Since there doesn't seem to be a centralized website of these snippets, I am turning this into a community wiki. Please paste any procedural -> functional snippets here. (Edited from this post on fshub ) The first time I went to reach for break/continue in OCaml/F#, it threw me for an (infinite) loop, so to speak, because no such thing exists! In OCaml, one can use exceptions to break from a loop because they are

Try to further understanding the interface/module of OCaml

喜夏-厌秋 提交于 2019-12-02 16:43:08
问题 I understand in OCaml there are concepts of interfaces and module . And I understand how to use them now. However, what I don't understand is how to fully utilise them. For example, in Java, let's say we have a interface Map and we also have Hashtable and HashMap that implement Map . In code, I can do like: Map m = new Hashtable(); m.put("key", value); Someday, if I change my mind, I can change to Hashmap very quickly by changing Map m = new Hashtable(); to Map m = new HashMap(); , right? But

Ocaml and Opam: unbound module Core

心不动则不痛 提交于 2019-12-02 15:24:37
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 ocamlfind and opam search show that the package was installed correctly. However when I try to open it