Lambda expressions, also known as anonymous functions, are a powerful feature in Haskell that allow you to create functions without naming them. They are particularly useful for short, throwaway functions that you don't need to reuse.

Key Concepts

  1. Syntax: The basic syntax for a lambda expression in Haskell is \args -> expression.
  2. Usage: Lambda expressions are often used in higher-order functions like map, filter, and fold.
  3. Scope: Variables defined within a lambda expression are local to that expression.

Syntax

The general form of a lambda expression is:

\arg1 arg2 ... argN -> expression
  • \ is the lambda symbol.
  • arg1 arg2 ... argN are the arguments.
  • expression is the body of the function.

Example

Here is a simple example of a lambda expression that adds two numbers:

add = \x y -> x + y

You can use this lambda expression just like any other function:

main = print (add 3 5)  -- Output: 8

Practical Examples

Example 1: Using Lambda with map

The map function applies a given function to each element of a list. You can use a lambda expression to define this function on the fly.

main = print (map (\x -> x * 2) [1, 2, 3, 4])  -- Output: [2, 4, 6, 8]

Example 2: Using Lambda with filter

The filter function selects elements from a list that satisfy a given predicate. A lambda expression can be used to define this predicate.

main = print (filter (\x -> x > 2) [1, 2, 3, 4])  -- Output: [3, 4]

Example 3: Using Lambda with foldr

The foldr function reduces a list to a single value using a binary function and a starting value. A lambda expression can define this binary function.

main = print (foldr (\x acc -> x + acc) 0 [1, 2, 3, 4])  -- Output: 10

Exercises

Exercise 1: Double the Elements

Write a lambda expression to double each element in a list.

-- Your code here
main = print (map (\x -> x * 2) [1, 2, 3, 4])

Exercise 2: Filter Even Numbers

Write a lambda expression to filter out even numbers from a list.

-- Your code here
main = print (filter (\x -> x `mod` 2 == 0) [1, 2, 3, 4, 5, 6])

Exercise 3: Sum of Squares

Write a lambda expression to calculate the sum of squares of a list of numbers using foldr.

-- Your code here
main = print (foldr (\x acc -> x * x + acc) 0 [1, 2, 3, 4])

Solutions

Solution 1: Double the Elements

main = print (map (\x -> x * 2) [1, 2, 3, 4])  -- Output: [2, 4, 6, 8]

Solution 2: Filter Even Numbers

main = print (filter (\x -> x `mod` 2 == 0) [1, 2, 3, 4, 5, 6])  -- Output: [2, 4, 6]

Solution 3: Sum of Squares

main = print (foldr (\x acc -> x * x + acc) 0 [1, 2, 3, 4])  -- Output: 30

Common Mistakes and Tips

  • Forgetting the backslash (\): Always start a lambda expression with \.
  • Misplacing the arrow (->): Ensure the arrow is placed correctly between the arguments and the expression.
  • Scope issues: Remember that variables inside a lambda expression are local to that expression.

Conclusion

Lambda expressions are a concise way to define functions on the fly. They are particularly useful in higher-order functions like map, filter, and fold. By mastering lambda expressions, you can write more flexible and concise Haskell code. In the next section, we will explore Map, Filter, and Fold in more detail, building on the concepts introduced here.

© Copyright 2024. All rights reserved