【发布时间】:2016-04-30 03:56:54
【问题描述】:
我正在研究 Phoenix 如何将模板呈现为 iodata,我发现一些列表对我来说很奇怪。对于涉及“竖条”或“竖管”字符 (|) 的列表,我似乎遗漏了一些基本语法。
以下是一些我明白理解的例子:
# prepends 1 to the list [2, 3]
l = [1 | [2, 3]] #=> [1, 2, 3]
# matches the head and tail into variables
inspect_tail = fn ([_head | tail]) -> IO.inspect(tail) end
我得到的那些。但是这是什么?
l = ["hi" | "there"] #=> ["hi" | "there"]
好像是一个有头有尾的列表:
is_list(["hi" | "there"]) #=> true
hd(["hi" | "there"]) #=> "hi"
tl(["hi" | "there"]) #=> "there"
...但是如果给定这样的列表,length(list) 函数会给出ArgumentError。
它是什么,它有什么用途?
【问题讨论】:
-
更多谷歌搜索让我觉得这可能是一个“不正确的列表”——对吗?
-
仍在尝试了解用例。正如this article 所示,
:re.replace(与Regex.replace不同)返回一个不正确的列表::re.replace("sing", "i", "o") #> ["s", "o", | "ng"]。我知道它正在重用现有字符串,但我不明白为什么结果不是["s", "o", "ng"]
标签: elixir