假设我有一个递归函数,该函数需要两个列表并返回一个int,如下所示
fn ys (x:xs)
| --some condition = original length of (x:xs)
| otherwise = fn ys xs
如果条件一为真,我需要返回输入列表的原始长度(在递归处理之前)。有没有办法保存原始长度?
您可以使用“工作者”函数(传统上名为go
)进行递归,该函数可用来引用原始参数并定义其他变量:
fn ys xs' = go xs'
where
l = length xs'
go (x:xs)
| --some condition = l
| otherwise = go xs
您可能还需要一个案例go []
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句