gpt4 book ai didi

haskell - `flip` 中缀应用程序内联参数

转载 作者:行者123 更新时间:2023-12-02 13:08:26 25 4
gpt4 key购买 nike

给定一个用于中缀用法的函数:

let f a b = (a+10, b)
f 4 5
=> (14,5)
4 `f` 5
=> (14,5)

可以通过定义辅助函数来翻转参数:

let g = flip f
4 `g` 5
=> (15,4)

是否可以内联执行此操作?

4 `flip f` 5
=> parse error on input `f'
4 `(flip f)` 5
=> parse error on input `('
<小时/>

我的用例是Control.Arrow.first。而不是

(+10) `first` (7,8)
(17,8)

我更喜欢像这样的前向应用程序式解决方案

(7,8) `forwardFirst` (+10)

无需书写

let forwardFirst = flip first

最佳答案

HaskellWiki article on infix operators 中详述,

Note that you can only normally do this with a function that takes two arguments. Actually, for a function taking more than two arguments, you can do it but it's not nearly as nice

在您的情况下执行此操作的方法如下:

let f a b = (a+10, b)
let h a b = (f `flip` a) b
let a = 3
let b = 2
f a b = (13,2)
a `h` b = (12,3)

关于haskell - `flip` 中缀应用程序内联参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27058604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com