Haskell函数来反转函数调用 [英] Haskell function to reverse function call
问题描述
我有一个lambda \x f - >在
,其中 foldM
操作中使用的fx x
是一个值, f :: a - > b
。
是否有一个内置函数可以实现这一点?
我可以取代
foldM(\xf - > fx)...
与一些 f'
foldM f'...
我认为 flip
会做到这一点,但它需要三个参数( flip ::(a - > b - > c) - > b - > a - > c
)
它可能类似于F#中的 |>
。
您可以使用 flip id
或 flip($)
(作为($)
)就是一个特殊的 id
):
Prelude> flip id 3(+2)
5
Prelude> flip($)7(> 10)
False
这是一个有趣的用法部分申请: id fx
with f
是一个函数,只是 fx
。很显然,这也与(flip id)xf
相同,所以 flip id
是你正在寻找的功能。
如果你觉得冒险,试着推断 flip id
或手动翻转($)
。这很有趣:)
I have a lambda \x f -> f x
that is being used in a foldM
operation, where x
is a value and f :: a -> b
.
Is there a built-in function that does this?
Can I replace
foldM (\x f -> f x) ...
with some f'
foldM f' ...
I thought that flip
would do this, but it takes three arguments (flip :: (a -> b -> c) -> b -> a -> c
)
It is probably similar to |>
in F#.
You can use flip id
or flip ($)
(as ($)
is just a special id
for functions):
Prelude> flip id 3 (+2)
5
Prelude> flip ($) 7 (>10)
False
This is an interesting use of partial application: id f x
with f
being a function is just f x
. Obviously, this is also the same as (flip id) x f
, so flip id
is the function you are looking for.
If you feel adventurous, try inferring the type of flip id
or flip ($)
manually. It's fun :)
这篇关于Haskell函数来反转函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!