Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Purely functional programming

From Wikipedia, the free encyclopedia
Programming paradigm entirely based on functions

Incomputer science,purely functional programming usually designates aprogramming paradigm—a style of building the structure and elements of computer programs—that treats allcomputation as the evaluation ofmathematical functions.

Program state andmutable objects are usually modeled withtemporal logic, as explicit variables that represent the program state at each step of a program execution: a variable state is passed as aninput parameter of a state-transforming function, which returns the updated state as part of its return value. This style handles state changes without losing thereferential transparency of the program expressions.

Purely functional programming consists of ensuring that functions, inside thefunctional paradigm, will only depend on their arguments, regardless of any global or local state. A pure functional subroutine only has visibility of changes of state represented by state variables included in its scope.

Difference between pure and impure functional programming

[edit]

The exact difference between pure and impure functional programming is a matter of controversy. Sabry's proposed definition of purity is that all commonevaluation strategies (call-by-name, call-by-value, and call-by-need) produce the same result, ignoring strategies that error or diverge.[1]

A program is usually said to be functional when it uses some concepts offunctional programming, such asfirst-class functions andhigher-order functions.[2] However, a first-class function need not be purely functional, as it may use techniques from theimperative paradigm, such asarrays or input/outputmethods that use mutable cells, which update their state as side effects. In fact, the earliest programming languages cited as being functional,IPL andLisp,[3][4] are both "impure" functional languages by Sabry's definition.

Properties of purely functional programming

[edit]

Strict versus non-strict evaluation

[edit]
Main article:Evaluation strategy

Eachevaluation strategy which ends on a purely functional program returns the same result. In particular, it ensures that the programmer does not have to consider in which order programs are evaluated, sinceeager evaluation will return the same result aslazy evaluation. However, it is still possible that an eager evaluation may not terminate while the lazy evaluation of the same program halts.An advantage of this is that lazy evaluation can be implemented much more easily; as all expressions will return the same result at any moment (regardless of program state), their evaluation can be delayed as much as necessary.

Parallel computing

[edit]

In a purely functional language, the only dependencies between computations are data dependencies, and computations are deterministic. Therefore, to program in parallel, the programmer need only specify the pieces that should be computed in parallel, and the runtime can handle all other details such as distributing tasks to processors, managing synchronization and communication, and collecting garbage in parallel. This style of programming avoids common issues such as race conditions and deadlocks, but has less control than an imperative language.[5]

To ensure a speedup, the granularity of tasks must be carefully chosen to be neither too big nor too small. In theory, it is possible to use runtime profiling and compile-time analysis to judge whether introducing parallelism will speed up the program, and thus automatically parallelize purely functional programs. In practice, this has not been terribly successful, and fully automatic parallelization is not practical.[5]

Data structures

[edit]
Main article:Purely functional data structure

Purely functional data structures arepersistent. Persistency is required for functional programming; without it, the same computation could return different results. Functional programming may use persistent non-purely functionaldata structures, while those data structures may not be used in purely functional programs.

Purely functionaldata structures are often represented in a different way than theirimperative counterparts.[6] For example,array with constant-time access and update is a basic component of most imperative languages and many imperative data-structures, such ashash table andbinary heap, are based on arrays. Arrays can be replaced bymap orrandom access list, which admits purely functional implementation, but the access and update time islogarithmic. Therefore, purely functional data structures can be used in languages which are non-functional, but they may not be the most efficient tool available, especially if persistency is not required.

In general, conversion of an imperative program to a purely functional one also requires ensuring that the formerly-mutable structures are now explicitly returned from functions that update them, a program structure calledstore-passing style.

Purely functional language

[edit]
For a more comprehensive list, seeList of programming languages by type § Pure.

A purely functional language is a language which only admits purely functional programming. Purely functional programs can however be written in languages which are not purely functional.

References

[edit]
  1. ^Sabry, Amr (January 1993). "What is a purely functional language?".Journal of Functional Programming.8 (1):1–22.CiteSeerX 10.1.1.27.7800.doi:10.1017/S0956796897002943.S2CID 30595712.
  2. ^Atencio, Luis (18 June 2016).Functional Programming in Javascript. Manning Publications.ISBN 978-1617292828.
  3. ^The memoir ofHerbert A. Simon (1991),Models of My Life pp.189-190ISBN 0-465-04640-1 claims that he, Al Newell, and Cliff Shaw are "commonly adjudged to be the parents of [the] artificial intelligence [field]", for writingLogic Theorist, a program which proved theorems fromPrincipia Mathematica automatically. In order to accomplish this, they had to invent a language and a paradigm which, which viewed retrospectively, embeds functional programming.
  4. ^McCarthy, John (June 1978)."History of LISP".The first ACM SIGPLAN conference on History of programming languages - HOPL-1. pp. 217–223.doi:10.1145/800025.808387.
  5. ^abMarlow, Simon (18 June 2013).Parallel and Concurrent Programming in Haskell: Techniques for Multicore and Multithreaded Programming. O'Reilly Media. pp. 5–6.ISBN 978-1449335946.
  6. ^Purely functional data structures byChris Okasaki,Cambridge University Press, 1998,ISBN 0-521-66350-4
Imperative
Structured
Object-oriented
Declarative
Functional
Dataflow
Logic
Domain-
specific
language

(DSL)
Concurrent,
parallel
Metaprogramming
Separation
of concerns
Comparisons/Lists
Level
Generation
Retrieved from "https://en.wikipedia.org/w/index.php?title=Purely_functional_programming&oldid=1294163800"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp