Saltar al contenido ForoHackers

Functional programming: start here

Programación funcional
#535 · Ophuscado · · Denunciar

Functional programming builds software from functions that take values and return values, avoiding hidden state and mutation where it can. Programs become easier to test, reason about and run in parallel. The ideas have spread to almost every mainstream language, so they are worth learning even if you never write in a purely functional one.

Key concepts

  • Pure functions: the same input always gives the same output, with no side effects.
  • Immutability: data is not changed in place; new values are built from old ones.
  • Higher-order functions: functions as values, passed to and returned from other functions, such as map, filter and fold.
  • Algebraic data types and pattern matching: model data as sums and products, and take it apart by shape.
  • Recursion and laziness: loops as recursive calls, and values computed only when needed.
  • Effects as values: monads and similar structures describe input, output and failure explicitly.

Vocabulary

  • Closure: a function together with the variables it captured.
  • Currying: turning a function of several arguments into a chain of one-argument functions.
  • Referential transparency: an expression can be replaced by its value without changing the program.
  • Tail call: a call in final position, which can reuse the current stack frame.

What to study, in order

  1. Rewrite loops in your usual language with map, filter and fold, and avoid mutation for a week.
  2. Learn one functional language well enough to write a small program in it.
  3. Model a problem with algebraic data types and pattern matching.
  4. Implement your own map, fold and a simple parser combinator.
  5. Understand how a monad sequences effects, using Option or Result before IO.

Related tags

#haskell #ocaml #lisp #type-theory

Inicia sesión o únete para responder.