理解 Erlang 列表理解生成器

Understanding Erlang List Comprehension Generators

给出这个函数:

pretty_print(Folders, Depth) ->
    
        {CurrrentFolder, ListSubfolders} = Folders,
        SignTemp = lists:duplicate(Depth, "-"),
        case Depth of
            0 -> Sign = SignTemp;
            _ -> Sign = "|" ++ SignTemp
        end,
    
        io:format("~s~s~n", [Sign, CurrrentFolder]),
        
        [pretty_print(Subfolder, Depth+1)|| Subfolder <- ListSubfolders].

当在列表理解中生成器为 null 或空时,Erlang 中的递归函数会发生什么情况?

所以当变量为空时,函数不会调用自己?

它不会在该行产生错误吗?没有什么可以控制的,这行不出错?

  {CurrrentFolder, ListSubfolders} = Folders,

例如在这段代码中,通过Depth控制另一个变量的行为:

case Depth of
    0 -> Sign = SignTemp;
    _ -> Sign = "|" ++ SignTemp
end,

这里有一点不太明白

函数到达目录的最后一个文件夹时会发生什么情况?

¿What happens to a recursive function in Elrang when in a list comprehesion the generator is null or empty?

当列表理解中的生成器为空时,理解为空。是否递归无关紧要

So when the variable is empty, the function won't call itself?

是的。

There is nothing to control that an error does not occur in this line?

不,它只是假设调用者会给出一个匹配它的参数(并且 ListSubfolders 是一个列表,其中的每个元素也匹配它)。直接在head里匹配会比较地道:

pretty_print({CurrrentFolder, ListSubfolders}, Depth) ->    
    SignTemp = lists:duplicate(Depth, "-"),
    ... %% the rest is the same

What happens to a recursive function in Elrang when in a list comprehesion the generator is null or empty?

易于测试:

-module(a).
-compile(export_all).

go(N) ->
    [go(X) || X <- [] ].

在shell:

3> a:go(0).
[]

您认为以下的 return 值是多少:

[ X+1 || X <- [] ]

与您定义的没有区别:

f(X) -> X+1.

然后执行:

[f(X) || X <- [] ]

如果没有用于调用函数的参数,则不会调用该函数。

只是为了更加清晰。下面的代码不是出于控制的目的——它们是 'just' 因为对于根文件夹输入,我们不想放 '|'文件夹名称前面的字符(化妆品)。因此,即使您将其更改为仅 Sign = SignTempSign = "|" ++ SignTemp 中的任何一个,它也不会改变逻辑。

case Depth of
    0 -> Sign = SignTemp;
    _ -> Sign = "|" ++ SignTemp
end,

在前面的代码中,Folders永远不会是[],它至少会有{CurrentFolder, []}的值(可能是空列表的是ListSubfolders). 并且由于下面的列表理解应用于 ListSubfolders,所以它是安全的,因为如果 ListSubfolders[],则不会调用 pretty_print

[pretty_print(Subfolder, Depth+1)|| Subfolder <- ListSubfolders].

我画这个是为了说明列表理解是如何工作的,以及你的函数的所有递归调用pretty_print。