除了一般情况下的函数组合外,您还可以将 keepAll 的特定结果分配给变量并稍后使用该值:
outputList = keepAll 3 [1,2,3,3,3,4,5,3]
print (init outputList) >> [3,3,3]
print (length outputList) >> 4
如果您想访问函数内部递归的输出列表,您可能希望将递归委托给内部的“帮助器”函数,例如:
keepSome y (x:xs) = keepAll y (x:xs)
where keepAll _ [] = []
keepAll y (x:xs) | x==y = y:keepAll y xs
| otherwise = keepAll y xs
现在您可以更改第一行,以便按照您的建议将“init”应用于递归结果:
keepSome y (x:xs) = init $ keepAll y (x:xs)
where keepAll _ [] = []
keepAll y (x:xs) | x==y = y:keepAll y xs
| otherwise = keepAll y xs
例如,您还可以将递归的输出列表命名为“outputList”,如果它使您更容易使用,并将 init 应用于该列表:
keepSome y (x:xs) = init outputList
where outputList = keepAll y (x:xs)
keepAll _ [] = []
keepAll y (x:xs) | x==y = y:keepAll y xs
| otherwise = keepAll y xs
样本输出:
*Main> keepSome 3 [1,2,3,3,3,4,5,3]
[3,3,3] --init of the inside result, [3,3,3,3]