I am trying to create a function in which given a lambda term will return all separate lambda terms
Here is my findT function:
findT :: T -> [T]
findT (V x) = []
findT (L x n) = [] ++ findT n
findT (A n m) = [n] ++ findT m
When I run this function on two separate tests, it works with the first but not the second.
You can look if n
is a Variable
, and if so, do not include it, for example with:
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
Here if the first parameter of Apply
is a Variable _
, we will thus not consider it, otherwise we will yield n
in the list.
You probably should also recurse on n
and m
, since that can contain terms as well. For a lambda you then can return the lambda itself:
findTerms :: Term -> [Term]
findTerms (Variable x) = []
findTerms l@(Lambda _ _) = [l]
findTerms (Apply n m) = findTerms n ++ findTerms m