0

I have source code below. I knew it doesn't work becaus missing camlp4. Now my OS is Ubuntu, I am using caml mode for emacs editor. Can you please help me to config camlp4 for my emacs, so I can run this code ? Thank you so much

type term = V of string | F of string * term list

let rec symbols = function
  | V x -> [x]
  | F (f, ts) -> f :: [ g | t <- ts; g <- symbols t ]

let rec functions = function
  | V _ -> []
  | F (f, ts) -> f :: [ g | t <- ts; g <- functions t ]
4

2 回答 2

3

您正在使用作为 Camlp4 一部分的列表理解。要在终端模拟器中编译此代码,您应该键入

ocamlfind c -package camlp4.listcomprehension -syntax camlp4o -c a.ml

编译行与 emacs 或 camlmode 无关。但是,如果您想在顶层尝试此代码,则需要输入:

$ ocaml
    OCaml version 4.02.1

Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

# #camlp4o;;
/home/kakadu/.opam/4.02.1/lib/ocaml/dynlink.cma: loaded
/home/kakadu/.opam/4.02.1/lib/ocaml/camlp4: added to search path
/home/kakadu/.opam/4.02.1/lib/ocaml/camlp4/camlp4o.cma: loaded
        Camlp4 Parsing version 4.02.1

# #require "camlp4.listcomprehension";;
/home/kakadu/.opam/4.02.1/lib/ocaml/camlp4/Camlp4Parsers/Camlp4ListComprehension.cmo: loaded
# type term = V of string | F of string * term list;;
type term = V of string | F of string * term list
# let rec symbols = function
    | V x -> [x]
    | F (f, ts) -> f :: [ g | t <- ts; g <- symbols t ];;
val symbols : term -> string list = <fun>

$ cat ~/.ocamlinit
#use "topfind";;

要将 camlp4 安装到您的机器上,您需要为 Ubuntu 或opam预编译的软件包。

于 2015-02-28T15:35:35.320 回答
0

非常感谢你,卡卡杜。我想我知道我的答案了。首先,我在我的 ubuntu 上安装了 camlp4。

apt-get install camlp4
apt-get install camlp4-extra

然后,我在源代码中创建一个文件 .ocamlinit 并添加:

#load "dynlink.cma";;
#load "camlp4of.cma";;

我认为这些步骤解决了我上面的问题,

于 2015-03-02T02:14:55.107 回答