Cha*_*upa 3 haskell functional-programming list
假设我有一个递归函数,它接受两个列表并返回一个类似的int
fn ys (x:xs)
| --some condition = original length of (x:xs)
| otherwise = fn ys xs
Run Code Online (Sandbox Code Playgroud)
如果条件一为真,我需要返回输入列表的原始长度(在递归之前已经搞乱了).有没有办法保存原来的长度?
您可以使用"worker"函数(传统命名go)执行递归,这允许您引用原始参数并定义额外变量:
fn ys xs' = go xs'
where
l = length xs'
go (x:xs)
| --some condition = l
| otherwise = go xs
Run Code Online (Sandbox Code Playgroud)
你可能也想要一个案例go [].