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

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

问题描述

例如, Haskell 具有 id 函数,Julia具有 identity code>函数,以及许多关于SO处理身份函数的问题。 (我在Python中假设你可以做 lambda x:x

我一直在努力考虑这个函数的用例;我失败了。



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

解决方案

请记住,在Haskell函数中是第一类的值,可以像其他值一样用作数据,并作为参数传递给其他值功能。通常你通过向对方应用其他功能来构建你真正想使用的功能。偶尔你会发现你想在某个地方使用的函数并不比 id 更复杂。



例如,下面是一个函数,它可以否定列表中的每一个第二个元素:

  negateEverySecond = zipWith id(cycle [id,否定])


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.

What is the purpose of the identity function, and what are its common use-cases?

解决方案

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天全站免登陆