为什么在Haskell中从右到左组成点? [英] Why does the dot compose from right to left in Haskell?

查看:113
本文介绍了为什么在Haskell中从右到左组成点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有两个函数: f g ,那么在Haskell h = F 。 g 相当于 h x = f(g x)。即功能从右到左应用于输入。为什么它从右到左,而不是从左到右有什么根本原因?即为什么他们不让 h = f。 g 相当于 hx = g(fx)而不是?

编辑:as others指出了我的等价函数在错误的方法,所以我修正了这些。 解决方案

首先,你的错误[原创,未经编辑]问题:


h = f。 g相当于h x = g(f x)

- 那不是真的: h = f。 g 相当于 hx = f(gx)



然而,为什么这样,而不是其他方式,这很可能是因为它是如何工作,并在数学工作;请参阅 http://en.wikipedia.org/wiki/Function_composition


[em] composite 函数表示为 g∘f:X→Z ,由所有 x X 中的所有(x)= g(f(x))

由于等于(f。g)x == f(gx),所以它也很直观,正如你所看到的, f g 在双方都是相同的。



< hr>

此外,如果您希望创建自己的逆向组合运算符,例如可读性:

 (。>)=翻转(。)

因此

  Prelude> ((+1)。>(* 2))3 
8
前奏> ((+1)。(* 2))3
7






实际上,您可以使用 Control.Arrow。(>>>),它对函数做了相同的处理,也适用于其他事情:

  Prelude Control.Arrow> ((+1)>>>(* 2))3 
8


If we have two functions, f and g, then in Haskell h = f . g is equivalent to h x = f(g x). I.e. the functions are applied from right to left to the input. Is there any fundamental reason why it goes from right to left, and not from left to right? I.e. why didn't they make h = f . g equivalent to h x = g(f x) instead?

EDIT: as others pointed out my equivalent functions where the wrong way around, so I fixed those.

解决方案

First of all, there's a mistake in your [original, unedited] question:

h = f . g is equivalent to h x = g(f x)

— that's not true: h = f . g is equivalent to h x = f (g x).

However, as to why it's that way and not the other way around, it's most likely because that's how it works and has worked in math; see http://en.wikipedia.org/wiki/Function_composition:

[...] composite function is denoted g ∘ f : X → Z, defined by (g ∘ f )(x) = g(f(x)) for all x in X.

It's also intuitive because of the equality (f . g) x == f (g x) — as you can see, the order of f and g is the same on both sides.


Moreover, it's trivial to create your own "reverse composition" operator if you desire one for reasons of e.g. readability:

(.>) = flip (.)

so that

Prelude> ((+1) .> (*2)) 3
8
Prelude> ((+1) . (*2)) 3
7


In fact, you can just use Control.Arrow.(>>>) which does the same for functions but is more general and works for other things as well:

Prelude Control.Arrow> ((+1) >>> (*2)) 3
8

这篇关于为什么在Haskell中从右到左组成点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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