Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Cons
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Functional implementation== Since Lisp has [[first-class function]]s, all data structures, including cons cells, can be implemented using functions. For example, in [[Scheme (programming language)|Scheme]]: <syntaxhighlight lang="scheme"> (define (cons x y) (lambda (m) (m x y))) (define (car z) (z (lambda (p q) p))) (define (cdr z) (z (lambda (p q) q))) </syntaxhighlight> This technique is known as [[Church encoding#Church pairs|Church encoding]]. It re-implements the ''cons'', ''car'', and ''cdr'' operations, using a function as the "cons cell". Church encoding is a usual way of defining data structures in pure [[lambda calculus]], an abstract, theoretical model of computation that is closely related to Scheme. This implementation, while academically interesting, is impractical because it renders cons cells indistinguishable from any other Scheme procedure, as well as introduces unnecessary computational inefficiencies. However, the same kind of encoding can be used for more complex algebraic data types with variants, where it may even turn out to be more efficient than other kinds of encoding.<ref>{{cite web |url=http://www.st.cs.ru.nl/papers/2006/janj2006-TFP06-EfficientInterpretation.pdf |title=Efficient Interpretation by Transforming Data Types and Patterns to Functions |accessdate=2009-03-01 |url-status=dead |archiveurl=https://web.archive.org/web/20100331083602/http://www.st.cs.ru.nl/papers/2006/janj2006-TFP06-EfficientInterpretation.pdf |archivedate=2010-03-31 }}</ref> This encoding also has the advantage of being implementable in a statically typed language that doesn't have variants, such as [[Java (programming language)|Java]], using interfaces instead of lambdas.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)