ocaml

What is the easiest way to add an element to the end of the list?

守給你的承諾、 提交于 2019-12-04 23:35:53
As :: : 'a -> 'a list -> 'a list is used to add an element to the begin of a list, Could anyone tell me if there is a function to add an element to the end of a list? If not, I guess List.rev (element::(List.rev list)) is the most straightforward way to do it? Thank you! Adi list@[element] should work. @ joins lists. The reason there's not a standard function to do this is that appending at the end of a list is an anti-pattern (aka a "snoc list" or a Schlemiel the Painter algorithm ). Adding an element at the end of a list requires a full copy of the list. Adding an element at the front of the

Does `string` in OCaml support UTF-8?

女生的网名这么多〃 提交于 2019-12-04 23:34:48
Does the type string in OCaml support utf8 ? Or what library I should use for utf8 string? The string type of OCaml consists of a series of 8-bit bytes in essence. You can store a UTF-8 value in a string, and I have often done this. However, there's no built-in support for handling them. A good library for handling Unicode in OCaml (so I've heard) is Camomile . There is also Uutf if you're looking for just unicode conversion. 来源: https://stackoverflow.com/questions/16152005/does-string-in-ocaml-support-utf-8

Cast float to int in OCaml

本小妞迷上赌 提交于 2019-12-04 22:59:58
How am I supposed to cast a float to an integer in OCaml? I know how to get a float from an int, but there doesn't seem to be an easy way to get an int from a float. # int_of_float ;; - : float -> int = <fun> I assume you want a nearby int (this rounds towards zero, same as C does). If you want the IEEE representation, see Int64.bits_of_float . you can simply truncate it, if the integer part of the float is what you want: printf "number\tint\tfloor\tceil\n"; List.iter (fun x -> printf "%.1f\t%d\t%.1f\t%.1f\n" x (truncate x) (floor x) (ceil x)) fs;; (* * number int floor ceil * 3.3 3 3.0 4.0 *

Why do some OCaml functions take () as a parameter?

◇◆丶佛笑我妖孽 提交于 2019-12-04 22:56:15
Example in Unix module: val environment : unit -> string array Why not just: val environment : string array ? Because it denotes a function that takes a value of type unit as its parameter. The unit type is only inhabited by the value "()". This is usually used to mean that the function is going to perform some kind of IO or induce a side-effect, and needs no input. The second type signature you provided is the signature for a value, not a function that can be applied. If some expression were bound to this name, that expression would be evaluated at the time the value binding takes place, not

OCaml: Is there a function with type 'a -> 'a other than the identity function?

亡梦爱人 提交于 2019-12-04 22:54:10
This isn't a homework question, by the way. It got brought up in class but my teacher couldn't think of any. Thanks. How do you define the identity functions ? If you're only considering the syntax, there are different identity functions, which all have the correct type: let f x = x let f2 x = (fun y -> y) x let f3 x = (fun y -> y) (fun y -> y) x let f4 x = (fun y -> (fun y -> y) y) x let f5 x = (fun y z -> z) x x let f6 x = if false then x else x There are even weirder functions: let f7 x = if Random.bool() then x else x let f8 x = if Sys.argv < 5 then x else x If you restrict yourself to a

Performance difference between pattern matching and if-else

耗尽温柔 提交于 2019-12-04 21:23:26
问题 Why can OCaml generate efficient machine code for pattern matching and not for if-else tests? I was reading Real World OCaml and I came across this section where they compared the performance of pattern matching to the performance of if-else tests. It turned out that pattern matching in their example was significantly faster than if-else tests. Even though the code doesn't utilize any special pattern match cases that would not be possible with if-else tests, it just compares integers. They

Working with ocaml Lwt sockets

微笑、不失礼 提交于 2019-12-04 21:12:22
I have been on learning Ocaml for a week, some things got clear, the others rather not. I'm trying to compose a simple Tic-Tac-Toe server accepting connections via telnet. Just for a word. I have to use Lwt and rigth now it seems for me a darken place because I had faced too much of language pecularities. The begining of the code: let sock = Lwt_unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 let setting_up_server_socket = let sockaddr = (Unix.ADDR_INET(Unix.inet_addr_of_string "127.0.0.1", 23233)) in Lwt_unix.set_close_on_exec sock; Lwt_unix.setsockopt sock Unix.SO_REUSEADDR true; Lwt_unix.bind

How to schedule a task in OCaml?

家住魔仙堡 提交于 2019-12-04 21:03:13
I have a task need to be done every 4 hours or once a day. In Java, it has quartz or spring or timer . But in OCaml, how do I do that? Any good lib for that? I don't know any library to do that, but I think you can easily implement that kind of behavior using the Lwt library . Little example, to print Hello world every 4 hours : let rec hello () = Lwt.bind (Lwt_unix.sleep 14400.) (fun () -> print_endline "Hello, world !"; hello ()) Lwt.async (hello) The Lwt.async function call the function given (here, hello) in an asynchronous light weight thread, so you're free to do other stuff in your

Need and impossibility of having a type of a signature

泄露秘密 提交于 2019-12-04 20:33:29
I have defined 2 signature and 4 modules as follows, and it works fine: module type TRIANGLE = sig type 'a t val get ... val set ... ... end module type MATRIX = sig type 'a t val unionArrayArray: 'a TriangleArray.t -> 'a TriangleArray.t -> 'a t val unionListList: 'a TriangleList.t -> 'a TriangleList.t -> 'a t val unionArrayList: 'a TriangleArray.t -> 'a TriangleList.t -> 'a t val unionListArray: 'a TriangleList.t -> 'a TriangleArray.t -> 'a t ... end module rec MatrixArray: MATRIX = struct type 'a t = 'a array array ... end and MatrixList: MATRIX = struct type 'a t = 'a list list ... end and

Tail-recursive merge sort in OCaml

女生的网名这么多〃 提交于 2019-12-04 19:58:31
问题 I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up with the following code: let tailrec_merge_sort l = let split l = let rec _split source left right = match source with | [] -> (left, right) | head :: tail -> _split tail right (head :: left) in _split l [] [] in let merge l1 l2 = let rec _merge l1 l2 result = match l1, l2 with | [], [] -> result | [], h :: t | h :: t, [] -> _merge [] t (h :: result) | h1 :: t1, h2 :: t2 -> if h1 < h2 then _merge t1 l2