Symbol (programming): Difference between revisions

Content deleted Content added
Support: The Scheme one stays constant.
m clean up using AWB (8459)
Line 32:
| [[SML/NJ]] || Atom.atom ||
|}
 
=== Lisp ===
 
Line 39 ⟶ 40:
 
In [[Common Lisp]] symbols may use any characters, including whitespace, such as spaces and newlines. If a symbol contains a whitespace character it needs to be written as |this is a symbol|. Symbols can be used as identifiers for any kind of named programming constructs: variables, functions, macros, classes, types, goto tags and more.
Symbols can be interned in a package.<ref>[http://www.lispworks.com/documentation/HyperSpec/Body/t_pkg.htm#package Common Lisp HyperSpec, system class Package]</ref>. Keyword symbols are self-evaluating<ref>[[Peter Norvig]]: ''Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp'', Morgan Kaufmann, 1991, ISBN 1-55860-191-0, [http://norvig.com/paip.html Web]</ref> and interned in the package named KEYWORD.
 
==== Examples ====
Line 71 ⟶ 72:
In [[Prolog (programming language)|Prolog]], symbols (or atoms) are the primary primitive data types, similar to numbers.<ref name=Bratko2001>{{Cite book | last1 = Bratko | first1 = Ivan | title = Prolog programming for artificial intelligence | year = 2001 | publisher = Addison Wesley | ___location = Harlow, England ; New York | isbn = 0-201-40375-7 | pages = }}</ref> The exact notation may differ in different [[Prolog (programming language)|Prolog]]'s dialects. However, it is always quite simple (no quotations or special beginning characters are necessary).
 
Contrary to other languages, it is possible to give symbols some <i>''meaning</i>'' by creating some [[Prolog (computer language)|Prolog]]'s facts and/or rules.
 
====Examples====
The following example demonstrates two facts (describing what <i>''father</i>'' is) and one rule (describing the <i>''meaining</i>'' of <i>''sibling</i>''). These three sentences use symbols (father, zeus, hermes, perseus and sibling) and some abstract variables (X, Y and Z). The <i>''mother</i>'' relationship has been omitted for clarity.
<source lang="prolog">
father (zeus, hermes).
Line 82 ⟶ 84:
 
===Ruby===
In [[Ruby (programming language)|Ruby]], symbols can be created with a literal form, or by converting a string.<ref name=pickaxe />.
They can be used as an identifier or an interned string.<ref name=rubysymbol>{{cite web|last=Kidd|first=Eric|title=13 Ways of Looking at a Ruby Symbol|url=http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#9|work=Random Hacks|accessdate=10 July 2011}}</ref>. Two symbols with the same contents will always refer to the same object.<ref>http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UI</ref>
It is considered a [[best practice]] to use symbols as keys to an [[associative array]] in Ruby.<ref name=rubysymbol /><ref name=wrongreason>{{cite web|title=Using Symbols for the Wrong Reason|url=http://microjet.ath.cx/WebWiki/2005.12.27_UsingSymbolsForTheWrongReason.html|work=Gnomic Notes}}</ref>.
 
====Examples====
The following is a simple example of a symbol literal in Ruby<ref name=pickaxe />:
Line 149 ⟶ 152:
[[Category:Articles with example Ruby code]]
[[Category:Programming constructs]]
 
 
{{compu-prog-stub}}