ocaml

Break a loop in OCaml

末鹿安然 提交于 2019-12-30 11:28:09
问题 I often need to break a loop in OCaml, there are at least two ways: (* by exception *) try for i = 0 to 100 do ... if cond then raise BreakLoop done; ... with BreakLoop -> ... (* by while *) let cond = ref false in let i = ref 0 in while (not !cond) && (i<= 100) do ... i := !i + 1 done; if !cond then ... What I care most is the optimisation of running time, as long as the program can be easily read and understood. The way while makes loops complicated when there are several nested loops. I

How to take product of two list in OCaml?

风格不统一 提交于 2019-12-30 10:04:13
问题 I have two lists : let a = ["a";"b"]; let b = ["c";"d"]; I want an output list c such as : c = ["a";"c";"a";"d";"b";"c";"b";"d"]; How to do it in ocaml as lists are immutable? I am new to it. 回答1: You would return a new list. If you really are interested in the cartesian product of the lists, then this should be enough: let cartesian l l' = List.concat (List.map (fun e -> List.map (fun e' -> (e,e')) l') l) # cartesian ["a";"b"] ["c";"d"];; - : (string * string) list = [("a", "c"); ("a", "d");

How to take product of two list in OCaml?

旧城冷巷雨未停 提交于 2019-12-30 10:02:53
问题 I have two lists : let a = ["a";"b"]; let b = ["c";"d"]; I want an output list c such as : c = ["a";"c";"a";"d";"b";"c";"b";"d"]; How to do it in ocaml as lists are immutable? I am new to it. 回答1: You would return a new list. If you really are interested in the cartesian product of the lists, then this should be enough: let cartesian l l' = List.concat (List.map (fun e -> List.map (fun e' -> (e,e')) l') l) # cartesian ["a";"b"] ["c";"d"];; - : (string * string) list = [("a", "c"); ("a", "d");

Does OCaml have String.split function like Python?

最后都变了- 提交于 2019-12-30 08:51:49
问题 I am using this to split strings: let split = Str.split (Str.regexp_string " ") in let tokens = split instr in .... But the problem is that for example here is a sentence I want to parse: pop esi and after the split it turns to be (I use a helper function to print each item in the tokens list): item: popitem: item: item: item: esi See, there are three spaces in the token list. I am wondering if there is a string.split like in Python which can parse instr this way: item: popitem: esi Is it

Substring check in Ocaml

社会主义新天地 提交于 2019-12-30 08:03:15
问题 Can someone help me in coding an effective substring check in OCaml? Given two strings, check whether the first one contains the second one? Using the Str module, can we do this? 回答1: Something like this might work: let contains s1 s2 = let re = Str.regexp_string s2 in try ignore (Str.search_forward re s1 0); true with Not_found -> false Here are some tests of the function: # contains "abcde" "bc";; - : bool = true # contains "abcde" "bd";; - : bool = false # contains "abcde" "b.";; - : bool

Type of addition (+) in F#

房东的猫 提交于 2019-12-30 03:03:08
问题 I just learned that OCAML have to have a . postfix for doing float arithmetic. An example would be 3. +. 4. which equals 7. (float). However, F# handles float and integer arithmetic in the same way, so both 3 + 4 (int) and 3. + 4. (float) works. F# have + naturally assigned to int so let add a b = a + b is of type int -> int -> int . And indeed (+) gives me val it : (int -> int -> int) = <fun:it@6-1> . That leads to the following sequence which I think quite counter-intuitive: > 3. + 4.;; val

How to visualize/draw automata in ocaml?

走远了吗. 提交于 2019-12-29 04:17:04
问题 I am doing composition of automata. So at the end of that, I want to draw the composed automata also. So are there any libraries for that in ocaml? or are there ocaml wrappers written for any graph visualization tool? I have googled for it but didn't get much for ocaml. Any comments on ocamlgraph? I will get more than 100 states in composed automata. 回答1: Use ocamlgraph -- it is a graph library that can generate a dot/graphviz file for you but can also do a lot of other stuff that maybe

How do you compute the difference between successive elements of a list of unknown size, functionally?

你。 提交于 2019-12-28 05:49:05
问题 In a programming language that is purely functional (like Haskell) or where you are only using it in a functional way (eg clojure); suppose you have a list/seq/enumerable (of unknown size) of integers and you want to produce a new list/seq/enumerable that contains the differences between successive items, how would you do it? What I did previously in C# was to fold over the list and keep a state object as the aggregating value which recorded the 'previous' item so that you could do a diff on

Subprocess timeout does not work

梦想的初衷 提交于 2019-12-25 17:15:52
问题 I have some buggy OCaml programs in strings, and I am using Python subprocess to check the feedback from the compiler. However, because there are some programs contain infinite loops, subprocess stuck even I set timeout field. Environment: Windows 10, Python 3.5.1 This is the code I have tried: annotated_prog = "let rec ww : ('a -> 'a * bool) * 'a -> 'a = fun (f,b) -> let (b',c') = f b in if c' then ww (f, b') else b';;\n let _ = let f x = let xx = (x * x) * x in (xx, (xx < 100)) in ww (f, 1)

OCaml non-blocking client socket

一笑奈何 提交于 2019-12-25 12:35:05
问题 Is there a way to use a client socket in a non blocking way. For example, if I create a socket for a client to connect on a server and that I do recursive recv on that socket, the last call of Unix.recv will block when no data is send and if the connection is not closed by the server. In C, you can specify flags for both : socket() and use the SOCK_NONBLOCK flag ORed with the socket type or receiv() with the MSG_DONTWAIT flags. I have looked here : http://caml.inria.fr/pub/docs/manual-ocaml