Any difference between First Class Function and High Order Function

前端 未结 6 1489
小鲜肉
小鲜肉 2021-01-29 17:23

I\'m wondering whether/what difference between First Class Function and High Order Function.

I read through those two wiki pages and they looks rather similar. If they

6条回答
  •  借酒劲吻你
    2021-01-29 18:20

    They're different.

    First class functions

    Values in a language that are handled uniformly throughout are called "first class". They may be stored in data structures, passed as arguments, or used in control structures.

    Languages which support values with function types, and treat them the same as non-function values, can be said to have "first class functions".

    Higher order functions

    One of the consequences of having first class functions is that you should be able to pass a function as an argument to another function. The latter function is now "higher order". It is a function that takes a function as an argument.

    The canonical example is "map"

    map :: (a -> b) -> [a] -> [b]
    map f []     = []
    map f (x:xs) = f x : map f xs
    

    That is, it takes a function, and an array, and returns a new array with the function applied to each element.

    Functional languages -- languages where functions are the primary means of building programs -- all have first class functions. Most also have higher order functions (very rare exceptions being languages like Excel, which can be said to be functional, but not higher order).

提交回复
热议问题