Static linking is preferred for some cases although it has its own various problems. Static building/linking is not always possible for some languages on some platform. For OCaml, the answer to this question is yes. In this post, we will introduce 2 methods to statically linking OCaml: static linking with runtime glibc required and static
Read more
Tag: OCaml
How to print a line to STDERR and STDOUT in OCaml?
Posted onIn OCaml, how to print a string as a line to STDOUT? That is, the string and the newline character, nicely? And similarly, how to print the line to STDERR? In OCaml, you may print a string with the end line character to STDOUT using the function in the Pervasives module: val print_endline : string
Read more
How to make dd faster on Linux?
Posted ondd seems slow when I use command like # dd if=/dev/sda2 of=./sda2.bak How to make it faster? You can make dd faster by specifying a good bs like # dd if=/dev/sda2 of=./sda2.bak bs=8192 8192 is a magic number. There are may be other good sizes for bs for different systems. But 8192 works pretty well
Read more
How to measure a function’s execution time in OCaml?
Posted onHow to measure the execution time of a function, say f: ‘a -> ‘b in OCaml? This small OCaml function is handy: let time f = let t = Unix.gettimeofday () in let res = f () in Printf.printf “Execution time: %f secondsn” (Unix.gettimeofday () -. t); res ;; The gettimeofday returns a float representing
Read more
if (p:string list) = c (is the only element)
Posted onif (p:string list) = [c] then (divide p1 c) showing unbound value c. i want to equalise (if one element in p which is (c:anything)) and use that variable let p = [“ocaml”] let f s = match s with | [c] -> print_endline c | _ -> print_endline “ops” f p It will print:
Read more
Syntactical difference among OCaml, Scala, F# and Haskell
Posted onWhat’s the syntactical difference among OCaml, Scala, F# and Haskell. This page gives a side-by-side reference among OCaml, F#, Scala and Haskell: ML Dialects and Friends: OCaml, F#, Scala, Haskell
How to find out which function causes Exception “Stack_overflow” in OCaml
Posted onMy OCaml program prints: Fetal error: Exception “Stack_overflow” without any further information. How to find out which function causes this “Stack_overflow” exception? First, recompile your OCaml program with -g. Second, rerun your OCaml program with OCAMLRUNPARAM=b and the backtrace will be printed out after the “Stack_overflow” exception.
utop key bindings / key shortcuts
Posted onutop is an improved toplevel for OCaml supporting line edition, history, real-time and context sensitive completion, colors and etc. utop is convenient to use. However, the key bindings are a little bit different from the ones with GNU readline. What are all the key bindings? The #utop_bindings command will print all the key bindings. Here
Read more
Auto indenting for OCaml code in Vim
Posted onHow to set up auto indenting for OCaml code in Vim? The built-in indenting in Vim for OCaml seems not very good. Please check the post at https://www.systutorials.com/5212/auto-indenting-for-ocaml-code-in-vim-with-ocp-indent/
Tutorial: How to trace a function in OCaml
Posted onTutorial: How to trace a function in OCaml. Use #trace. For example, to trace function f: # let rec f n = if n = 0 then 1 else f (n – 1) * n;; val f : int -> int = <fun> # #trace f;; f is now traced. # f 4;; f <–
Read more
Where is the .mli files for Core
Posted onWhere is the .mli files for Core installed by opam. It is under ~/.opam/4.00.1/lib/core where 4.00.1 is the version of OCaml used. For example, date.mli is under ~/.opam/4.00.1/lib/core/date.mli.
Add the jane street opam repository
Posted onHow to add the jane street opam repository is introduced here. Here, we assume the branch to add is core-branch. You may need to replace the core-branch with the branch you need. $ git clone -b core-branch https://github.com/janestreet/opam-repository.git $ opam repo add js opam-repository Or, directly add the remote branch as an opam repository. $
Read more
How to import OCaml libraries
Posted onHow to import 3rd party libraries (e.g. not standard libraries) in OCaml? An answer by Gabriel Scherer on how to import Batteries is just great and answers this question with much information. Although it is for Batteries, the method is general. The OCaml compiler (or toplevel, etc.) will find with no additional information only the
Read more
Max array length in OCaml
Posted onWhat’s the max array length in OCaml. You can check it by: # Sys.max_array_length;; On 64-bit machine: # Sys.max_array_length;; – : int = 18014398509481983 On a 32-bit machne: # Sys.max_array_length;; – : int = 4194303 This is due to the memory representation of arrays. 64 Bit machines are better here. And AFAIK, OCaml was developed
Read more
How to get the assembly code for OCaml code generated by ocamlopt?
Posted onHow to get the native assembly code (e.g. x86-64 asm) for OCaml code generated by the native ocamlopt compiler? To get the assembly code for an OCaml program, add these parameters to ocamlopt: -S -inline 20 -nodynlink An example is as follows. The OCaml program: $ cat not.ml let not x = ((x – 1)
Read more
How to use OCaml as a script language?
Posted onHow to use OCaml as a script language like bash and python? It will be good that I can write a script in OCaml and directly invoke it. For example, I write a script named “script.ml” and then I can invoke it directly by $ ./script.ml Thanks! Three helloworld scripts: script3.ml #! /usr/bin/env ocaml print_string
Read more
How to import a source file to OCaml’s toplevel?
Posted onHow to import a source file to OCaml’s toplevel? Say, I want to use a function implemented in a source file in the toplevel. #use “file-name”;; Read, compile and execute source phrases from the given file. This is textual inclusion: phrases are processed just as if they were typed on standard input. The reading of
Read more
Good tools to manage OCaml packages
Posted onWhich tools to mange OCaml packages in my system (Linux)? I use OPAM to manage OCaml packages: http://opam.ocamlpro.com/index.html To install it: $ wget https://raw.githubusercontent.com/ocaml/opam/master/shell/opam_installer.sh $ sh ./opam_installer.sh /usr/local/bin More options are available here. To make opam settings take effect, append this to ~/.bashrc: eval `opam config env` Some frequent usages: opam list # List all
Read more
Length of int in OCaml?
Posted onWhat is the length of int in OCaml? It is said that int in OCaml is 31. But I get this: # (1 lsl 33);; – : int = 8589934592 8589934592 is larger than 2^31. On my laptop (64-bit Fedora Linux), the int in OCaml is 63 bits. The code (count1s counts the number of
Read more
How to call C functions from OCaml code
Posted onI am writing a OCaml program but want to call some C functions provided in some source code or library. How to achieve this? Is there any good tutorials on this? A very good tutorial that is easy to follow and understand: How to wrap C functions to OCaml. A comprehensive reference manual: Chapter 19
Read more