【发布时间】:2020-05-25 20:25:43
【问题描述】:
我在玩 Elixir 宏 - 特别是自称宏,这是我在 Scheme 中经常做的事情。我在下面创建了一个小测试宏,但它只是挂起 iex - 没有任何内容打印到控制台。有没有人知道为什么以及可以做些什么来纠正它?
defmodule RecMac do
defmacro test_rec(x) do
quote do
IO.puts("Started?")
if(unquote(x) < 1) do
IO.puts("Done?")
"done"
else
IO.puts("Where are we")
IO.puts(unquote(x))
RecMac.test_rec(unquote(x) - 1)
end
end
end
end
编辑!!
好的,事实证明你可以定义递归宏,其中存在要匹配的结构差异(例如列表)。以下对我有用。并在下面确认@Aleksei Matiushkin,上面的方法不起作用,而且在方案中确实不起作用!
defmacro test_rec([h | t]) do
quote do
IO.inspect([unquote(h) | unquote(t)])
RecMac.test_rec(unquote(t))
end
end
defmacro test_rec([]) do
quote do
IO.puts "Done"
end
end
end
我很高兴能够深入研究这一点,因为我学到了两种语言的知识!
【问题讨论】: