人们使用身份识别功能的是什么? [英] What do people use the identity function for?
问题描述
例如, 我一直在努力考虑这个函数的用例;我失败了。 身份函数的用途是什么,它的常见用例是什么? 请记住,在Haskell函数中是第一类的值,可以像其他值一样用作数据,并作为参数传递给其他值功能。通常你通过向对方应用其他功能来构建你真正想使用的功能。偶尔你会发现你想在某个地方使用的函数并不比 例如,下面是一个函数,它可以否定列表中的每一个第二个元素: For example, 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 For example, here is a function that negates every second element of a list:
这篇关于人们使用身份识别功能的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Haskell
具有 id
函数,Julia具有 identity code>函数,以及许多关于SO处理身份函数的问题。 (我在Python中假设你可以做
lambda x:x
)
id
更复杂。
negateEverySecond = zipWith id(cycle [id,否定])
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
)id
.negateEverySecond = zipWith id (cycle [id, negate])