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
Factor (programming language)
(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!
==Description== Factor is a [[dynamically typed]], [[functional programming|functional]] and [[object-oriented programming|object-oriented]] [[programming language]]. Code is structured around small procedures, called words. In typical code, these are 1β3 lines long, and a procedure more than 7 lines long is very rare. Something that would idiomatically be expressed with one procedure in another programming language would be written as several words in Factor.<ref name="dls">{{cite journal |last1=Pestov |first1=Sviatoslav |last2=Ehrenberg |first2=Daniel |year=2010 |title=Factor: a dynamic stack-based programming language |journal=ACM SIGPLAN Notices |volume=45 |issue=12 |pages=43β58 |publisher=ACM |doi=10.1145/1899661.1869637 }}</ref> Each word takes a fixed number of arguments and has a fixed number of return values. Arguments to words are passed on a [[stack (data structure)|data stack]], using [[reverse Polish notation]]. The stack is used just to organize calls to words, and not as a data structure. The stack in Factor is used in a similar way to the stack in [[Forth (programming language)|Forth]]; for this, they are both considered [[stack-oriented programming language|stack languages]]. For example, below is a snippet of code that prints out "hello world" to the current output stream: "hello world" print <code>print</code> is a word in the <code>io</code> vocabulary that takes a string from the stack and returns nothing. It prints the string to the current output stream (by default, the terminal or the graphical listener).<ref name = "dls" /> The [[factorial|factorial function]] <math>n!</math> can be implemented in Factor in the following way: <syntaxhighlight lang="factor"> : factorial ( n -- n! ) dup 1 > [ [1..b] product ] [ drop 1 ] if ; </syntaxhighlight> Not all data has to be passed around only with the stack. [[Lexical scoping|Lexically scoped]] local variables let one store and access [[temporary variable|temporaries]] used within a procedure. [[Dynamic scoping|Dynamically scoped]] variables are used to pass things between procedure calls without using the stack. For example, the current input and output streams are stored in dynamically scoped variables.<ref name="dls" /> Factor emphasizes flexibility and the ability to extend the language.<ref name = "dls" /> There is a system for macros, as well as for arbitrary extension of Factor syntax. Factor's syntax is often extended to allow for new types of word definitions and new types of [[literal (computer science)|literal]]s for data structures. It is also used in the [[XML]] library to provide literal syntax for generating XML. For example, the following word takes a string and produces an XML document object which is an HTML document emphasizing the string: <syntaxhighlight lang="factor"> : make-html ( string -- xml ) dup <XML <html> <head><title><-></title></head> <body><h1><-></h1></body> </html> XML> ; </syntaxhighlight> The word <code>dup</code> duplicates the top item on the stack. The <code><-></code> stands for filling in that part of the XML document with an item from the stack.
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)