My goal is to define a function "proper" which takes in a "Tree a" Datatype:
data Tree a = One a | Two (Tree a) (Tree a)
and produces all proper subtrees (i.e. all subtrees except itself) with a recursive definition.
My current code looks like this:
proper :: Tree a -> [Tree a]
proper (One a) = []
proper (Two t1 t2) = [t1,t2] ++ proper' t1 ++ proper' t2
proper' (One a) = [One a]
proper' (Two t1 t2) = [t1,t2] ++ proper' t1 ++ proper' t2
and I was wondering whether there is any way to get proper'
in a where clause instead. I have tried using standard pattern matching and case expressions but both lead to parser errors.
Note: there is a previous function I made called subs which produces all subtrees of the tree including itself but I am not allowed to use this in my definition of proper
edit: I solved it without having to define a new function anyway, but the where clause should work I must have messed it up in the script. Working code now:
proper :: Tree a -> [Tree a]
proper (One a) = []
proper (Two t1 t2) = [t1,t2] ++ proper t1 ++ proper t2
Yes, you can:
proper :: Tree a -> [Tree a]
proper (One a) = []
proper (Two t1 t2) = [t1,t2] ++ proper' t1 ++ proper' t2
where
proper' (One a) = [One a]
proper' (Two t1 t2) = [t1,t2] ++ proper' t1 ++ proper' t2
However I don't see for what you need proper'
as this code works as well (and in contrary to yours doesn't duplicate the leaves):
proper :: Tree a -> [Tree a]
proper (One a) = []
proper (Two t1 t2) = [t1,t2] ++ proper t1 ++ proper t2