ocaml

Reading HTML contents of a URL in OCaml

谁都会走 提交于 2019-12-04 05:29:21
I would like to write an OCaml function which takes a URL and returns a string made up of the contents of the HTML file at that location. Any ideas? Thanks a lot! Best, Surikator. I've done both of those things using ocurl and nethtml ocurl to read the contents of the URL (tons of properties here; this is the minimum), let string_of_uri uri = try let connection = Curl.init () and write_buff = Buffer.create 1763 in Curl.set_writefunction connection (fun x -> Buffer.add_string write_buff x; String.length x); Curl.set_url connection uri; Curl.perform connection; Curl.global_cleanup (); Buffer

Error: Unbound module … in Ocaml

[亡魂溺海] 提交于 2019-12-04 05:09:18
问题 I have just installed a library called Apron , the installation seems to be done: @ubuntu$ find -name "*apron*" ./lib/libapron_debug.so ./lib/libapron.a ./lib/libapron.so ./lib/libapron_debug.a ./local/lib/ocaml/3.11.2/stublibs/dllapron_caml.so.owner ./local/lib/ocaml/3.11.2/stublibs/dllapron_caml.so ./local/lib/ocaml/3.11.2/apron ./local/lib/ocaml/3.11.2/apron/libapron_caml_debug.a ./local/lib/ocaml/3.11.2/apron/apron.cmxa ./local/lib/ocaml/3.11.2/apron/libapron_caml.a ./local/lib/ocaml/3.11

How to make an interactive program?

自闭症网瘾萝莉.ら 提交于 2019-12-04 04:44:56
问题 I'm learning Ocaml and I need to create a program that can interact with the user in the following way: Program: "Welcome!" User: command1 arg1 arg2 program: "The answer is..." User: command2 arg program: "The answer is..." User: exit I need a scheme of the loop that make something like that 回答1: Here's a loop that will read lines of input until it reaches end of file, or sees a line that says "exit". let rec loop () = match read_line () with | "exit" -> () | s -> Printf.printf "I saw %s\n%!"

OCaml: get value's type name

让人想犯罪 __ 提交于 2019-12-04 04:37:07
问题 Is is possible to print value's name in OCaml, for example if I have type my_type = | MyType_First of int | MyType_Second of string and then do something like: let my_value = MyType_First 0 in print_string ("my_value is of type " ^ String.from_type my_value ^ ".\n"; can I get "my_value is of type MyType_First." ? Thank you. 回答1: Monomorphic solution: let from_type = function | MyType_First _ -> "MyType_First" | MyType_Second _ -> "MyType_Second" Polymorphic solution: none. (AFAIK, lexical

CLI shell script code generation from compiled executable? [closed]

家住魔仙堡 提交于 2019-12-04 03:41:41
Question, topic of discussion I am very interested in generation of command line shell scripting source code from code written in a more robustness-promoting, well-performant and platform-independent compiled language (OCaml, for instance). Basically, you would program in a compiled language to perform any interactions with the OS that you want (I would propose: the more complex interactions or ones that are not easy to do in a platform-independent way), and finally you would compile it to a native binary executable (preferably), which would generate a shell script that effects in the shell

function returns list in reverse order in OCaml

拥有回忆 提交于 2019-12-04 03:38:58
问题 I want to read some numbers from a file, take them to a list and finally display them on the screen. numbers.txt currently has 2 3 5 7 11 however as output i'am getting 11 7 5 3 2 - : unit = () Why is this happening? let rec int_list_from_sb sb n = match n with | 0 -> []; | _ -> (bscanf sb " %d" (fun a -> a))::(int_list_from_sb sb (n - 1));; let file_name = open_in "numbers.txt" in let sb = Scanning.from_channel file_name in let int_list = int_list_from_sb sb 5 in List.iter (fun a -> print

OCaml: Can't run utop after installing it

試著忘記壹切 提交于 2019-12-04 03:01:40
I'm trying to learn OCaml through the Real World OCaml book. They have a guide by which I am supposed to install the Core package and utop. However, while I seem to be successfully installing both of these using Opam, neither of them works when I try to use them. I know that they're installed, because when I try to install them again, I get this message: $ opam install utop core [NOTE] Package utop is already installed (current version is 1.10). [NOTE] Package core is already installed (current version is 109.55.02). However, when I try to enter "utop" to start utop, it doesn't work. $ utop

Ocaml modules implementation

匆匆过客 提交于 2019-12-04 02:58:40
Ocaml's standard library contains various modules: List , Map , Nativeint , etc. I know that interfaces for these modules are provided (e.g. for the List module ), but I am interested in the algorithms and their implementations used in modules' functions. Where can I find that? On your system: /usr/lib/ocaml/list.ml and other .ml files On the web: https://github.com/ocaml/ocaml/blob/trunk/stdlib/list.ml and other .ml files in https://github.com/ocaml/ocaml/tree/trunk/stdlib The List implementation is interesting to study. For example, the map function could be implemented like this: let rec

How do you append a char to a string in OCaml?

一笑奈何 提交于 2019-12-04 02:55:57
It seems as if there is no function in the standard library of type char -> string -> string , which insert a char in front of (or at the end of) a string . There are workarounds, e.g. by using String.make or String.blit . Is there an elegant way to do this? String.make and String.blit is a good way to do so, but they seem to be imperative. Personally I prefer to make infix functions using Char.escaped and string concatenation: let (^$) c s = s ^ Char.escaped c (* append *) let ($^) c s = Char.escaped c ^ s (* prepend *) The code from @pad is what I would use, because I like to treat strings

What's the difference between -> and |> in reasonml?

坚强是说给别人听的谎言 提交于 2019-12-04 02:55:50
A period of intense googling provided me with some examples where people use both types of operators in one code, but generally they look just like two ways of doing one thing, they even have the same name tl;dr: The defining difference is that -> pipes to the first argument while |> pipes to the last. That is: x -> f(y, z) <=> f(x, y, z) x |> f(y, z) <=> f(y, z, x) Unfortunately there are some subtleties and implications that makes this a bit more complicated and confusing in practice. Please bear with me as I try to explain the history behind it. Before the age of pipe Before there were any