元组和函数组合

发布于 2024-12-28 15:49:24 字数 88 浏览 0 评论 0原文

有没有更好的方法用函数组合来表达 (\(a, b) -> a < b) ?我觉得我错过了一些东西,尝试咖喱只会让我更加困惑。

Is there a better way to express (\(a, b) -> a < b) with function composition? I feel like I'm missing something and experimenting with curry only confused me more.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2025-01-04 15:49:24

curry 在这里使用是错误的;它将对元组进行操作的函数转换为柯里化函数。您想要相反的内容,即 uncurry

uncurry :: (a -> b -> c) -> (a, b) -> c

在本例中,它是 uncurry (<)

(在元组上编写函数时有用的组合器的另一个有用来源是 Control.Arrow; 由于 (->)Arrow 的实例,因此您可以读取 ab c 作为b -> c。)

curry is the wrong thing to use here; it turns a function operating on tuples into a curried function. You want the opposite, which is uncurry:

uncurry :: (a -> b -> c) -> (a, b) -> c

In this case, it's uncurry (<).

(Another useful source for combinators useful in writing functions on tuples is Control.Arrow; since (->) is an instance of Arrow, you can read a b c as b -> c.)

捂风挽笑 2025-01-04 15:49:24

在 Haskell 中,查看类型是了解函数功能的最佳方式:

curry :: ((a, b) -> c) -> a -> b -> c
uncurry :: (a -> b -> c) -> (a, b) -> c

curry:pair → curried 函数的函数(它柯里化函数)。

uncurry:柯里化函数→pair 函数。

关于柯里化的 Haskell Wiki 页面 在页面末尾有一些小练习:

  • 简化curry ID
  • 简化uncurry const
  • 使用 curryuncurry 以及其他基本 Prelude 函数且不使用 lambda 来表达 snd
  • 编写函数\(x,y) -> (y,x) 没有 lambda,只有 Prelude 函数

立即尝试解决这些练习,它们将使您深入了解 Haskell 类型系统和函数应用。

uncurry 有几个有趣的应用,尝试将不同的参数传递给下面的函数,看看它们的作用:

uncurry (.) :: (b -> c, a -> b) -> a -> c
uncurry (flip .) :: (b -> a -> b1 -> c, b) -> b1 -> a -> c
uncurry (flip (.)) :: (a -> b, b -> c) -> a -> c
uncurry ($) :: (b -> c, b) -> c
uncurry (flip ($)) :: (a, a -> c) -> c

-- uncurry (,) is an identity function for pairs
uncurry (,) :: (a, b) -> (a, b)
uncurry (,) (1,2) -- returns (1,2)
uncurry uncurry :: (a -> b -> c, (a, b)) -> c
uncurry uncurry ((+), (2, 3)) -- returns 5

-- curry . uncurry and uncurry . curry are identity functions
curry . uncurry :: (a -> b -> c) -> (a -> b -> c)
(curry . uncurry) (+) 2 3 -- returns 5
uncurry . curry :: ((a, b) -> c) -> ((a, b) -> c)
(uncurry . curry) fst (2,3) -- returns 2

-- pair -> triple
uncurry (,,) :: (a, b) -> c -> (a, b, c)
uncurry (,,) (1,2) 3 -- returns (1,2,3)

Looking at the types is the best way in Haskell to get the first idea, what any function does:

curry :: ((a, b) -> c) -> a -> b -> c
uncurry :: (a -> b -> c) -> (a, b) -> c

curry: function of pair → curried function (it curries a function).

uncurry: curried function → function of pair.

Haskell Wiki page on currying has small exercises at the end of the page:

  • Simplify curry id
  • Simplify uncurry const
  • Express snd using curry or uncurry and other basic Prelude functions and without lambdas
  • Write the function \(x,y) -> (y,x) without lambda and with only Prelude functions

Try to solve these exercises right now, they will give you a massive insight into Haskell type system and function application.

There are several interesting applications of uncurry, try to pass different arguments to functions below and see what they do:

uncurry (.) :: (b -> c, a -> b) -> a -> c
uncurry (flip .) :: (b -> a -> b1 -> c, b) -> b1 -> a -> c
uncurry (flip (.)) :: (a -> b, b -> c) -> a -> c
uncurry ($) :: (b -> c, b) -> c
uncurry (flip ($)) :: (a, a -> c) -> c

-- uncurry (,) is an identity function for pairs
uncurry (,) :: (a, b) -> (a, b)
uncurry (,) (1,2) -- returns (1,2)
uncurry uncurry :: (a -> b -> c, (a, b)) -> c
uncurry uncurry ((+), (2, 3)) -- returns 5

-- curry . uncurry and uncurry . curry are identity functions
curry . uncurry :: (a -> b -> c) -> (a -> b -> c)
(curry . uncurry) (+) 2 3 -- returns 5
uncurry . curry :: ((a, b) -> c) -> ((a, b) -> c)
(uncurry . curry) fst (2,3) -- returns 2

-- pair -> triple
uncurry (,,) :: (a, b) -> c -> (a, b, c)
uncurry (,,) (1,2) 3 -- returns (1,2,3)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文