<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ElasticDog.com &#187; Factor</title>
	<atom:link href="http://elasticdog.com/category/development/factor/feed/" rel="self" type="application/rss+xml" />
	<link>http://elasticdog.com</link>
	<description>Imagine Something Clever</description>
	<lastBuildDate>Thu, 04 Dec 2008 07:09:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Beginning Factor &#8211; Shufflers &amp; Combinators</title>
		<link>http://elasticdog.com/2008/12/beginning-factor-shufflers-and-combinators/</link>
		<comments>http://elasticdog.com/2008/12/beginning-factor-shufflers-and-combinators/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 05:59:35 +0000</pubDate>
		<dc:creator>Aaron Schaefer</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Factor]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://elasticdog.com/?p=160</guid>
		<description><![CDATA[In the previous installment of Beginning Factor, we discussed some of the attributes of stack-based languages and the syntax for defining words in Factor. This time around, I&#8217;d like to introduce stack shufflers, quotations &#038; combinators, and touch on some more basic data types and their properties.
Up until now, we&#8217;ve essentially been using Factor as [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://elasticdog.com/2008/11/beginning-factor-introduction/">previous installment</a> of Beginning Factor, we discussed some of the attributes of stack-based languages and the syntax for defining words in Factor. This time around, I&#8217;d like to introduce stack shufflers, quotations &#038; combinators, and touch on some more basic data types and their properties.</p>
<p>Up until now, we&#8217;ve essentially been using Factor as an overqualified <abbr title="Reverse Polish Notation">RPN</abbr> calculator. I just wanted to make sure that you don&#8217;t underestimate Factor because of these particular examples; Factor is an extremely capable and modern language that can be used for everything from web applications, to game development, to complex text parsing, and so on. I&#8217;m purposefully using over-simplified examples as a means to demonstrate specific points about the language. Stick with me, and I assure you the examples will gradually get more expressive.</p>
<h2 id="shufflers">Stack Shufflers</h2>
<p>Because of Factor&#8217;s stack-based nature, you sometimes need the ability to rearrange or copy items in the stack to ensure that they are in the correct position for future use. The way to go about this is a group of words called &#8220;stack shufflers&#8221;.</p>
<p>Much like the name implies, stack shufflers are merely words that change the order (or number) of the items on the top of the stack. It is said that shuffle words &#8220;control the flow of data between words that perform actions.&#8221; In fact, you already know one stack shuffler&#8230; <code>drop</code></p>
<p>There are three basic varieties of stack shufflers. Here they are along with the most commonly used word for each type:</p>
<ol>
<li>
		Removing Shufflers</p>
<dl>
<dt><code>drop</code> <i>( x &#45;&#45; )</i></dt>
<dd>removes the top item from the stack and discards it</dd>
</dl>
</li>
<li>
		Duplicating Shufflers</p>
<dl>
<dt><code>dup</code> <i>( x &#45;&#45; x x )</i></dt>
<dd>duplicates the top item on the stack</dd>
</dl>
</li>
<li>
		Permuting Shufflers</p>
<dl>
<dt><code>swap</code> <i>( x y &#45;&#45; y x )</i></dt>
<dd>exchanges the positions of the first and second items on the top of the stack</dd>
</dl>
</li>
</ol>
<p><img src="/images/2008/12/stack-swap.png" alt="Swapping Two Items on the Top of a Stack" width="554" height="257"></p>
<p>&#8230;and here are a couple of word definitions that use these shufflers appropriately:</p>
<pre><code>: sq ( x -- y )
    dup * ;

: neg ( x -- -x )
    0 swap - ;</code></pre>
<p></p>
<pre><code>(scratchpad) <strong>5 sq .</strong>
25
(scratchpad) <strong>1 neg .</strong>
-1</code></pre>
<p></p>
<h3 id="caveats">Caveats</h3>
<p>There are many more <a href="http://docs.factorcode.org/content/article-shuffle-words.html">shuffle words</a> that support intricate rearrangements and the duplication/removal of multiple items from differing locations within the stack. The problem is, <em>code that is full of stack shufflers can easily become confusing</em>. In general, you should try to minimize the use of stack shufflers to keep things understandable.</p>
<p>Of course there are exceptions to that rule, times when shufflers make the most sense, but the <em>preferred</em> alternative to complicated stack shufflers is the use of a &#8220;combinator&#8221; that fits your use case. That said, combinators will take a bit of explaining&#8230;</p>
<h2 id="quotations">Quotations</h2>
<p>Before we can get to the idea of combinators, we first have to discuss quotations:</p>
<pre><code>(scratchpad) <strong>5 [ "hello" print ] times</strong>
hello
hello
hello
hello
hello</code></pre>
<p></p>
<p>In this example, <code>[ "hello" print ]</code> is a quotation.</p>
<p>In layman&#8217;s terms, a quotation is a way to encapsulate a snippet of code so it doesn&#8217;t get called right away, but can be passed around on the stack and called later. The computer science term for this is an &#8220;anonymous function&#8221;. It&#8217;s referred to as &#8220;anonymous&#8221; because it is fundamentally a word that has no name.</p>
<p>If we didn&#8217;t have quotations, that same example would get ugly real fast:</p>
<pre><code>(scratchpad) <strong>"hello" dup dup dup dup print print print print print</strong>
hello
hello
hello
hello
hello</code></pre>
<p></p>
<p>Imagine if you wanted to print &#8220;hello&#8221; 1000 times!</p>
<p>To create a quotation, you just surround your code snippet with square brackets, and it will be pushed onto the stack: <code>[ ... ]</code>. In order to do anything useful with that quotation once it&#8217;s on the stack, what you need is a combinator.</p>
<h2 id="combinators">What is a Combinator?</h2>
<p>A combinator is just a fancy name for a word that takes a quotation as one of its inputs. In the example above, <code>times</code> is a combinator which takes an integer (<i>n</i>) and a quotation from the stack, and it calls that quotation <i>n</i> times.</p>
<p>Factor uses quotations &#038; combinators extensively for conditionals, sequence traversal, namespaces, closures, and more&#8230;but that&#8217;s jumping the gun a bit. Before we dive into all of that, I&#8217;d like to get back to the idea of minimizing the use of stack shufflers by replacing them with appropriate combinators instead.</p>
<h2 id="intent">Combinators That Express Intent</h2>
<p>While our code examples thus far have been easy to follow, when you start tackling more realistic problems, stack shufflers <em>will</em> begin to obfuscate your code. If we can represent our intentions consistently with a combinator instead, then code becomes cleaner and you can consequently focus more on your problem domain and less on mentally organizing your stack.</p>
<p>To illustrate this point, I&#8217;d like to introduce a couple of simple combinators:</p>
<dl>
<dt><code>dip</code> <i>( x quot &#45;&#45; x )</i></dt>
<dd>calls a quotation while temporarily hiding the top item on the stack</dd>
<dt><code>keep</code> <i>( x quot &#45;&#45; x )</i></dt>
<dd>calls a quotation with an item on the stack, restoring that item after the quotation returns</dd>
</dl>
<p>While both of these combinators have the same stack effect declaration, their usage is a bit different:</p>
<pre><code>(scratchpad) <strong>1 2 4 [ + ] dip</strong>

--- Data stack:
3
4
(scratchpad) <strong>clear</strong>
(scratchpad) <strong>1 2 4 [ + ] keep</strong>

--- Data stack:
1
6
4</code></pre>
<p></p>
<p>These two combinators alone can greatly reduce the number of stack shufflers your code will need. If you&#8217;re curious about how these combinators work, they both secretly take advantage of an auxiliary stack (called the &#8220;retain stack&#8221;) to temporarily store items while the supplied quotation is being executed. There are a few other <a href="http://docs.factorcode.org/content/article-slip-keep-combinators.html">retain stack combinators</a> that are worth exploring as well.</p>
<h3 id="cleave">Cleave, Spread, and Apply</h3>
<p>The cleave, spread, and apply combinators are your best weapons when trying to reduce the use of stack shufflers while simultaneously expressing intent. The key point being that they <em>should</em> express intent&#8230;if you find yourself writing code where these combinators don&#8217;t fit logically, then try another option.</p>
<ol>
<li><strong>Cleave Combinators</strong>
<p>These are used when you want to <em>apply multiple quotations to the same set of items</em> on the top of the stack.</p>
<p>	Let&#8217;s say that you want to find the average of a bunch of numbers in an array. The steps are straightforward, you take the sum of all the numbers and divide that sum by how many numbers you have (the length of the array):</p>
<pre><code>(scratchpad) <strong>{ 1 2 3 } dup sum swap length / .</strong>
2</code></pre>
<p></p>
<p>	We can eliminate the need for those stack shufflers and better express our intent by using a cleave combinator to achieve the same thing:</p>
<dl>
<dt><code>bi</code> <i>( x p q &#45;&#45; )</i></dt>
<dd>applies quotation <b><i>p</i></b> to <b><i>x</i></b>, then applies quotation <b><i>q</i></b> to <b><i>x</i></b></dd>
</dl>
<p>	<img src="http://elasticdog.com/images/2008/12/cleave.png" alt="Cleave Combinator Demonstration" width="510" height="222" /></p>
<pre><code>(scratchpad) <strong>{ 1 2 3 } [ sum ] [ length ] bi / .</strong>
2</code></pre>
<p></p>
<p>	The different <a href="http://docs.factorcode.org/content/article-cleave-combinators.html">cleave combinators</a> change either the number of quotations applied to your items (<code>bi</code> vs. <code>tri</code>), or the number of items used as input for your quotations (<code>bi</code> vs. <code>2bi</code>).
	</li>
<li><strong>Spread Combinators</strong>
<p>These are used when you want to <em>apply a different quotation to different items</em> on the top of the stack. The spread combinators are closely related to <code>dip</code>, but provide a bit more flexibility while also expressing intent.</p>
<p>	Let&#8217;s say that you have two coordinate positions in the form of <code>{ x y }</code>, and you&#8217;d like to extract the x-coordinate from the first position, and the y-coordinate from the second position to form a new position with those values:</p>
<pre><code>(scratchpad) <strong>{ 1 2 } { 3 4 } swap first swap second 2array .</strong>
{ 1 4 }</code></pre>
<p></p>
<p>	We can eliminate the need for those stack shufflers and better express our intent by using a spread combinator to achieve the same thing:</p>
<dl>
<dt><code>bi*</code> <i>( x y p q &#45;&#45; )</i></dt>
<dd>applies quotation <b><i>p</i></b> to <b><i>x</i></b>, then applies quotation <b><i>q</i></b> to <b><i>y</i></b></dd>
</dl>
<p>	<img src="http://elasticdog.com/images/2008/12/spread.png" alt="Spread Combinator Demonstration" width="510" height="222" /></p>
<pre><code>(scratchpad) <strong>{ 1 2 } { 3 4 } [ first ] [ second ] bi* 2array .</strong>
{ 1 4 }</code></pre>
<p></p>
<p>	When you want to do the same thing with more than two quotations/items, then using spread combinators eliminates the need for nested dips or shufflers and the added clarity becomes much more evident.</p>
<p>	The different <a href="http://docs.factorcode.org/content/article-spread-combinators.html">spread combinators</a> change the number of quotations applied to the corresponding number of items on the stack (<code>bi*</code> vs. <code>tri*</code>).
	</li>
<li><strong>Apply Combinators</strong>
<p>These are used when you want to <em>apply a single quotation to multiple items</em> on the top of the stack.</p>
<p>	Let&#8217;s say that you have two strings, each containing a name, and you want to see if those names are the same. In order to ignore case when doing the comparison, you decide to convert both strings to upper case before checking for equality:</p>
<pre><code>(scratchpad) <strong>"john" "John" swap &gt;upper swap &gt;upper = .</strong>
t</code></pre>
<p></p>
<p>	We can eliminate the need for those stack shufflers and better express our intent by using an apply combinator to achieve the same thing:</p>
<dl>
<dt><code>bi@</code> <i>( x y quot &#45;&#45; )</i></dt>
<dd>applies the quotation to <b><i>x</i></b>, then to <b><i>y</i></b></dd>
</dl>
<p>	<img src="http://elasticdog.com/images/2008/12/apply.png" alt="Apply Combinator Demonstration" width="510" height="222" /></p>
<pre><code>(scratchpad) <strong>"john" "John" [ &gt;upper ] bi@ = .</strong>
t</code></pre>
<p></p>
<p>	The different <a href="http://docs.factorcode.org/content/article-apply-combinators.html">apply combinators</a> change the number of items on the stack your quotation is applied to  (<code>bi@</code> vs. <code>tri@</code>).
	</li>
</ol>
<p>The cleave, spread, and apply combinators are all closely related; if you&#8217;re having trouble keeping them apart, try to memorize the naming convention:</p>
<ul>
<li>If there is no suffix, it is a &#8220;cleave&#8221;</li>
<li>If the suffix is *, it is a &#8220;spread&#8221;</li>
<li>If the suffix is @, it is an &#8220;apply&#8221;</li>
</ul>
<p>Once you learn these combinators, you should be able to express almost any pattern of complicated stack shufflers. Note that there are also <a href="http://docs.factorcode.org/content/article-combinators.html">generic forms</a> for all of these combinators that can take additional inputs from the stack. If you find that you resort to using the generic forms more often then not, that&#8217;s usually a good indication that you should rethink your approach or put your data into a more appropriate structure.</p>
<h2 id="data">Data Type Details</h2>
<p>Before I turn you loose, I wanted to offer a few extra details about some of Factor&#8217;s basic data types&#8230;</p>
<h3 id="sequences">Sequences</h3>
<p>In the cleave and spread examples above, I was sneaky and used sequences without explaining them formally. A sequence is a <em>finite, ordered, collection of elements</em>. Any data type that implements the sequence mixin class (meaning a data type that knows its length and will let you set/get an element at a specific index) gains the ability to use the powerful built-in <a href="http://docs.factorcode.org/content/article-sequences.html">sequence operators</a>. Read through that documentation to get an idea on how to manipulate sequences and their elements.</p>
<p>Factor has many sequence types that you may already be familiar with, such as <a href="http://docs.factorcode.org/content/article-arrays.html">arrays</a> (fixed-size mutable sequences) and <a href="http://docs.factorcode.org/content/article-vectors.html">vectors</a> (resizable mutable sequences), but there are also other data types that you might not expect to be sequences, such as strings. In Factor, a string is merely an array of Unicode 5.0 code points.</p>
<p>Using the sequence operators and combinators together, you can create all sorts of powerful abstractions that I&#8217;ll talk more about next time. Here are a couple of examples to whet your appetite:</p>
<pre><code>(scratchpad) <strong>{ 1 2 3 4 5 } [ even? ] filter .</strong>
{ 2 4 }
(scratchpad) <strong>{ 1 2 3 } [ . ] each</strong>
1
2
3
(scratchpad) <strong>"Hello" [ alpha? ] all? .</strong>
t
(scratchpad) <strong>"Hello!!!" [ alpha? ] all? .</strong>
f</code></pre>
<p></p>
<p>Keep in mind that in Factor, <strong>sequences are zero-based</strong>.</p>
<h3 id="numbers">Numbers</h3>
<p>The last topic for the day is numbers. So far, we have only used integers, but Factor also supports <a href="http://docs.factorcode.org/content/article-rationals.html">rational numbers</a> (fractions), <a href="http://docs.factorcode.org/content/article-floats.html">floats</a> (decimal approximations of a number), and <a href="http://docs.factorcode.org/content/article-complex-numbers.html">complex numbers</a> (imaginary numbers).</p>
<pre><code>(scratchpad) <strong>100 330 / .</strong>
10/33
(scratchpad) <strong>5/4 1/2 + .</strong>
1+3/4
(scratchpad) <strong>5/4 0.5 + .</strong>
1.75
(scratchpad) <strong>2 5 rect&gt; .</strong>
C{ 2 5 }</code></pre>
<p></p>
<p>Integers have one additional property that makes them special&#8230;<strong>non-negative integers are sequences</strong>. A positive integer (<i>n</i>) is a sequence of length <i>n</i>, whose elements are its non-negative predecessors (i.e. 0 to <i>n</i>-1 inclusive). This property can be very helpful when performing counted loops or other control flow statements.</p>
<p>Because of this property, the following two lines are equivalent:</p>
<pre><code>{ 0 1 2 3 4 5 6 7 8 9 } [ . ] each
10 [ . ] each</code></pre>
<p></p>
<h2 id="next">Next Time</h2>
<p>Next time weâ€™ll stick with a theme of &#8220;flow&#8221; and discuss control flow for your words and also the typical work flow when developing code with Factor. Hope you enjoyed this installment!</p>
---<br />Related Posts at ElasticDog:<ul><li><a href="http://elasticdog.com/2008/11/beginning-factor-introduction/" rel="bookmark" title="November 29th, 2008">Beginning Factor &#8211; Introduction</a></li>
</ul><!-- Similar Posts took 6.352 ms -->]]></content:encoded>
			<wfw:commentRss>http://elasticdog.com/2008/12/beginning-factor-shufflers-and-combinators/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Beginning Factor &#8211; Introduction</title>
		<link>http://elasticdog.com/2008/11/beginning-factor-introduction/</link>
		<comments>http://elasticdog.com/2008/11/beginning-factor-introduction/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 17:55:20 +0000</pubDate>
		<dc:creator>Aaron Schaefer</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Factor]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://elasticdog.com/?p=29</guid>
		<description><![CDATA[The <a href="http://factorcode.org/">Factor programming language</a> is a huge departure from the norm for most developers and it can be overwhelming to someone just getting started. I would like to help ease that transition by posting on various topics that I know have been confusing to me over the past year...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been involved in the <a href="http://factorcode.org/">Factor programming language</a> community for about a year now, and am constantly amazed with how productive its contributors are. Large improvements to the language and its libraries are made on a weekly (if not daily) basis, and it&#8217;s finally starting to attract some much-deserved attention from the programming community.</p>
<p>The problem is, the language is a huge departure from the norm for most developers and it can be overwhelming to someone just getting started. I would like to help ease that transition by posting on various topics that I know have been confusing to me over the past year.</p>
<p>Note that many topics are covered in <a href="http://factorcode.org/faq.fhtml">the official FAQ</a> &#8212; which is well worth a read &#8212; and I won&#8217;t spend time covering how to install Factor on the 14 or so platforms it supports, but beyond that, I&#8217;ll try to give enough information (from basic to advanced) to get you going.</p>
<h2 id="basics">The Basics of the Basics</h2>
<p>First of all, Factor is a stack-based language, which means that it uses a <a href="http://en.wikipedia.org/wiki/Stack_(data_structure)">stack</a> to store all arguments and returned values from functions (called &#8220;words&#8221; in Factor). To put that another way, Factor words don&#8217;t receive arguments in the traditional manner; all input values that your word needs are expected to already exist on the top of the stack.</p>
<h3 id="what-stack">What is a Stack?</h3>
<p>If you&#8217;re unfamiliar with the data structure called a stack, the concept is fairly simple. It centers around the idea of &#8220;Last In First Out&#8221; (LIFO), meaning the last item placed onto the stack is the first item that will you will get when you remove an item from the stack. You cannot get to the lower items until the all of the items above it have been removed.</p>
<p>I like to picture a stack like a tower of LEGOs. The only thing you can do with it are add another brick to the top (&#8220;push&#8221; an item onto the stack), or take the top brick off to use it for something else (&#8220;pop&#8221; an item off the stack). That&#8217;s all there is to stacks!</p>
<p><img src="/images/2008/11/stack-push.png" alt="Pushing an Item onto a Stack" width="554" height="257" /></p>
<h3 id="use-stack">How to Use the Stack</h3>
<p>So, now we know that Factor manages its input and output with a stack, but how do we actually use it? Well, when entering data into Factor&#8217;s listener, one of two things is happening:</p>
<ol>
<li>You are pushing a literal onto the stack<br /><strong>OR</strong></li>
<li>You are calling a word which will consume literals from the stack</li>
</ol>
<p>Things do get slightly more complicated than that, but for the most part those two rules hold true. The most simple example of this behavior is the ubiquitous hello world program, shown entered directly into Factor&#8217;s interactive listener environment:</p>
<pre><code>(scratchpad) <strong>"Hello world!" print</strong>
Hello world!</code></pre>
<p></p>
<p>To understand what is happening, you can just type the string first, and then execute the <code>print</code> word later:</p>
<pre><code>(scratchpad) <strong>"Hello world!"</strong>

--- Data stack:
"Hello world!"
(scratchpad) <strong>print</strong>
Hello world!</code></pre>
<p></p>
<p>In this particular case, typing the literal string <code>"Hello world!"</code> will push that value onto the stack; then the word <code>print</code>, takes a single string off the stack and writes it to the output stream.</p>
<p>The same idea can be applied to simple arithmetic. Like strings, numbers are also literals, so you just have to type them in (separated by spaces) for them to be pushed onto the stack:</p>
<pre><code>(scratchpad) <strong>2 3</strong>

--- Data stack:
2
3</code></pre>
<p></p>
<p><i>NOTE: the stack is displayed in the listener upside-down from the way you&#8217;d think, so the bottom number is actually the top of the stack.</i></p>
<p>&#8230; and if you want to add the top two numbers together, the <code>+</code> word will simply pop two numbers off the stack, add them together, and push the result back onto the stack:</p>
<pre><code>--- Data stack:
2
3
(scratchpad) <strong>+</strong>

--- Data stack:
5</code></pre>
<p></p>
<h3 id="words">A Few More Words</h3>
<p>If you start messing around in the listener, odds are your stack is going to grow pretty quickly and become unmanageable. There are a few words that are essential to know in order to keep things under control:</p>
<dl>
<dt><code>drop</code> <i>( x &#45;&#45; )</i></dt>
<dd>removes the top item from the stack and discards it</dd>
<dt><code>.</code> <i>( obj &#45;&#45; )</i></dt>
<dd>takes the top item from the stack and prettyprints it</dd>
<dt><code>clear</code> <i>( &#45;&#45; )</i></dt>
<dd>removes all items from the stack</dd>
</dl>
<pre><code>(scratchpad) <strong>2 3 -</strong>

--- Data stack:
-1
(scratchpad) <strong>drop</strong>
(scratchpad) <strong>20 5 / .</strong>
4
(scratchpad) <strong>2 3 + 6 7</strong>

--- Data stack:
5
6
7
(scratchpad) <strong>clear</strong>
(scratchpad)</code></pre>
<p></p>
<h3 id="stack-ramifications">Ramifications of Stack-Based Design</h3>
<p>From these simple examples, we can observe a couple of important things about Factor:</p>
<ol>
<li><strong>Postfix Notation</strong>
<p>When using a stack, passing data becomes implicit and we can assume that all input needed by words already exists on the stack. This naturally lends itself to using <a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation">postfix notation</a> because you have to push your data onto the stack before you can use it.</p>
<p>This also makes words more concise and unambiguous when compared to <em>infix</em> notation and eliminates the need for copious amounts of parentheses used by <em>prefix</em> notation languages, like Lisp or Scheme.</p>
<pre><code>POSTFIX:  6 5 4 * +
INFIX:    6 + 5 * 4 =
PREFIX:   (+ (* 4 5) 6)</code></pre>
<p></p>
<p>With the infix example above, you&#8217;d have to know <a href="http://en.wikipedia.org/wiki/Order_of_operations">order of operations rules</a> in order to get the correct answer (or use parentheses to force the matter). When using postfix notation, the fact that multiplication is done first becomes explicit. Prefix notation also gets rid of that ambiguity, but becomes messier and harder to type with the more nesting you add.</li>
<li><strong>Calling Words is Implicit</strong>
<p>You don&#8217;t have to specify that you&#8217;re calling a word, you simply use the word. This, combined with postfix syntax, means that you can easily nest words or cut and paste parts of definitions into new words without disrupting the flow of data. This lends to keeping code modular, short, easily testable, and readable.</p>
</li>
</ol>
<h2 id="first-word">Your First Word</h2>
<p>If you&#8217;ve typed in the examples from above and messed around in the listener, then you might want to know how to write your own word rather than just using ones that are predefined. Drawing on what we already know, here&#8217;s how to write your first word:</p>
<pre><code>: plus-two ( x -- y )
    2 + ;</code></pre>
<p></p>
<p>If you copy and paste that into your listener, than you can use the word <code>plus-two</code> anywhere you would like to add two to a number on the top of the stack:</p>
<pre><code>(scratchpad) <strong>15 plus-two .</strong>
17</code></pre>
<p></p>
<p>Not very exciting, but it gives us a couple more things to talk about&#8230;</p>
<h3 id="syntax">Syntax Specifics</h3>
<p>If you study the word definition, you&#8217;ll see that it&#8217;s made up of a few elements:</p>
<pre><code>: plus-two ( x -- y )
    2 + ;</code></pre>
<p></p>
<ol>
<li>A <strong>colon</strong> (<code>:</code>) is used to start the definition of a word. This is required and must have a space after it.</li>
<li>Right after the opening colon is the <strong>name</strong> of your word, also required.</li>
<li>Following the name of your word is its <strong>stack effect declaration</strong>, which is a list of the word&#8217;s inputs and outputs separated by <code>--</code> and surrounded by parentheses. All words must have a stack effect declaration unless it only pushes literals on the stack. The names of elements in the stack effect declaration don&#8217;t make a difference, only the number of elements. That means that <code>( elt elt -- seq )</code> and <code>( x y -- z )</code> are the same thing. There <em>are</em> some common conventions for these names, but don&#8217;t get caught up by it as I&#8217;ll talk more about them in a later article and it won&#8217;t change how your program runs.</li>
<li>Next comes the <strong>word definition</strong> itself, in this case <code>2 +</code>.</li>
<li>And the last item is a <strong>semicolon</strong> (<code>;</code>), used to end the word definition. This is required and must have a space before it.</li>
</ol>
<p>The reason that everything must be surrounded by spaces is that there aren&#8217;t any syntax-only elements to Factor&#8230;everything is a word! The colon/semicolon, the parentheses, etc. are all just parsing words working together in order to create the syntax.</p>
<h2 id="next">Next Time</h2>
<p>Next time we&#8217;ll talk more about stack shufflers, quotations &#038; combinators, details about more datatypes, and more&#8230;</p>
<p>If something is unclear or if you&#8217;re having any trouble, let me know and I&#8217;ll try to help out!</p>
---<br />Related Posts at ElasticDog:<ul><li><a href="http://elasticdog.com/2008/12/beginning-factor-shufflers-and-combinators/" rel="bookmark" title="December 4th, 2008">Beginning Factor &#8211; Shufflers &#038; Combinators</a></li>
</ul><!-- Similar Posts took 3.644 ms -->]]></content:encoded>
			<wfw:commentRss>http://elasticdog.com/2008/11/beginning-factor-introduction/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
