[Haskell-cafe] Primitive Recursive Algebraic Types

Alexteslin alexteslin at yahoo.co.uk
Thu Aug 2 13:33:59 EDT 2007

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 
View this message in context: http://www.nabble.com/Primitive-Recursive-Algebraic-Types-tf4207521.html#a11969026
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

More information about the Haskell-Cafe mailing list