用无点样式写f吗? [英] Write f in pointfree-style?

查看:108
本文介绍了用无点样式写f吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有功能

g :: a -> b, h :: a -> c 

f :: b -> c -> d. 

是否可以编写函数

 f' :: a -> a -> d 

f' x y = f (g x) (h y) 

自由点式吗?.

一个可以编写函数

f' a -> d, f' x = f (g x) (h x) 

通过设置

f' = (f <$> g) <*> h  

但是我不知道该怎么做.

but I couldn't figure out how to do the more general case.

推荐答案

我们有:

k x y = (f (g x)) (h y)

,我们希望以无点样式编写k.

and we wish to write k in point-free style.

传递给k的第一个参数是x. x我们需要做什么?好吧,首先我们需要先调用g,然后调用f,然后再做一些花哨的操作,以将其应用于(h y).

The first argument passed to k is x. What do we need to do with x? Well, first we need to call g on it, and then f, and then do something fancy to apply this to (h y).

k = fancy . f . g

这是什么fancy?好吧:

k x y = (fancy . f . g) x y
      = fancy (f (g x)) y
      = f (g x) (h y)

所以我们渴望fancy z y = z (h y).减少Eta,我们得到fancy z = z . hfancy = (. h).

So we desire fancy z y = z (h y). Eta-reducing, we get fancy z = z . h, or fancy = (. h).

k = (. h) . f . g


一种更自然的思考方式可能是


A more natural way to think about it might be

                             ┌───┐           ┌───┐
                        x ───│ g │─── g x ───│   │
                      /      └───┘           │   │
               (x, y)                        │ f │─── f (g x) (h y)
                      \      ┌───┐           │   │
                        y ───│ h │─── h y ───│   │
                             └───┘           └───┘

                      └──────────────────────────────┘
                                      k

输入 Control.Arrow :

k = curry ((g *** h) >>> uncurry f)

这篇关于用无点样式写f吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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