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
Assignment (computer science)
(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!
==Single assignment== {{See also|Static single-assignment form}} Any assignment that changes an existing value (e.g. <code>x := x + 1</code>) is disallowed in [[purely functional programming|purely functional]] languages.<ref name="Bruce Tate"/> In [[functional programming]], assignment is discouraged in favor of single assignment, more commonly known as ''initialization''. Single assignment is an example of [[name binding]] and differs from assignment as described in this article in that it can only be done once, usually when the variable is created; no subsequent reassignment is allowed. An evaluation of an expression does not have a [[side effect (computer science)|side effect]] if it does not change an observable state of the machine,<ref name="Mitchell2003">{{cite book |last=Mitchell |first=John C. |author-link=John C. Mitchell |title=Concepts in programming languages |url=https://books.google.com/books?id=7Uh8XGfJbEIC&pg=PA23|access-date=3 January 2011 |year=2003 |publisher=Cambridge University Press |isbn=978-0-521-78098-8 |page=23}}</ref> other than producing the result, and always produces same value for the same input.<ref name="Bruce Tate"/> Imperative assignment can introduce side effects while destroying and making the old value unavailable while substituting it with a new one,<ref name="csci210">{{cite web |title=Imperative Programming Languages (IPL) |url=http://www.seas.gwu.edu/~bell/csci210/lectures/imperative_languages.pdf |archive-url=https://web.archive.org/web/20110716201414/http://www.seas.gwu.edu/~bell/csci210/lectures/imperative_languages.pdf |archive-date=2011-07-16 |access-date=20 April 2018 |website=gwu.edu}}</ref> and is referred to as ''destructive assignment'' for that reason in [[LISP]] and [[functional programming]], similar to [[destructive update|destructive updating]]. Single assignment is the only form of assignment available in purely functional languages, such as [[Haskell (programming language)|Haskell]], which do not have variables in the sense of imperative programming languages<ref name="Bruce Tate"/> but rather named constant values possibly of compound nature, with their elements progressively defined ''on-demand'', for the [[lazy evaluation|lazy]] languages. Purely functional languages can provide an opportunity for [[parallel computing|computation to be performed in parallel]], avoiding the [[von Neumann bottleneck]] of sequential one step at a time execution, since values are independent of each other.<ref name="Mitchell2003.1">{{cite book |author=John C. Mitchell |title=Concepts in programming languages |url=https://books.google.com/books?id=7Uh8XGfJbEIC&pg=PA81|access-date=3 January 2011 |year=2003 |publisher=Cambridge University Press |isbn=978-0-521-78098-8 |pages=81β82}}</ref> Impure functional languages provide both single assignment as well as true assignment (though true assignment is typically used with less frequency than in imperative programming languages). For example, in Scheme, both single assignment (with <code>let</code>) and true assignment (with <code>set!</code>) can be used on all variables, and specialized primitives are provided for destructive update inside lists, vectors, strings, etc. In OCaml, only single assignment is allowed for variables, via the <code>let ''name'' = ''value''</code> syntax; however destructive update can be used on elements of arrays and strings with separate <code><-</code> operator, as well as on fields of records and objects that have been explicitly declared [[mutable]] (meaning capable of being changed after their initial declaration) by the programmer. Functional programming languages that use single assignment include [[Clojure]] (for data structures, not vars), [[Erlang (programming language)|Erlang]] (it accepts multiple assignment if the values are equal, in contrast to Haskell), [[F Sharp (programming language)|F#]], [[Haskell (programming language)|Haskell]], [[JavaScript]] (for constants), Lava, [[OCaml]], [[Oz (programming language)|Oz]] (for dataflow variables, not cells), [[Racket (programming language)|Racket]] (for some data structures like lists, not symbols), [[SASL (programming language)|SASL]], [[Scala (programming language)|Scala]] (for vals), [[SISAL]], [[Standard ML]]. Non-[[backtracking]] [[Prolog]] code can be considered ''explicit'' single-assignment, explicit in a sense that its (named) variables can be in explicitly unassigned state, or be set exactly once. In Haskell, by contrast, there can be no unassigned variables, and every variable can be thought of as being implicitly set, when it is created, to its value (or rather to a computational object that will produce its value ''on demand'').
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)