I've successfully installed Batteries and can get it work...somewhat.
Any ideas why I'm getting the syntax error since Opam list this:
depends: "ocaml" {>= "4.00.0" & < "4.10.0"}
And, I'm at: The OCaml toplevel, version 4.07.1
This code relies on camlp4 preprocessor which is deprecated and is no longer supported. Moreover, list comprehension is no longer a part of the Batteries library and is a separate package. So you need to install it using opam install pa_comprehension
. You can still make your code work for OCaml 4.07.1, by issuing the following directives right after you start OCaml toplevel (or utop)
#use "topfind";;
#camlp4o;;
#require "pa_comprehension";;
The first directive (note you have to type #
it is a part of the directive), enables ocamlfind in the toplevel (I think it is not needed in utop, but won't hirt). The next directive enables camlp4o
syntax, so that all inputs are preprocessed. You're no longer coding in OCaml after that, but in camlp4o
dialect. Finally, the last directive loads the preprocessor that supports list comprehensions.
To compile the code that uses list comprehension you need to specify the same options to the compiler (i.e., enable syntax, load the preprocessor), e.g.,
ocamlfind ocamlopt -syntax camlp4o -package pa_comprehension -linkpkg example.ml -o example
The camlp4 package also provides another list comprehension syntax, which is a little bit different from the one that is provided by Batteries. It is called camlp4.listcomprehension
, and you can use it with roughly the same invocations modulo the package name, e.g., in toplevel,
#use "topfind";;
#camlp4o;;
#require "camlp4.listcomprehension";;
and to compile
ocamlfind ocamlopt -syntax camlp4o -package camlp4.listcomprehension -linkpkg example.ml -o example
With all that said, I highly discourage you from using camlp4 list comprehension in modern days. This is an obsolete technology.
Besides, your example is ill-formed, you're missing the ?
character in the closing bracket, e.g., this is an example interaction with the toplevel,
# #use "topfind";;
# #camlp4o;;
# #require "pa_comprehension";;
# open Batteries;;
# [? x | x <- 1--10; x mod 2 = 0 ?];;
- : int Batteries.Enum.t = <abstr>