ocaml

N-ary tuples vs pairs

萝らか妹 提交于 2019-12-01 16:12:21
In Ocaml, tuples with different arities have different type and value constructors: # let a = (1, 2, 3);; val a : int * int * int = (1, 2, 3) # let b = (1, (2, 3));; val b : int * (int * int) = (1, (2, 3)) Note that second example (b) is more flexible than first (a) because "tail" of b - (2, 3) - itself is valid value: # let (_, c) = b;; val c : int * int = (2, 3) # let d = snd b;; val d : int * int = (2, 3) What is the reason to not parse "(1, 2, 3)" as "(1, (2, 3))" and instead introduce infinite (or, even worse, finite) amount of new type and value constructors for different arities? What

N-ary tuples vs pairs

喜你入骨 提交于 2019-12-01 15:10:47
问题 In Ocaml, tuples with different arities have different type and value constructors: # let a = (1, 2, 3);; val a : int * int * int = (1, 2, 3) # let b = (1, (2, 3));; val b : int * (int * int) = (1, (2, 3)) Note that second example (b) is more flexible than first (a) because "tail" of b - (2, 3) - itself is valid value: # let (_, c) = b;; val c : int * int = (2, 3) # let d = snd b;; val d : int * int = (2, 3) What is the reason to not parse "(1, 2, 3)" as "(1, (2, 3))" and instead introduce

How should I organise my OCaml project?

柔情痞子 提交于 2019-12-01 14:33:16
问题 I know this question is quite general and I even don't know how to better ask. I don't have much experiences in C and I just wish I can do similar thing in OCaml as in Java. For example, in Java , I normally create a project (using Eclipse or other IDE), then I have a src folder and a bin folder. All compiled stuff go to bin . So for the starter, how could I do something as simple as above? just split the source files and compile files easily? Normally, how do you guys organise OCaml project

ocaml printf function: skip formatting entirely if some condition holds

三世轮回 提交于 2019-12-01 10:50:35
(extracted from ocaml: exposing a printf function in an object's method , so it can be answered independently) I have the following (simplified) ocaml code, for a logger: type log_level = | Error | Warn | Info let ord lvl = match lvl with | Error -> 50 | Warn -> 40 | Info -> 30 let current_level = ref (ord Warn) let logf name lvl = let do_log str = if (ord lvl) >= !current_level then print_endline str in Printf.ksprintf do_log The logf function can be used with a printf format, as in: logf "func" Warn "testing with string: %s and int: %d" "str" 42; Is there any way to achieve the typical

Break a loop in OCaml

房东的猫 提交于 2019-12-01 10:46:20
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 see somewhere in the Internet people state that throwing and catching an exception in OCaml is costly.

Is it possible to invoke OCaml from .NET?

萝らか妹 提交于 2019-12-01 09:30:00
Is it possible to use OCaml with .NET code? I would like to invoke it like C++. Or maybe there a bridge for it. Have you considered CSML ? You would need to write a bit in its interface description language, and then should be able to call from .NET to OCaml and vice versa. 来源: https://stackoverflow.com/questions/7592451/is-it-possible-to-invoke-ocaml-from-net

ocaml printf function: skip formatting entirely if some condition holds

懵懂的女人 提交于 2019-12-01 09:09:50
问题 (extracted from ocaml: exposing a printf function in an object's method, so it can be answered independently) I have the following (simplified) ocaml code, for a logger: type log_level = | Error | Warn | Info let ord lvl = match lvl with | Error -> 50 | Warn -> 40 | Info -> 30 let current_level = ref (ord Warn) let logf name lvl = let do_log str = if (ord lvl) >= !current_level then print_endline str in Printf.ksprintf do_log The logf function can be used with a printf format, as in: logf

Is it possible to invoke OCaml from .NET?

て烟熏妆下的殇ゞ 提交于 2019-12-01 09:07:06
问题 Is it possible to use OCaml with .NET code? I would like to invoke it like C++. Or maybe there a bridge for it. 回答1: Have you considered CSML? You would need to write a bit in its interface description language, and then should be able to call from .NET to OCaml and vice versa. 来源: https://stackoverflow.com/questions/7592451/is-it-possible-to-invoke-ocaml-from-net

Will OCaml convert multi-argument function to currying or the other way around?

心已入冬 提交于 2019-12-01 06:24:51
When I was learning OCaml essentials, I was told that every function in OCaml is actually a function with only one parameter. A multi-argument function is actually a function that takes one argument and returns a function that takes the next argumetn and returns .... This is currying, I got that. So my question is : case 1 if I do let plus x y = x + y Inside OCaml when it compiles, will OCaml change it to let plus = fun x -> fun y -> x + y ? or the other way around that case 2 If I do let plus = fun x -> fun y -> x + y OCaml will convert it to let plus x y = x + y ? Which case is true? What's

How to take product of two list in OCaml?

若如初见. 提交于 2019-12-01 05:49: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. 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"); ("b", "c"); ("b", "d")] If you need that strange flat structure instead, you can use an additional list