我有什么方法可以制作一个以数学方程为参数的函数?例如
derive :: --equation
...
main = print (derive $ x**2-x)
我是否需要制作自定义数据类型?
答案 0 :(得分:3)
是;您需要创建表示公式的数据类型,而不是传递需要评估的Haskell表达式。例如,
data Formula = Term Int Int -- coefficient and exponent
| Sum Formula Formula deriving Show
derive :: Formula -> Formula
derive (Term coeff exp) = Term (coeff * exp) (exp - 1)
derive (Sum x y) = Sum (derive x) (derive y)
main = print (derive $ Sum (Term 1 2) (Term (-1) 1))