Lesson 3. Lambda functions and lexical scope

 

After reading lesson 3, you’ll be able to

  • Write lambda functions in Haskell
  • Use lambda functions for ad hoc function definitions
  • Understand lexical scope
  • Create scope with a lambda function

In this lesson, you’re going to continue your journey into understanding functional programming and Haskell by learning about one of the most foundational concepts in all of functional programming: the lambda function. On the surface, a lambda function—which is a function with no name—seems almost too simple to be interesting. But lambda functions provide incredible theoretical benefits as well as a surprising amount of real-world usefulness.

Consider this

You’re messing around in GHCi and want to quickly calculate the difference between the square of the sum of three values and the sum of the squares of three values: 4, 10, 22. You could write this out by hand:

GHCi> (4 + 10 + 22)^2 - (4^2 + 10^2 + 22^2)

But this makes it easy to have a typo that causes your expression to create an error. Additionally, it’s difficult to change these values if you want to edit this item from your GHCi command history (press the up arrow in GHCi to get the previous item). Is there a way to make this a bit cleaner without having to explicitly define a function?

3.1. Lambda functions

 

3.2. Writing your own where clause

 
 
 

3.3. From lambda to let: making your own variable variables!

 
 
 

3.4. Practical lambda functions and lexical scope

 
 
 

Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage