Haskell递归子集 [英] Haskell Recursion Subsets
问题描述
我是Haskell的新手.我发现很难用递归来理解下面的子程序.它的评估方式是什么?
i am new to Haskell. i am find it hard to understand the below subset program with recursion.what is the way it evaluates?
subset :: [a] -> [[a]]
subset [] = [[]]
subset (x:xs) = [zs | ys <- subset xs, zs <- [ys,(x:ys)]]
输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]
当我手动评估时,其总结如下
When i manually evaluate it is summing up as below
手动输出:[],[3],[2],[1,2]
我在这里缺少一些逻辑,您能帮我理解上面的递归概念吗?请先评估保护声明的哪一部分,评估顺序?
I am missing some logic here can you please help me understand the above recursion concept and which part of guard statement will be evaluated first ,order of evaluation?
推荐答案
最后一行说,x:xs
的子集集是xs
的子集集,而x
都添加到了这些集中
The last line says that the set of subsets of x:xs
is the set of subsets of xs
along with x
added to each of those sets.
另一种表达方式是
zs <- [ys,(x:ys)]
为每个xs
的子集之一精确地生成两个集ys
和x:ys
.
generates exactly two sets, ys
and x:ys
for each ys
which is one of the subsets of xs
.
这篇关于Haskell递归子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!