bea*_*rdc 6 monads haskell pointfree
我遇到了一个Haskell函数,它告诉我们列表是否已经排序,而且我很难理解它是如何工作的.
有问题的代码是
f = zipWith (<=) <*> tail
Run Code Online (Sandbox Code Playgroud)
我明白这是相当的(在点式)
f' xs = zipWith (<=) xs (tail xs)
Run Code Online (Sandbox Code Playgroud)
并作为一个例子返回
f [4, 5, 1] == [True,False]
Run Code Online (Sandbox Code Playgroud)
我认为它与列表monad和顺序应用程序有关,但如果有人能让我的意思更清楚,我将不胜感激.到底在<*>
做什么?
Dan*_*zer 12
在<*>
这里不作用于被[a]
应用性,它的作用在(->) a
应用性实例.
实质上
instance Applicative ((->) a) where
pure = const -- same as monadic return
f <*> a = \x -> f x (a x)
Run Code Online (Sandbox Code Playgroud)
因此它就像函数应用程序一样,但也将应用程序包装在一个函数中,并将参数提供给双方.
所以扩展你的功能
zipWith (<=) <*> tail
\x -> zipWith (<=) x (tail x)
\(x:xs) -> zipWith (<=) (x:xs) xs
Run Code Online (Sandbox Code Playgroud)
一般来说<*>
,仅仅查看功能应用程序+一些额外的好东西是正确的.你几乎可以把它看成空格!
<*>
实际上是(->) a
作为Applicative Functor.这是一个S-组合子,其分布参数(列表xs
中您的扩展),以两个函数(zipWith (<=)
和tail
您在扩张中所指定的方式)(f <*> g) x = f x (g x)
.
要理解这一点,您需要检查应用的类型(<*>)
.由于它的两个论点都是a->b
,我们所说的a->b
是Applicative Functor - 而不是List.