Content deleted Content added
Fatalasian (talk | contribs) No edit summary |
→Definition: (m) clarify |
||
(39 intermediate revisions by 25 users not shown) | |||
Line 1:
{{Short description|Data structure implementable in purely functional languages}}
{{
In [[computer science]], a '''purely functional data structure''' is a [[data structure]] that can be directly implemented in a [[purely functional language]]. The main difference between an arbitrary data structure and a purely functional one is that
==Definition==
Formally, a ''purely functional data structure'' is a data structure which can be implemented in a [[purely functional language]], such as [[Haskell (programming language)|Haskell]]. In practice, it means that the data structures must be built using only persistent data structures such as tuples, [[sum type]]s, [[product type]]s, and basic types such as integers, characters, strings. Such a data structure is necessarily persistent. However, not all persistent data structures are purely functional.{{r|Okasaki-book|p=16}} For example, a [[persistent array]] is a data-structure which is persistent and which is implemented using an array, thus is not purely functional.{{citation needed|date=December 2018}}
In the book ''Purely functional data structures'', Okasaki compares destructive updates to master chef's knives.{{r|Okasaki-book|p=2}} Destructive updates cannot be undone, and thus they should not be used unless it is certain that the previous value is not required anymore. However, destructive updates can also allow efficiency that can not be obtained using other techniques. For example, a data structure using an array and destructive updates may be replaced by a similar data structure where the array is replaced by a [[Map (computer science)|map]], a random access list, or a [[balanced tree]], which admits a purely functional implementation. But the access cost may increase from constant time to [[logarithmic time]].{{citation needed|date=December 2018}}
==Ensuring that a data structure is purely functional==
A data structure is never inherently functional. For example, a stack can be implemented as a [[singly-linked list]]. This implementation is purely functional as long as the only operations on the stack return a new stack without altering the old stack. However, if the language is not
In order to ensure that a data structure is used in a purely functional way in an impure functional language, [[modular programming|modules]] or [[class (computer programming)|classes]] can be used to ensure manipulation via authorized functions only.{{citation needed|date=December 2018}}▼
==Using purely functional data structures==
One of the central challenges in adapting existing code to use purely functional data structures lies in the fact that mutable data
structures provide "hidden outputs" for functions that use them. Rewriting these functions to use purely functional data structures
requires adding these data structures as explicit outputs.
For instance, consider a function that accepts a mutable list, removes the first element from the list, and returns that element. In a purely functional setting, removing an element from the list produces a new and shorter list, but does not update the original one. In order to be useful, therefore, a purely functional version of this function is likely to have to return the new list along with the removed element. In the most general case, a program converted in this way must return the "state" or "store" of the program as an additional result from every function call. Such a program is said to be written in [[store-passing style]].
▲In order to ensure that a data structure is used in a purely functional way in an impure functional language, [[modular programming|modules]] or [[class (computer programming)|classes]] can be used to ensure manipulation via authorized functions only.
==Examples==
Here is a list of
* Stack (first in, last out) implemented as a [[singly linked list]],
* Queue, implemented as a [[real-time queue]],
* Double-ended queue, implemented as a [[real-time deque|real-time double-ended queue]],
* [[Set (abstract data type)|(Multi)set]] of ordered elements and [[map (computer science)|map]] indexed by ordered keys, implemented as a [[red–black tree]], or more generally by a [[search tree]],
* [[Priority queue]], implemented as a [[Brodal queue]]
* Random access list, implemented as a skew-binary random access list
* [[Hash consing]]
* [[Zipper (data structure)]]
==Design and implementation==
In his book ''Purely Functional Data Structures'', computer scientist [[Chris Okasaki]] describes techniques used to design and implement purely functional data structures, a small subset of which are summarized below.
===Laziness and memoization===
One of the key tools in building efficient, purely functional data structures is
===Amortized analysis and scheduling===
Some data structures, even
In general, having inefficient operations is not acceptable for persistent data structures, because this
In order to avoid those problems, some data structures allow for the inefficient operation to be
====Example: queue====
==See
* [[Persistent data structure]]
==References==
{{reflist}}
==External links==
*[
*[
*[
*[http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-854j-advanced-algorithms-fall-2005/lecture-notes/persistent.pdf Persistent Data Structures] from the [[MIT OpenCourseWare]] course [http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-854j-advanced-algorithms-fall-2005 Advanced Algorithms]
*[
[[Category:Functional programming]]▼
[[Category:Functional data structures]]
▲[[Category:Functional programming]]
|