笛卡尔积不适用于应用程序

Cartesian product not working for applicative

我试图理解 applicative 以及如何将它用作 K 函数和 N 参数之间的笛卡尔积,但我无法理解为什么我不能执行以下操作:

[Just (+1),Just (+2)] <*> [Just 1 ,Just 2] 渲染

错误

* Couldn't match expected type `Maybe Integer -> b'
                  with actual type `Maybe (Integer -> Integer)'
    * Possible cause: `Just' is applied to too many arguments      In the expression: Just (+ 1)      In the first argument of `(<*>)', namely `[Just (+ 1), Just (+ 2)]'
      In the expression: [Just (+ 1), Just (+ 2)] <*> [Just 1, Just 2]

我不明白,因为根据定义,它应该从上下文中提取函数,获取值并应用所有组合。

我也试过了:

:t [pure (+1),pure (+2)] <*> [Just 1 ,Just 2] :: Num a => [a -> a] 我不明白为什么结果类型不是值列表(而不是 a->a),因为所有运算符只需要一个参数,而我已经提供了.

有人可以解释一下吗?

这里涉及两个应用层([]Maybe),所以(<*>)本身必须是应用层:

GHCi> (<*>) <$> [Just (+1),Just (+2)] <*> [Just 1 ,Just 2]
[Just 2,Just 3,Just 3,Just 4]

此用例由 Compose 新类型捕获。 :

GHCi> import Data.Functor.Compose
GHCi> Compose [Just (+1),Just (+2)] <*> Compose [Just 1 ,Just 2]
Compose [Just 2,Just 3,Just 3,Just 4]

.