申请人的互换法 [英] Applicative's Interchange Law

查看:61
本文介绍了申请人的互换法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有效果的应用编程,McBride和Paterson的论文,显示交换法:

Applicative Programming with Effects, the paper from McBride and Paterson, presents the interchange law:

u <*> pure x = pure (\f -> f x) <*> u

为了尝试理解它,我尝试了以下示例-代表左侧.

In order to try to understand it, I attempted the following example - to represent the left-hand side.

ghci> Just (+10) <*> pure 5
Just 15

如何用右侧写这个例子?

How could I write this example using the right-hand side?

此外,如果uf (a -> b),其中fApplicative,那么右边的功能是什么:pure (\f -> f x) ...?

Also, if u is an f (a -> b) where f is an Applicative, then what's the function on the right-hand side: pure (\f -> f x) ...?

推荐答案

它将写为

pure (\f -> f 5) <*> Just (+10)

甚至

pure ($ 5) <*> Just (+10)

在这种情况下两者都是等效的.从字面上看,您要使用pure包装一个函数,该函数将另一个函数作为参数,然后将x应用于该函数.您提供f作为Just的内容,在本例中为(+10).当您看到(\f -> f x)的lambda语法时,它非常直观,这是用于此定义的lambda.

Both are equivalent in this case. Quite literally, you're wrapping a function with pure that takes another function as its argument, then applies x to it. You provide f as the contents of the Just, which in this case is (+10). When you see the lambda syntax of (\f -> f x), it's being very literal, this is a lambda used for this definition.

这篇关于申请人的互换法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆