Haskell Lambda 帮助 - 从 lambda 项输入中拆分项

Haskell Lambda help - Splitting up terms from a lambda-term input

我正在尝试创建一个函数,其中给定一个 lambda 项 将 return 所有单独的 lambda 项

这是我的 findT 函数:

findT :: T -> [T]
findT (V x) = []
findT (L x n) = [] ++ findT n
findT (A  n m) = [n] ++ findT m

当我 运行 在两个单独的测试中使用此功能时,它适用于第一个但不适用于第二个。

您可以查看 n 是否是 Variable,如果是,请不要包含它,例如:

findTerms :: Term -> [Term]
findTerms (Variable x) = []
findTerms (Lambda x n) = findTerms n
findTerms (Apply <b>(Variable _)</b> m) = findTerms m
findTerms (Apply n m) = n : findTerms m

这里如果Apply的第一个参数是Variable _,我们就不考虑了,否则会yield n在列表中。

您可能还应该在 nmrecurse,因为它们也可以包含术语。对于 lambda,您可以 return lambda 本身:

findTerms :: Term -> [Term]
findTerms (Variable x) = []
findTerms l@(Lambda _ _) = [l]
findTerms (Apply n m) = findTerms n ++ findTerms m