作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个递归函数,它接受两个列表并返回一个 int,如下所示
fn ys (x:xs)
| --some condition = original length of (x:xs)
| otherwise = fn ys xs
如果条件一为真,我需要返回输入列表的原始长度(在递归弄乱它之前)。有没有办法保存原来的长度?
最佳答案
您可以使用“worker”函数(传统上称为 go
)进行递归,它允许您引用原始参数并定义额外的变量:
fn ys xs' = go xs'
where
l = length xs'
go (x:xs)
| --some condition = l
| otherwise = go xs
您可能还想要一个 go []
的保护套。
关于list - 有没有办法在递归时将列表的长度保存在变量中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29382195/
我是一名优秀的程序员,十分优秀!