What is a lambda (function)?

后端 未结 23 2795
太阳男子
太阳男子 2020-11-22 04:47

For a person without a comp-sci background, what is a lambda in the world of Computer Science?

23条回答
  •  情书的邮戳
    2020-11-22 05:24

    The lambda calculus is a consistent mathematical theory of substitution. In school mathematics one sees for example x+y=5 paired with x−y=1. Along with ways to manipulate individual equations it's also possible to put the information from these two together, provided cross-equation substitutions are done logically. Lambda calculus codifies the correct way to do these substitutions.

    Given that y = x−1 is a valid rearrangement of the second equation, this: λ y = x−1 means a function substituting the symbols x−1 for the symbol y. Now imagine applying λ y to each term in the first equation. If a term is y then perform the substitution; otherwise do nothing. If you do this out on paper you'll see how applying that λ y will make the first equation solvable.

    That's an answer without any computer science or programming.

    The simplest programming example I can think of comes from http://en.wikipedia.org/wiki/Joy_(programming_language)#How_it_works:

    here is how the square function might be defined in an imperative programming language (C):

    int square(int x)
    {
        return x * x;
    }
    

    The variable x is a formal parameter which is replaced by the actual value to be squared when the function is called. In a functional language (Scheme) the same function would be defined:

    (define square
      (lambda (x) 
        (* x x)))
    

    This is different in many ways, but it still uses the formal parameter x in the same way.


    Added: http://imgur.com/a/XBHub

提交回复
热议问题