Haskell递归子集 [英] Haskell Recursion Subsets

查看:66
本文介绍了Haskell递归子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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的子集之一精确地生成两个集ysx:ys.

generates exactly two sets, ys and x:ys for each ys which is one of the subsets of xs.

这篇关于Haskell递归子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆