Lambda: Reclaiming An Old Concept

Feb. 14, 2008
I think I’m going to go back to programming in APL and Lisp. The concepts and methodologies of decades ago are being reborn in today’s languages, like Microsoft’s C# and Sun’s Java. Of course, languages like C#, C++, and Java were design

I think I’m going to go back to programming in APL and Lisp. The concepts and methodologies of decades ago are being reborn in today’s languages, like Microsoft’s C# and Sun’s Java. Of course, languages like C#, C++, and Java were designed to take advantage of other object-oriented precursors such as Simula and SmallTalk.

However, in trying to minimize the complexity of the new languages, designers left out many features. Some of these features found their way back into the new languages through runtime support or extensions that can be added within the language’s normal extension mechanisms.

Yet in many cases, a fundamental change is necessary to the syntax or semantics. These types of changes aren’t taken lightly, since they can break existing applications. Still, enhancing a language this way can have a major impact on developers in areas such as efficiency and code quality.

I Say Tomato, You Say... Lisp is built around lists and lambda expressions. Lambda expressions are closures that include any bound variables at the time the closure is created. Take this short interactive Lisp session:

> (define timesN (lambda (N)

(lambda (x) (* x N)))

> ((timesN 4) 3)

12

The timesN variable is assigned a lambda expression that takes a single parameter. It returns another lambda expression that also has a single parameter. The difference is that the inner unnamed lambda expression references the N parameter.

The first parameter of a list being evaluated in Lisp is the function to be executed. This works recursively so the second statement executed in the example evaluates (timesN 4), which returns a lambda expression that will multiply its parameter by four. In this example, it is applied to parameter 3 with a result of 12. As shown, lambda expressions are first-class values that can be stored and passed as parameters.

I Say Delegate, You Say... C# 2.0 introduced the concept of anonymous methods, which allowed the creation of method definitions without names that are common in the callback approach commonly used throughout Microsoft’s Windows operating system. This was more shorthand than a radical semantic enhancement because the same thing could be done, albeit with more textual decorations.

C# 3.0 adds explicitly or implicitly typed lambda expressions to the mix. In the latter case, the types are inferred from the surrounding code context. The syntax is simpler as well.

(int x) => return x * 4;

Similar closure functionality is being considered for Java 7.

From a C# or Java developer’s point of view, enhancements like lambda expressions are favorable additions. Their implementation may lack the elegance and consistency of Lisp since the new features are tacked on rather than part of the original language definition. But, elegant or not, they will get a lot of use.

So what about C and C++, since they’re used in the bulk of embedded applications? Callback functions are already the norm, but two issues crop up: syntax and garbage collection.

Syntax is arbitrary since there is no standard. C macros and C++ class definitions can hide this, though an implementation may have to deal with ugly issues such as casting void* pointers.

Memory allocation is more problematic since the memory must be explicitly deallocated. C++ has the edge over C here. With C++, though, developers still need to deallocate the closure. Either way, the solution isn’t very elegant.

Microsoft • www.microsoft.com
Sun • www.sun.com

About the Author

William G. Wong | Senior Content Director - Electronic Design and Microwaves & RF

I am Editor of Electronic Design focusing on embedded, software, and systems. As Senior Content Director, I also manage Microwaves & RF and I work with a great team of editors to provide engineers, programmers, developers and technical managers with interesting and useful articles and videos on a regular basis. Check out our free newsletters to see the latest content.

You can send press releases for new products for possible coverage on the website. I am also interested in receiving contributed articles for publishing on our website. Use our template and send to me along with a signed release form. 

Check out my blog, AltEmbedded on Electronic Design, as well as his latest articles on this site that are listed below. 

You can visit my social media via these links:

I earned a Bachelor of Electrical Engineering at the Georgia Institute of Technology and a Masters in Computer Science from Rutgers University. I still do a bit of programming using everything from C and C++ to Rust and Ada/SPARK. I do a bit of PHP programming for Drupal websites. I have posted a few Drupal modules.  

I still get a hand on software and electronic hardware. Some of this can be found on our Kit Close-Up video series. You can also see me on many of our TechXchange Talk videos. I am interested in a range of projects from robotics to artificial intelligence. 

Sponsored Recommendations

Comments

To join the conversation, and become an exclusive member of Electronic Design, create an account today!