Can somebody explain me lambda expressions & what they can be used for. I have googled for it & have a rough idea. most of the examples give c# code. How about lambda expressions in plain old C...?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Why wrapping a function into a lambda potentially
- Index of single bit in long integer (in C) [duplic
There are actually two things called "lambda expressions", which are rather loosely related:
Lambda expressions are fundamental part of lambda calculus and are closely related to functional programming
In imperative languages, lambda expressions are usually synonyms for anonymous methods. In C#, for example you can pass lambda expression (ie. an expression itself, not just its result) as an argument:
C#:
Having said that, C does not support anonymous methods. You can, however, use function pointers to achieve similar results:
Look here on the MSDN
C doesn't support lamba expressions...if you know perl, I highly recommend the book "higher order perl" which will give you a great introduction to all sorts of functional programming techniques in a familiar (if you know perl) and practical setting.
el.pescado's answer is right but the example that he provides has an easy work around, using a function pointer. Many uses of lambda functions can't be solved with c's function pointers.
Say you write these functions in c:
Those are all pretty easy to understand. Now assume that you want to write a function that takes y as input and returns a pointer to the function Multiply_y():
Where "Multiply_y" is a dynamically created function of the form of Multiply_1, Multiply_2, etc. Languages that have first-class lambda functions can do that.