ocaml

OCaml Mutex module cannot be found

僤鯓⒐⒋嵵緔 提交于 2019-12-08 03:15:02
问题 I tried to use Mutex module, such as Mutex.create(), but compiler says Unbound module Mutex. Does it require some special namespace? Thanks 回答1: For toplevel : ocaml -I +threads # #load "unix.cma";; # #load "threads.cma";; # Mutex.create ();; - : Mutex.t = <abstr> For ocamlc : ocamlc -thread unix.cma threads.cma src.ml For ocamlopt : ocamlopt -thread unix.cmxa threads.cmxa src.ml For findlib : ocamlfind ocamlc -thread -package threads -linkpkg src.ml 来源: https://stackoverflow.com/questions

OCaml: How to remove all non-alphabetic characters from a string?

十年热恋 提交于 2019-12-08 02:52:08
问题 How do I remove all the non-alphabetic characters from a string? E.g. "Wë_1ird?!" -> "Wëird" In Perl, I'd do this with =~ s/[\W\d_]+//g . In Python, I'd use re.sub(ur'[\W\d_]+', u'', u"Wë_1ird?!", flags=re.UNICODE) Etc. AFAICT, Str.regex does not support \W , \d , etc. (I can't tell whether it supports Unicode, but somehow I doubt it). 回答1: I'm not an expert in regexes and utf, but if I were in your shoes, then I would use re2 library, and this is my first approximation: open Core.Std open

camlzip: “This package relies on external (system) dependencies that may be missing.”

久未见 提交于 2019-12-08 02:01:58
问题 I'm trying to install camlzip (an OCaml package housed in opam), but when I run opam install camlzip I get this: The following actions will be performed: ∗ install camlzip 1.05 =-=- Gathering sources =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= [camlzip] Archive in cache =-=- Processing actions -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= [ERROR] The compilation of camlzip failed at "make all". Processing 1/1: [camlzip: ocamlfind remove] #=== ERROR while installing

The correct way to use Ocamlnet 3 - Http_client.Convenience.http_post

こ雲淡風輕ζ 提交于 2019-12-08 01:42:50
问题 I am trying to use Http_client.Convenience.http_post to make a http post request . The API is fairly simple: val http_post : string -> (string * string) list -> string Does a "POST" request with the given URL and returns the response body. The list contains the parameters send with the POST request. What I wish to do is to construct a http post request to get the flight information via google flights , explained as part 1 in here: http://www.nohup.in/blog/using-json-google-flights To maintain

OCaml - Cannot find graphics.cma

陌路散爱 提交于 2019-12-08 00:57:07
问题 When loading the Graphics module in the toplevel, I get an error saying "Cannot find graphics.cma". I'm using OS X and I'm pretty sure I've installed OCaml correctly since I've been using it for about a month now. So it seems that the Graphics module wasn't included in the OCaml package. How can I fix this issue, or how can I install the Graphics module myself? 回答1: First of all, check Graphics is really installed. This library is optional and therefore it may not be installed. There are

Ocaml Lwt - some implementations of multiplayer game

此生再无相见时 提交于 2019-12-07 23:15:09
问题 I'm going on to writing a simple implementation of tic-tac-toe server (via telnet). The task - players connect to server and after they send START the server looks for a partner who typed START too, and game begins. A piece of code: let handle_income () = let con = Lwt_unix.accept sock in con >>= fun (cli, addr) -> let player = Lwt.return {state = Sleeping; descriptor = Lwt.return cli} in send_to_client player "Welcome to the server. To start game type in START and press Enter"; player;; let

Round-robin algorithm in OCaml

时间秒杀一切 提交于 2019-12-07 22:50:08
问题 This is the followup question of What's the grouping plan so that every two people are grouped together just once? Basically, I implemented the Round robin algorithm. By the algorithm, it can generate pairs list where each possible pair of elements are grouped together exactly once. For example, we have a, b, c, d , then On first day, we do a b c d Then we group like [(a,c);(b,d)]. Then we round it clockwise like a c d b Then we group like [(a,d);(c,b)]. Then we round it clockwise like a d b

OCaml OUnit bracket example: setup and tear-down

泪湿孤枕 提交于 2019-12-07 19:46:20
问题 I do not really get how to use bracket setup and tear-down with OUnit (version 2). Anyone feel like supplying a full example ? Here is the OUnit2.bracket function documentation: val bracket : (test_ctxt -> 'a) -> ('a -> test_ctxt -> unit) -> test_ctxt -> 'a bracket set_up tear_down test_ctxt set up an object and register it to be tore down in test_ctxt. You setup a test suite like this: let test_suite = "suite" >::: [ "test1" >:: test1_fun ] And run it like this: let _ = run_test_tt_main test

How to auto-generate stubs from mli file?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 18:41:29
问题 Suppose I have some interface file mylib.mli like var foo : 'a list -> int val bar : f:('a -> 'b) -> 'a list -> 'b list val baz : f:('a -> bool) -> 'a list -> 'a list val frobnitz : init:'acc -> f:('acc -> 'a -> 'acc) -> 'a list -> 'acc val frobozz : 'a list -> 'a list -> 'a list val quux : 'a list list -> 'a list Is there an automated way to generate the corresponding mylib.ml as a collection of stubs? (By "stub" I mean a "minimal, interface-implementing function".) 回答1: AFAIK, there is no

Using Str module in OCaml top level?

喜你入骨 提交于 2019-12-07 18:07:07
问题 I tried two commands to load Str Module in OCaml top level. The first command gives me the error "Cannot find file Str.cmo". I then tried to use the file i was using in top level with the second command. With that command i got "Reference to undefined global Str". #load "Str.cmo";; #use "my_file.ml";; What do i need to do to successfully load Str module in OCaml top level. 回答1: Modules are "archives", so they are .cma files, not .cmo : # #load "str.cma";; # Str.regexp;; - : string -> Str