ocaml

Is it posible to pass a C function as callback to OCaml?

一世执手 提交于 2020-08-09 09:11:19
问题 I'm studying on how to integrate an OCaml TCP/IP stack into my C++ project. I already know how to call C from OCaml and call OCaml from C thanks to this answer: OCaml as C library, hello world example The OCaml will be controlled by C++, not the other way around. So, for a TCP/IP stack, I must be able to send and receive packets. I can easily send data to the TCP/IP stack through C++, but how to receive it? I need to pass a C function (a callback) as a parameter to OCaml so it delivers the

What is the syntax for OCaml style generic type parameters in F#?

ぐ巨炮叔叔 提交于 2020-08-07 05:30:10
问题 According to this answer, F# supports OCaml style type parameters. The example in the question is: type 'a NestedList = List of 'a NestedList list | Elem of 'a However, I could not find this syntax documented anywhere in the F# documentation and moreover, I cannot get the F# compiler accept the syntax in the answer I gave the link to. This attempt to use multiple parameters are not accepted by the compiler: type ('a * 'b) SomeType = ('a * 'b) This however, works: type ('a , 'b) SomeType = ('a

How to print into file in OCaml in a appending way?

断了今生、忘了曾经 提交于 2020-07-23 10:24:26
问题 Currently I have some code in this way: method printFuncIntoFile X = let funHead = "head" and funbody = "body" and oc = open_out "test.txt" in Printf.fprintf oc "%s%s" funHead funBody; close_out oc; foo X.child And this code can only leave the last function content in the text.txt. I search the Printf document but can only find a val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a which requires a Buffer data structure, I think there should be some strategies more easy, even though I

How to print into file in OCaml in a appending way?

邮差的信 提交于 2020-07-23 10:22:21
问题 Currently I have some code in this way: method printFuncIntoFile X = let funHead = "head" and funbody = "body" and oc = open_out "test.txt" in Printf.fprintf oc "%s%s" funHead funBody; close_out oc; foo X.child And this code can only leave the last function content in the text.txt. I search the Printf document but can only find a val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a which requires a Buffer data structure, I think there should be some strategies more easy, even though I

How to print into file in OCaml in a appending way?

一笑奈何 提交于 2020-07-23 10:22:11
问题 Currently I have some code in this way: method printFuncIntoFile X = let funHead = "head" and funbody = "body" and oc = open_out "test.txt" in Printf.fprintf oc "%s%s" funHead funBody; close_out oc; foo X.child And this code can only leave the last function content in the text.txt. I search the Printf document but can only find a val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a which requires a Buffer data structure, I think there should be some strategies more easy, even though I

How to print into file in OCaml in a appending way?

江枫思渺然 提交于 2020-07-23 10:21:26
问题 Currently I have some code in this way: method printFuncIntoFile X = let funHead = "head" and funbody = "body" and oc = open_out "test.txt" in Printf.fprintf oc "%s%s" funHead funBody; close_out oc; foo X.child And this code can only leave the last function content in the text.txt. I search the Printf document but can only find a val bprintf : Buffer.t -> ('a, Buffer.t, unit) format -> 'a which requires a Buffer data structure, I think there should be some strategies more easy, even though I

Convert record to list

跟風遠走 提交于 2020-07-10 10:25:30
问题 Suppose type pair_int = {l1:int; l2:int, ..., ln:int} let test = {l1=2; l2=4, ..., ln=71} I thought I could do something like map (fun (x,y) -> y) test , but it doesn't work How can I get the list [2,4, ..., 71] from test ? 回答1: There's no nice way to do this inside the OCaml type system. You can't map over the fields of a record because they can be of all different types. Your type pair_int looks suspiciously like a list or an array already. The field names don't add any semantic content,

How to implement “appendFile” function?

。_饼干妹妹 提交于 2020-07-03 04:22:13
问题 I can use the following function to overwrite a text file: let writeFile ~filename:fn s = let oc = open_out fn in output_string oc s; close_out oc ;; Howeve, i donot know how to append a line to a text file ? 回答1: You could pass additional mode flag Open_append to open_out_gen function: let _ = let oc = open_out_gen [Open_creat; Open_text; Open_append] 0o640 "a.txt" in output_string oc "append\n"; close_out oc 回答2: This is what I do: let append_string path s = let chan = open_out_gen [Open

Polymorphic variants and let%bind type error

早过忘川 提交于 2020-06-29 06:39:28
问题 I'm trying to use the technique in Composable Error Handling in OCaml (Result type with polymorphic variants for errors) for some code I've written. The types of the functions I'm trying to use look like this: val parse : parser -> token list -> (Nominal.term, [> `ParseError of string ]) Result.t val lex : lexer -> string -> (token list, [> `LexError of string ]) Result.t My attempt at composing them is this: let lex_and_parse : parser -> lexer -> string -> (Nominal.term, [> `ParseError of

Polymorphic variants and type signatures

前提是你 提交于 2020-06-16 19:15:30
问题 (This is an extension / distillation of Polymorphic variants and let%bind type error) Consider the following code: Version 1: let x : [> `Error1 ] = (`Error1 : [> `Error1 ]) let y : [> `Error1 | `Error2 ] = x Version 2: let x : [> `Error1 ] = (`Error1 : [ `Error1 ]) let y : [> `Error1 | `Error2 ] = x Version 1 typechecks, but version 2 fails (I'm compiling with 4.09.0): File "test.ml", line 2, characters 33-34: 2 | let y : [> `Error1 | `Error2 ] = x ^ Error: This expression has type [ `Error1