When matching a template, you need to match the constructor in which you passed. In your example, Expr may have an Operator constructor that contains relevant print information:
printOut (Num n) = show n printOut X = "x" printOut (Operator op ab) = printOut a ++ printOp op ++ printOut b printOut (Function fu a) = printFun fu ++ printOut a
Now you simply define additional pattern matches for your operators and functions:
printOp Add = "+" printOp Mul = "x" printFun Sin = "Sin" printFun Cos = "Cos"
In addition to what @WillemVanOnsem wrote, you actually don't print anything on the screen with these print features. Instead, you convert them from your data type to a string. There is a regular type called Show . You can also simply create show instances for your types and use this function to display them:
instance Show Fu where show Sin = "Sin" show Cos = "Cos"
and then use Show instead:
printOut (Function fu a) = show fu ++ printOut a
If you have a simple instance of Show , you can also just get it automatically for you, using the same syntax that you use to get Eq :
data Fu = Sin | Cos deriving (Show, Eq)
To handle creating an Expr instance, you again use various constructors:
let anExpr = Operator Add (Num 3.0) (Num 4.0)
source share