Ocaml编译列表理解

我想使用ocamlopt来编译列表理解。我不想使用任何特殊的库,因为ocaml至少在顶层(交互式)支持列表理解。

Marko Tunjic发布了:

列表理解已经包含在标准ocaml中

#require "camlp4.listcomprehension";;

[ x * x | x <- [ 1;2;3;4;5] ];;

- : int list = [1; 4; 9; 16; 25]

在ocaml 4.07.1上,我发现interactive需要满足以下条件:

#use "topfind";;
#camlp4o;;
#require "camlp4.listcomprehension";;

文件lc1.ml包含

open Camlp4.ListComprehensions

let _ = [ x * y | x <- [ 1;2;3;4;5]; y <- [10;11;12;13;14 ] ];;

编译

ocamlfind ocamlopt -package camlp4 -linkpkg lc1.ml -o lc1

File "lc1.ml", line 3, characters 16-17:
Error: Syntax error

预期结果

[10; 11; 12; 13; 14; 20; 22; 24; 26; 28; 30; 33; 36; 39; 42; 40; 44;      48; 52;
 56; 50; 55; 60; 65; 70]

转载请注明出处:http://www.cospi.net/article/20230401/1693859.html