Base 不会在 OCaml 中加载,为什么?

问题描述 投票:0回答:2

所以基本上我正在尝试遵循这些说明https://dev.realworldocaml.org/install.html

我从字面上跟着他们。我有一个工作的 utop。我有一个工作的 VS 代码环境。我可以运行代码和一切。

我们知道 Base 包由于不同的原因比 stdlib 包更有效。例如:

在 stdlib 中,为了检查列表中的成员是否存在,我们这样做

List.mem "a" ["a"];;

在 Base(对于文档,看这里https://ocaml.janestreet.com/ocaml-core/v0.12/doc/base/Base/List/index.html),我们做这样的事情:

open Base
List.mem ["a"] "a" ~equal:String.equal;;

当我在 utop 中执行最后一个操作时,出现以下错误:`Error: Unbound module Base'

现在,Base 肯定存在,当我尝试通过

opam install Base
安装它时,它给出了
Package base is already installed (current version is v0.15.1).
但它不会加载,问题是什么?

当我在 utop 中执行

#require "Base";;
时,它会显示
No such package: Base
当我执行
#require "base";;
时它会加载,现在我不知道到底发生了什么。

ocaml
2个回答
1
投票

模块以大写字母开头,但库以小写字母引用。我们可以看到与 ocamlfind 相同。

$ ocamlfind query base
/home/wtd/.opam/5.0.0/lib/base
$ ocamlfind query Base
ocamlfind: Package `Base' not found

或在沙丘中。

(executable 
  (name main) 
  (libraries base ...))

确保 Base 模块在 utop 中可用的一种方法是启动 utop:

utop -require base
.


现在谈谈意见领域:Base 库与 Stdlib 不同,但更有效的判断可能是您对这门语言太陌生而无法做出的判断。您为

List.mem
提供的示例在 Basr 中比在 Stdlib 中更灵活,因为可以将确定相等性的函数作为参数提供,但对于这种简单的情况,它会使对
List.mem
的调用时间更长而没有任何好处。

现在,也许 Base 用户想要在不考虑大小写的情况下检查是否相等。他们可能会写:

List.mem ["a"] "A" ~equal:String.Caseless.equal

但是使用 Stdlib 我可能会写:

List.exists (fun s -> String.uppercase_ascii s = "A") ["a"]

那并不可怕。


0
投票

轻松完成:在

#use "topfind";;
之前添加
#require "Base";;
,然后添加
open Base

© www.soinside.com 2019 - 2024. All rights reserved.