我试图创建一个函数,在这个函数中给定一个 兰姆达术语 将返回所有单独的lambda术语
这是我的 查找T 函数。
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 (Variable _) m) = findTerms m
findTerms (Apply n m) = n : findTerms m
这里,如果第一个参数是 Apply
是一个 Variable _
因此,我们将不考虑它,否则,我们将产生 n
在列表中。
您可能还应该 递归 关于 n
和 m
,因为它也可以包含术语。对于一个lambda,你可以返回lambda本身。
findTerms :: Term -> [Term]
findTerms (Variable x) = []
findTerms l@(Lambda _ _) = [l]
findTerms (Apply n m) = findTerms n ++ findTerms m