人们使用身份功能做什么? [英] What do people use the identity function for?

查看:26
本文介绍了人们使用身份功能做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,Haskellid 函数,Julia 有 identity 函数,SO 的很多问题都涉及到identity 函数.(我想在 Python 中你可以做 lambda x:x)

For example, Haskell has the id function, Julia has the identity function, and many questions on SO deal with the identity function. (I suppose in Python you can do lambda x:x)

我一直在绞尽脑汁想这个功能的用例;我失败了.

I've been busting my brain trying to think of a use case for this function; and I've failed.

身份函数的目的是什么,它的常见用例是什么?

推荐答案

请记住,在 Haskell 中,函数是一等值,可以像其他值一样用作数据,并作为参数传递给其他函数.通常,您通过将其他功能相互应用来构建您真正想要使用的功能.偶尔你会发现,你想在某个地方使用的函数,恰好不比id复杂.

Remember that in Haskell functions are first class values, and can be used as data the same way as other values, and passed as arguments to other functions. Often you build the functions you really want to use by applying other functions to each other. Occasionally you will find that the function you want to use in a spot happens to be nothing more complicated than id.

例如,这里有一个对列表的每个第二个元素求反的函数:

For example, here is a function that negates every second element of a list:

negateEverySecond = zipWith id (cycle [id, negate])

这篇关于人们使用身份功能做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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