在Haskell中,(<*>)运算符的默认实现(它将函数a->b的应用应用于a导致应用程序的应用程序b)在Control.Applicative中定义为 -
(<*>) :: f (a -> b) -> f a -> f b
(<*>) = liftA2 id
Run Code Online (Sandbox Code Playgroud)
而我根本无法理解它是如何工作的.
liftA2具有类型liftA2 :: (a -> b -> c) -> f a -> f b -> f c,意味着它需要二进制函数,而id不是.根据我的理解,这种方式id在某种程度上被解释为一些更复杂的类型 - 但我不确定它是什么或如何使这个定义工作.如果有人也许可以解释什么类型的id解释为(在什么类型a的id :: a -> a定义代表),也走过它是如何产生这需要的功能的应用性和价值的应用性和适用他们,我会非常感激的功能.
我正在尝试创建一个系统来派生符号函数,我遇到了一个问题:
我有一个表达式的类型类Exp,它定义了一个派生函数:
class Exp e where
derivative :: (Exp d) => e -> d
Run Code Online (Sandbox Code Playgroud)
我希望该类有几个实例:
data Operator a b = a :* b | a :+ b
instance (Exp a, Exp b) => Exp (Operator a b) where
derivative (f :* g) = ((derivative f) :* g) :+ (f :* (derivative g)) --The derivative of the multiplication of two expressions
derivative (f :+ g) = derivative f :+ derivative g --The derivative of the addition of two expressions …Run Code Online (Sandbox Code Playgroud)