在 Erlang 中,内置的 head 和 tail 函数在哪里?
In Erlang, where are the builtin head and tail functions?
我知道可以使用模式匹配从列表中提取头部和尾部。但是有必要吗? lists
模块中没有名为 head
或 tail
的内容,呃。 (它确实包括 nth/2
和 nthtail/2
,它们更强大,但这些似乎是实现 head
和 tail
的不自然冗长的方法。)
我也没有在顶级命名空间中找到它们:
Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]
Eshell V12.0 (abort with ^G)
1> head([1,2]).
** exception error: undefined shell command head/1
2> tail([1,2]).
** exception error: undefined shell command tail/1
模式匹配是访问列表头部和尾部的推荐方式。过于频繁地使用函数访问它们的代码可能不是很好的惯用 Erlang。然而,在极少数情况下,这些功能确实很有用。您可以在名称为 hd
和 tl
.
的 this link 中的其他 Erlang 内置函数中找到它们
模式匹配是函数式编程语言对数据进行操作的标准方式,Erlang 也不例外。因此也存在一种处理列表的方法。然后,List
是 native
/built-in
/primitive
类型,可用的是那些 hd
和 tl
BIF(s) 来操作在他们之上。
我知道可以使用模式匹配从列表中提取头部和尾部。但是有必要吗? lists
模块中没有名为 head
或 tail
的内容,呃。 (它确实包括 nth/2
和 nthtail/2
,它们更强大,但这些似乎是实现 head
和 tail
的不自然冗长的方法。)
我也没有在顶级命名空间中找到它们:
Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]
Eshell V12.0 (abort with ^G)
1> head([1,2]).
** exception error: undefined shell command head/1
2> tail([1,2]).
** exception error: undefined shell command tail/1
模式匹配是访问列表头部和尾部的推荐方式。过于频繁地使用函数访问它们的代码可能不是很好的惯用 Erlang。然而,在极少数情况下,这些功能确实很有用。您可以在名称为 hd
和 tl
.
模式匹配是函数式编程语言对数据进行操作的标准方式,Erlang 也不例外。因此也存在一种处理列表的方法。然后,List
是 native
/built-in
/primitive
类型,可用的是那些 hd
和 tl
BIF(s) 来操作在他们之上。