ocaml

Print a List in OCaml

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 06:15:45
I want to do something as simple as this: Print a list. let a = [1;2;3;4;5] How can I print this list to Standard Output? You can do this with a simple recursion : let rec print_list = function [] -> () | e::l -> print_int e ; print_string " " ; print_list l The head of the list is printed, then you do a recursive call on the tail of the list. You should become familiar with the List.iter and List.map functions. They are essential for programming in OCaml. If you also get comfortable with the Printf module, you can then write: open Printf let a = [1;2;3;4;5] let () = List.iter (printf "%d ") a

Separate compilation of OCaml modules

强颜欢笑 提交于 2019-11-28 04:59:54
问题 I have read this question and others, but my compile problem is unsolved. I am testing separate compilation with these files: testmoda.ml module Testmoda = struct let greeter () = print_endline "greetings from module a" end testmodb.ml module Testmodb = struct let dogreet () = print_endline "Modul B:"; Testmoda.greeter () end testmod.ml let main () = print_endline "Calling modules now..."; Testmoda.greeter (); Testmodb.dogreet (); print_endline "End." ;; let _ = main () Now I generate the

Does != have meaning in OCaml?

喜夏-厌秋 提交于 2019-11-28 04:03:52
It seems to be an equivalency comparison for some types, but not strings. # 3 != 3;; - : bool = false # 3 != 2;; - : bool = true This is as expected. # "odp" = "odp";; - : bool = true # "odp" != "odp";; - : bool = true # "odp" <> "odp";; - : bool = false Why does "odp" != "odp" evaluate to true ? What is it actually doing? Shouldn't it generate a type error? you have experienced the difference between structural and physical equality. <> is to = (structural equality) as != is to == (physical equality) "odg" = "odg" (* true *) "odg" == "odg" (* false *) is false because each is instantiated in

int * int vs (int * int) in OCaml sum type

坚强是说给别人听的谎言 提交于 2019-11-28 03:38:44
问题 type foo = A of int * int | B of (int * int) What is the difference between int * int and (int * int) there? The only difference I see is in pattern matching: let test_foo = function | A (f, s) -> (f, s) | B b -> b Is it just a syntactic sugar? How do you select which one to use? Is there any performance difference between these two forms? 回答1: Yes, there is a performance difference: In memory A (23, 42) will contain a tag identifying it as an A and the two integers 23 and 42. B (23, 42) will

When does the relaxed value restriction kick in in OCaml?

﹥>﹥吖頭↗ 提交于 2019-11-28 03:15:47
问题 Can someone give a concise description of when the relaxed value restriction kicks in? I've had trouble finding a concise and clear description of the rules. There's Garrigue's paper: http://caml.inria.fr/pub/papers/garrigue-value_restriction-fiwflp04.pdf but it's a little dense. Anyone know of a pithier source? An Addendum Some good explanations were added below, but I was unable to find an explanation there for the following behavior: # let _x = 3 in (fun () -> ref None);; - : unit -> 'a

OCaml Printf.sprintf

﹥>﹥吖頭↗ 提交于 2019-11-28 03:02:34
问题 Why does this behavior occur? # Printf.sprintf ("Foo %d %s") 2 "bar";; - : string = "Foo 2 bar" # Printf.sprintf ("Foo %d" ^ " %s") 2 "bar";; Printf.sprintf ("Foo %d" ^ " %s") 2 "bar";; Error: This expression has type string but an expression was expected of type ('a -> 'b -> 'c, unit, string) format = ('a -> 'b -> 'c, unit, string, string, string, string) format6 I would expect that the string concatenation would be evaluated first, so everything will proceed as normal. Does this have to do

Unary minus and floating point number in OCaml

爷,独闯天下 提交于 2019-11-28 02:04:48
I wanted to have a vector of complex numbers in my program, so I wrote this: [|pt 0. 0.; pt -4. 1.; pt -7. -2.; pt 4. 5.; pt 1. 1.|] Here pt is a function of type float -> float -> Complex.t . But ocaml refused to compile this saying: Characters 12-14: [|pt 0. 0.; pt -4. 1.; pt -7. -2.; pt 4. 5.; pt 1. 1.|];; ^^ Error: This expression has type float -> float -> Complex.t but an expression was expected of type int What I wanted to do here is (obviously) include the complex number whose real part is -4 and whose imaginary part is 1. But ocaml treated what I intended to be an unary minus as a

The right way to use a data structure in OCaml

限于喜欢 提交于 2019-11-28 01:57:54
问题 Ok, I have written a binary search tree in OCaml. type 'a bstree = |Node of 'a * 'a bstree * 'a bstree |Leaf let rec insert x = function |Leaf -> Node (x, Leaf, Leaf) |Node (y, left, right) as node -> if x < y then Node (y, insert x left, right) else if x > y then Node (y, left, insert x right) else node I guess the above code does not have problems. When using it, I write let root = insert 4 Leaf let root = insert 5 root ... Is this the correct way to use/insert to the tree? I mean, I guess

Why is an int in OCaml only 31 bits?

我是研究僧i 提交于 2019-11-27 19:46:39
问题 Haven't seen this "feature" anywhere else. I know that the 32nd bit is used for garbage collection. But why is it that way only for ints and not for the other basic types? 回答1: This is called a tagged pointer representation, and is a pretty common optimization trick used in many different interpreters, VMs and runtime systems for decades. Pretty much every Lisp implementation uses them, many Smalltalk VMs, many Ruby interpreters, and so on. Usually, in those languages, you always pass around

Ocaml Unbound Graphics Module

倾然丶 夕夏残阳落幕 提交于 2019-11-27 15:27:40
Running open Graphics;; in ocaml returns an error, saying it is an unbound module. Running it in terminal (ocaml) returns the same thing. Does this mean my Graphics Module was somehow not installed with the ocaml package? If so, how can I install the module? On Fedora. Graphics module is not ready by default. You need to load it manually. In toplevel: $ ocaml OCaml version blahblah # #load "graphics.cma";; # open Graphics;; or you can specify it at the command line: $ ocaml graphics.cma OCaml version blahblah # open Graphics;; I do not know about Fedora but if the above fails, graphics is not