Can lambda functions be recursive?

Recursive Lambda Functions in Python

Yes, lambda functions can be recursive in Python, but implementing recursion with lambdas requires specific techniques due to their anonymous and single-expression nature. While it's possible, it's generally more straightforward to use named functions for recursion. However, understanding how to create recursive lambdas can be an interesting exercise in functional programming.

Method 1: Assigning the Lambda to a Variable

The simplest way to create a recursive lambda is by assigning it to a variable. This allows the lambda to reference itself by name within its definition.

Example: Factorial Function

# Recursive lambda for factorial factorial = lambda n: 1 if n == 0 else n * factorial(n - 1) # Usage print(factorial(5)) # Output: 120

Explanation:

  • Assignment: The lambda function is assigned to the variable factorial.
  • Base Case: If n is 0, it returns 1.
  • Recursive Case: Otherwise, it returns n * factorial(n - 1), effectively calling itself.

Method 2: Using Default Arguments

Another approach involves using default arguments to pass the lambda function itself as a parameter. This method allows defining the lambda without assigning it beforehand.

Example: Factorial Function

# Recursive lambda using default argument factorial = (lambda f: lambda n: 1 if n == 0 else n * f(f, n - 1))( lambda f, n: 1 if n == 0 else n * f(f, n - 1) ) # Usage print(factorial(5)) # Output: 120

Explanation:

  • Outer Lambda (lambda f: ...): Takes a function f as an argument and returns a new lambda.
  • Inner Lambda (lambda f, n: ...): Defines the recursive logic, calling f(f, n - 1) to achieve recursion.
  • Immediate Invocation: The outer lambda is immediately invoked with the inner lambda as its argument, effectively tying the recursion.

Method 3: Using a Fixed-Point Combinator (Y Combinator)

For those interested in functional programming concepts, the Y combinator allows creating recursive lambda functions without explicitly naming them. While intellectually fascinating, it's generally not recommended for practical use in Python due to readability and complexity concerns.

Example: Factorial Function

# Y Combinator implementation for recursion Y = (lambda f: (lambda x: f(lambda *args: x(x)(*args)))(lambda x: f(lambda *args: x(x)(*args)))) # Factorial using Y combinator factorial = Y(lambda f: lambda n: 1 if n == 0 else n * f(n - 1)) # Usage print(factorial(5)) # Output: 120

Explanation:

  • Y Combinator (Y): A higher-order function that enables recursion in anonymous functions.
  • Factorial Definition: Uses the Y combinator to define a recursive lambda for calculating factorial.
  • Execution: Calls factorial(5) to compute the factorial of 5.

Caution: The Y combinator is more of a theoretical concept and can make the code hard to read and maintain.

Practical Considerations

  1. Readability: Recursive lambdas can become hard to read and maintain. Using named functions is typically clearer and more idiomatic in Python.

    def factorial(n): return 1 if n == 0 else n * factorial(n - 1)
  2. Debugging: Debugging recursive lambdas can be more challenging compared to named recursive functions. Tracebacks may be less informative.

  3. Performance: There's no significant performance difference between recursive lambdas and named recursive functions. However, deeply recursive calls can lead to stack overflow errors, regardless of how recursion is implemented.

  4. Use Cases: Recursive lambdas are more of a curiosity or for specific functional programming paradigms rather than everyday use in Python.

Example: Fibonacci Sequence with Recursive Lambda

Here's how you can implement a recursive lambda to compute Fibonacci numbers by assigning it to a variable:

# Recursive lambda for Fibonacci fibonacci = lambda n: n if n <= 1 else fibonacci(n - 1) + fibonacci(n - 2) # Usage for i in range(10): print(f"Fib({i}) = {fibonacci(i)}")

Output:

Fib(0) = 0
Fib(1) = 1
Fib(2) = 1
Fib(3) = 2
Fib(4) = 3
Fib(5) = 5
Fib(6) = 8
Fib(7) = 13
Fib(8) = 21
Fib(9) = 34

Conclusion

While it's entirely possible to implement recursive lambda functions in Python, the approaches often involve additional complexity that can hinder code clarity and maintainability. For most practical purposes, especially in production code, using named recursive functions is recommended due to their simplicity and readability. However, understanding how to create recursive lambdas can deepen your grasp of functional programming concepts and the flexibility of Python's lambda expressions.

TAGS
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What skill is Microsoft Office?
What Is the Revolut Interview Process Like? (Round by Round)
Revolut runs four to five stages: an online assessment, a recruiter screen, live coding, a take-home project with review, and a final values round.
Which subject is best for a software engineer?
What are HR challenges in Apple?
What is the symbol for OpenAI?
What type of coding questions will be asked in an interview?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.