Difference list
In computer science, the term difference list refers to a data structure representing a list with an efficient O(1) concatenation operation and conversion to a linked list in time proportional to its length. Difference lists can be implemented using first-class functions or using unification. Whether a difference list is more efficient than other list representations depends on usage patterns. If an algorithm builds a list by concatenating smaller lists, which are themselves built by concatenating still smaller lists, then the usage of difference lists can improve performance by effectively "flattening" the list building computations.
Implementation using functions
A difference list f is a single-argument function append L, which when given a linked list X as argument, returns a linked list containing L prepended to X. Concatenation of difference lists is implemented as function composition. The contents may be retrieved using f [].[1]
This implementation is typically used in functional programming languages such as Haskell, although it could be used in imperative languages as well.
As functions, difference lists are a Cayley representation of lists as monoids, or more specifically their transformation monoid induced by left multiplication.
Examples of use are in the ShowS type in the Prelude of Haskell, and in Donald Bruce Stewart's difference list library for Haskell.[2]
Implementation using unification
Another implementation in the logic programming language Prolog uses unification variables.[3] A difference list is a pair OpenList-Hole, where the first element OpenList is a list containing an unbound unification variable (hole) and the second element Hole is a reference to the hole.
References
- ^ A Novel Representation of Lists and Its Application to the Function "Reverse" by John Hughes (1986)
- ^ Difference Lists in Haskell (programming language)
- ^ Open Lists and Difference Lists in Prolog Retrieved 2019-02-17
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.