Functional Programming Essentials

Table of Contents

What is functional programming?

Define functional programming.   drill functional_programming

:END: Functional programming (FP) is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

What are the core principles of functional programming?   drill functional_programming

  1. Immutability: Data is immutable, meaning that once a value is assigned, it cannot be changed. Instead of modifying existing data, new values are created.
  2. Pure functions: Functions produce the same output for the same input, with no side effects, such as modifying global variables or performing I/O operations.
  3. First-class and higher-order functions: Functions are treated as first-class citizens, which means they can be passed as arguments to other functions, returned as results from functions, and assigned to variables. Higher-order functions are functions that operate on or return other functions.
  4. Recursion: Loops are often replaced with recursion, a function that calls itself to solve smaller subproblems.

What are some of the advantages of functional programming?   drill functional_programming

  • Easier reasoning: Due to immutability and pure functions, programs are easier to understand, test, and debug.
  • Concurrency and parallelism: Without shared mutable state, programs are inherently safe for concurrent execution.
  • Modularity: Functions can be easily composed and reused, leading to more modular and maintainable code.

Functions

What is a pure function?   drill functional_programming

A pure function always produces the same output for the same input and has no side effects.

What is a higher-order function?   drill functional_programming

A higher-order function is a function that takes one or more functions as arguments and/or returns a function as its result.

What is currying?   drill functional_programming

Currying is a technique for transforming a function that takes multiple arguments into a sequence of functions that each take a single argument.

Immutability

What is immutability?   drill functional_programming

Immutability means that once a value is created, it cannot be modified.

How does immutability help in functional programming?   drill functional_programming

Immutability simplifies reasoning about program behavior, makes it easier to test and debug code, and allows for safer concurrency and parallelism.

How does immutability relate to persistent data structures?   drill functional_programming

Persistent data structures are immutable data structures that preserve their previous versions when modified. This allows efficient updates without changing the original data.

Recursion

What is recursion?   drill functional_programming

Recursion is a technique where a function calls itself to solve smaller subproblems.

What is a base case in recursion?   drill functional_programming

A base case is a condition that stops the recursion and returns a result.

What is tail recursion?   drill functional_programming

Tail recursion is a special form of recursion where the recursive call is the last operation in the function, making it more efficient than general recursion.

Functional Programming Concepts

What is a lambda function?   drill functional_programming

A lambda function (also known as an anonymous function) is a function defined without a name.

What is a closure?   drill functional_programming

A closure is a function that retains access to the variables in its lexical scope, even after the outer function has finished executing.

What is lazy evaluation?   drill functional_programming

Lazy evaluation (or call-by-need) is a strategy that delays the evaluation of an expression until its value is needed.

Common Functional Programming Paradigms

What is map?   drill functional_programming

The `map` function applies a function to each element of a collection and returns a new collection with the results.

What is filter?   drill functional_programming

The `filter` function selects elements from a collection that satisfy a predicate function.

What is reduce (or fold)?   drill functional_programming

The `reduce` function (also known as `fold`) combines the elements of a collection using a binary operation.

Functional Programming Languages

Name some popular functional programming languages.   drill functional_programming

Haskell, Clojure, Elixir, Elm, Scala, F#, and Erlang.

What are some distinguishing features of Haskell?   drill functional_programming

Haskell features strong static typing, lazy evaluation, pure functions, and a rich type system with algebraic data types and type classes.

How does functional programming differ from object-oriented programming?   drill functional_programming

Functional programming focuses on immutability, pure functions, and expressions, while object-oriented programming emphasizes mutable state, encapsulation, and objects with methods.

Author: Jason Walsh

j@wal.sh

Last Updated: 2024-10-30 16:43:54