
Alexteslin wrote:
Hi, I am doing some simple exercises about recursive algebraic types and this particular exercise asks to define a function which counts the number of operators in an expression. I defined the function below, but i am not sure if on the second line changing from "evalLength (Lit n) = n" to "(Lit n) = 0" is the right solution. although the function produces correct result.
data Expr = Lit Int | Add Expr Expr | Sub Expr Expr deriving (Eq, Show)
evalLength :: Expr -> Int evalLength (Lit n) = 0 evalLength (Add e1 e2) = 1 + (evalLength e1) + (evalLength e2) evalLength (Sub e1 e2) = 1 + (evalLength e1) - (evalLength e2)
Thank you
It actually doesn't work. Initially i tested on Add operator only. But whith Sub operator it produces wrong result. -- View this message in context: http://www.nabble.com/Primitive-Recursive-Algebraic-Types-tf4207521.html#a11... Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.