ocamllist-comprehensionocaml-batteries

Building OCaml code that uses list comprehension


From this SO question: List Comprehension in Ocaml?, I could install the comprehension package with opam install pa_comprehension, and use the package in toplevel REPL.

# #require "pa_comprehension";;
# open Batteries;;
# [? 2 * x | x <- 0 -- max_int ; x * x > 3 ?];;
- : int Batteries.Enum.t = <abstr>

Then, how can I compile the code?


Solution

  • Unfortunately, since pa_comprehension package name is not ended with .syntax extension, this is a little bit harder than it should be. (The fact that this issue is still not fixed, shows that using pa_comprehension is not very popular in modern OCaml). So, without the proper extension, we need to do everything manually. If your file is named pr.ml, then the correct invocation is:

    $ ocamlbuild -use-ocamlfind -syntax camlp4o -pkg camlp4,pa_comprehension pr.native
    

    If you don't wan't to type it every time, then you can create _tags file, with the following contents:

    $ cat _tags
    <**/*> : package(camlp4),syntax(camlp4o),package(pa_comprehension)
    

    In that case, the command line spell is a little bit easier:

    $ ocamlbuild -use-ocamlfind pr.native