首页 > 解决方案 > 在 Erlang 中,内置的 head 和 tail 函数在哪里?

问题描述

我知道可以使用模式匹配从列表中提取头部和尾部。但这有必要吗?模块中没有调用heador ,并且 erl. (它确实包括更强大的and ,但这些似乎是实现and的不自然冗长的方法。)taillistsnth/2nthtail/2headtail

我也没有在顶级命名空间中找到它们:

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

标签: listerlang

解决方案


模式匹配是访问列表中头部和尾部的推荐方法。经常使用函数来访问它们的代码可能不是很好的惯用 Erlang。然而,在极少数情况下,这些功能确实很有用。您可以在此链接的名称hdtl.


推荐阅读