ocaml null 关键字:一次但不再?

发布于 2024-12-09 07:09:19 字数 837 浏览 0 评论 0原文

在 ocaml 顶层(版本 3.11.2),这个简单的表达式给了我一个错误:

# let a = [] in if null a then 0 else 1;;
Error: Unbound value null

我刚刚开始从 oreilly book,这本书似乎经常使用 null 作为关键字 - 例如,第 32 页的顶部:

# let rec size a_list =
    if null a_list then 0
    else 1 + (size (List.tl a_list));;

我很尴尬在这里问这样一个明显可以谷歌搜索的问题。但经过多次谷歌搜索后,我却空手而归。因此,我对谷歌查询建议持开放态度,就像对简单答案持开放态度一样。 (谷歌尝试失败:[ocaml“错误:未绑定值null”] [ocaml null关键字] [ocaml变更日志null] [ocaml更改null])。

问题:ocaml 关键字曾经为 null,但现在不再为 null?或者我是否安装了 ocaml 错误或拼写错误?

我当然可以将代码中每次出现的“null”替换为“[]”,但令我惊讶的是,书中代码的逐字副本这么早就给了我一个错误。这本书还充满其他陷阱吗?我相信它是根据 ocaml 2.04 编写的;那太旧了吗?我选择它是因为我喜欢目录和免费在线可用性。除了这个空错误(我仍然更愿意责怪自己而不是作者),解释很好,我期待着混合功能和功能的讨论。命令式风格(对于我来说,作为一个只熟悉 c/c++ 的人来说,思维扩展)。

At the ocaml toplevel (version 3.11.2), this simple expression is giving me an error:

# let a = [] in if null a then 0 else 1;;
Error: Unbound value null

I have just started learning ocaml from the oreilly book, which seems to use null as a keyword frequently - for example, top of page 32:

# let rec size a_list =
    if null a_list then 0
    else 1 + (size (List.tl a_list));;

I'm embarrassed to ask such an obviously googleable question here. But after much googling I came up empty-handed. So I'm as open to google query suggestions as I am to straightforward answers. (failed google attempts: [ocaml "Error: Unbound value null"] [ocaml null keyword] [ocaml changelog null] [ocaml change null] ).

Question: was null once an ocaml keyword, but no longer? Or did I install ocaml wrong or misspell something?

I can of course replace every occurrence of "null" with "[ ]" in code, but I'm surprised that a verbatim copy of code from a book gives me an error so early. Is this book full of other gotchas? I believe it was written with ocaml 2.04 in mind; is that too old? I chose it because I liked the TOC and the free availability online. Other than this null error (which I am still more ready to blame on myself than on the authors), the explanations are nice and I'm looking forward to the discussion of mixing functional & imperative style (mind-expanding for me, as someone only familiar with c/c++).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2024-12-16 07:09:19

null 在本书第 31 页中定义。这是一个您可以自己定义的普通函数:

let null l = (l = [])

这个命名法或多或少基于 Lisp,其中 NIL 是一个空列表(也写为 ()),NULL 是一个谓词(a函数返回 true 或 false),与上面的 null 完全相同。

null is defined in the book on page 31. It's a normal function that you can define yourself:

let null l = (l = [])

This nomenclature is more or less based on Lisp, where NIL is an empty list (also written as ()) and NULL is a predicate (a function returning true or false), exactly like null above.

棒棒糖 2024-12-16 07:09:19

OCaml 提供了强大的模式匹配,它允许您为了更易读地定义函数:

let rec size a_list = match a_list with
 | [] -> 0
 | _ :: tl -> 1 + (size tl)

由于经常对最后一个参数执行模式匹配,因此可以使用特殊符号:

let rec size = function
 | [] -> 0
 | _ :: tl -> 1 + (size tl)

可以使用 (=) 来定义更短的 null (即,平等检验为普通/前缀函数):

let null = (=) []

OCaml provides powerful Pattern Matching, which allows you to define the function more readably:

let rec size a_list = match a_list with
 | [] -> 0
 | _ :: tl -> 1 + (size tl)

As pattern matching is often performed on the last argument, a special notation is available:

let rec size = function
 | [] -> 0
 | _ :: tl -> 1 + (size tl)

null could be defined shorter by using (=) (i.e., the test for equality as a normal/prefix function):

let null = (=) []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文