Haskell Prelude中的'const'有什么意义? [英] What's the point of 'const' in the Haskell Prelude?

查看:136
本文介绍了Haskell Prelude中的'const'有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Haskell序曲,我看到一个函数 const

  const x _ = x 

我似乎无法找到任何与此功能相关的内容。



有什么意义?任何人都可以举一个例子说明这个函数可以用在哪里?

解决方案

当你不愿意传递给高阶函数不需要所有的灵活性。例如,一元序列运算符>>> 可以用一元绑定运算符定义为

  x>>> y = x>> = const y 

它比使用lambda

  x>>> y = x>> = \ _  - > y 

并且您甚至可以使用它无点

 (>>)=(。const)。 (> =)

虽然我不特别推荐在这种情况下。 p>

Looking through the Haskell Prelude, I see a function const:

const x _ = x

I can't seem to find anything relevant regarding this function.

What's the point? Can anyone give an example of where this function might be used?

解决方案

It's useful for passing to higher-order functions when you don't need all their flexibility. For example, the monadic sequence operator >> can be defined in terms of the monadic bind operator as

x >> y = x >>= const y

It's somewhat neater than using a lambda

x >> y = x >>= \_ -> y

and you can even use it point-free

(>>) = (. const) . (>>=)

although I don't particularly recommend that in this case.

这篇关于Haskell Prelude中的'const'有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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