我目前正在使用"Real Word OCaml",其中一个带有命名/标记参数的基本示例似乎不起作用(使用utop 4.01.0):
let languages = ["OCaml"; "Perl"; "C"];;
List.map ~f:String.length languages;;
Run Code Online (Sandbox Code Playgroud)
生产:
Error: The function applied to this argument has type 'a list -> 'b list
This argument cannot be applied with label ~f
Run Code Online (Sandbox Code Playgroud)
鉴于:
List.map String.length languages;;
Run Code Online (Sandbox Code Playgroud)
产生预期的输出[5; 4; 1]
.
在核心语言中,与大多数语言一样,参数是匿名的.
这是否意味着我必须包含某种外部库才能使这段代码有效?
编辑
这是我的~/.ocamlinit
文件(根据本书的安装说明):
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind"
#camlp4o
#thread
#require "core.top" …
Run Code Online (Sandbox Code Playgroud) 我正试图通过Real World OCaml书来学习OCaml.他们有一个指南,我应该安装Core包和utop.然而,虽然我似乎使用Opam成功安装了这两个,但当我尝试使用它们时它们都不起作用.
我知道它们已经安装,因为当我再次尝试安装它们时,我收到了以下消息:
$ opam install utop core
[NOTE] Package utop is already installed (current version is 1.10).
[NOTE] Package core is already installed (current version is 109.55.02).
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试输入"utop"来启动utop时,它不起作用.
$ utop
bash: utop: command not found
Run Code Online (Sandbox Code Playgroud)
当我尝试打开核心时:
$ open Core.Std
Couldn't get a file descriptor referring to the console
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?我是编程的新手,所以可能有一些关于安装的东西,我只是不理解.
我是Ocaml的新手,只是用emacs,merlin和flycheck设置我的开发环境.除了一件事之外,一切都或多或少地起作用:merlin似乎无法识别同一项目中模块之间的依赖关系.
例如,我有一个包含两个模块的测试项目:main.ml和awesome.ml.
这是我的main.ml引用第二个模块awesome.ml
(* main.ml *)
open Core
module A = Awesome
let _ =
Printf.printf "hello \n Converted to string we get: %s\n"
(A.str_of_t (A.succ A.one_t));
Run Code Online (Sandbox Code Playgroud)
这是awesome.ml:
(* awesome.ml *)
type t = int
let one_t = 1
let succ i = i + 1
let str_of_t = string_of_int
Run Code Online (Sandbox Code Playgroud)
当我发送main.ml缓冲区以使用utop-eval-buffer函数评估到utop时,我收到一个错误:"错误:未绑定的模块很棒"
我在项目的根目录中有.merlin,它有S指令.我知道它是由梅林发现的,因为它没有抱怨"开放核心"
S src
PKG core lwt ounit
B _build/src
B +threads
Run Code Online (Sandbox Code Playgroud)
这是我的_tags:
<src/**>: include
<src/**>: package(oUnit), package(core)
true:thread
Run Code Online (Sandbox Code Playgroud)
ocamlbuild的常规项目编译工作正常,没有错误.这是Makefile
## Makefile
default: main
main: main.native
test: test.native
%.native:
ocamlbuild …
Run Code Online (Sandbox Code Playgroud) 在utop中,当打开库(通过~requires ......)或打开模块时(通过打开Module_name),有没有办法获取库或模块的内容?utop通过完成选项卡提供此功能,但我希望立即看到所有功能.
我正在使用 OCaml utop 和 Core.Std 模块。
要查看模块的帮助,我必须点击别名链接,如下所示:
utop # #show_module Array;;
module Array = Core_kernel.Std_kernel.Array
utop # #show_module Core_kernel.Std_kernel.Array;;
module Array = Core_kernel.Std_internal.Array
utop # #show_module Core_kernel.Std_internal.Array;;
module Array = Core_kernel.Core_array
Run Code Online (Sandbox Code Playgroud)
最后 show_module 将显示信息。有没有更快的方法?
我已经添加了几行代码来.ocamlinit
使utop提示减少一个眼睛:
#require "react";;
#require "lambda-term";;
UTop.prompt := fst (React.S.create LTerm_text.(eval [
S "\n";
B_bold true;
B_fg (LTerm_style.cyan);
S "# ";
]));;
Run Code Online (Sandbox Code Playgroud)
(我不明白那里发生的一切......我刚发现它在网上发布并进行了调整.)
但是现在如果我使用常规的ocaml
顶级我得到:
Error: Unbound module UTop
Run Code Online (Sandbox Code Playgroud)
.ocamlinit
除非我正在运行utop,否则我可以添加测试以跳过这些行吗?我可以将它们放在仅由utop加载的附加文件中吗?
当我尝试打电话#require "core"
时utop
,我收到一个错误:
utop # #require "core";;
Exception: Unix.Unix_error (Unix.ENOENT, "sysconf", "").
Raised by primitive operation at unknown location
Called from file "toplevel/topdirs.ml", line 144, characters 10-51
Run Code Online (Sandbox Code Playgroud)
我有一个.ocamlinit
在我当前目录中看起来像这样的文件:
(* Added by OPAM. *)
let () =
try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
with Not_found -> ()
;;
#use "topfind";;
#thread;;
#camlp4o;;
Run Code Online (Sandbox Code Playgroud)
可能导致此错误发生的原因是什么?它似乎是Findlib的一个错误?
我试图抹着我的~/.opam
目录,重新运行opam init
,opam switch set 4.05.0
以及重新安装core
和utop
,但这个错误仍然存在.是否有可能我还需要重新安装opam
?我目前正在使用opam-1.2.2的二进制版本.
我正在运行Arch Linux,我相信在更新系统上的所有软件包之后会出现这个错误,但我不确定.
我仍然可以用来jbuilder
构建可执行文件,但是在运行可执行文件时,它们会因类似的错误而失败:
$ _build/install/default/bin/my_executable
Uncaught …
Run Code Online (Sandbox Code Playgroud) 有没有办法直接从 utop 检索此处以 html 形式显示的文档。
\n\n我可以通过使用获取类型
\n\nutop # open Core;;\n\xe2\x94\x80( 10:41:50 )\xe2\x94\x80< command 4 >\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80{ counter: 0 }\xe2\x94\x80\nutop # #typeof "In_channel.input_char";;\nval Core.In_channel.input_char : Core.In_channel.t -> Base.char Base.option\n\xe2\x94\x80( 10:41:54 )\xe2\x94\x80< command 5 >\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80{ counter: 0 }\xe2\x94\x80\n
Run Code Online (Sandbox Code Playgroud)\n\n或者
\n\nutop # #show In_channel.input_char;;\nval input_char : in_channel -> char option\n\xe2\x94\x80( 10:43:00 )\xe2\x94\x80< command 7 >\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80{ counter: 0 }\xe2\x94\x80\nutop #\n
Run Code Online (Sandbox Code Playgroud)\n\n但医生更丰富
\n\n val input_char : t \xe2\x80\x91> Stdio__.Import.char Stdio__.Import.option\n\nRead one character from the given input channel. \nReturn None …
Run Code Online (Sandbox Code Playgroud) ocaml ×10
utop ×10
opam ×2
emacs ×1
installation ×1
merlin ×1
ocaml-core ×1
ocaml-dune ×1
prompt ×1