From fc3dae6f5ee8a60deafaa69ce960bf265ade8700 Mon Sep 17 00:00:00 2001 From: Daniel Ehrenberg Date: Fri, 29 Apr 2005 22:01:35 +0000 Subject: [PATCH] fixing overflows --- doc/comparison.tex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/comparison.tex b/doc/comparison.tex index 87baa06288..dbd14b88db 100644 --- a/doc/comparison.tex +++ b/doc/comparison.tex @@ -18,7 +18,7 @@ Factor uses essentially the same parsing model as Forth, except there is no inte You can put any code as these lists, and you can move it to the return stack for execution using the word call. This technique is used very often for higher-order functions, largely the same way \texttt{[']} or \texttt{'} is used. \texttt{[ word ]} is the Factor equivalent of \texttt{['] word} for most purposes, but for other operations, prefix it with a backslash (\texttt{\char'134 word}). For executing a plain word (prefixed with a backslash in Factor), you can use \texttt{execute} just like Forth. But this is not used very often. Normally, you want to use a whole quotation (surrounded by square brackets) since it can be longer than a word, and to execute that, use \texttt{call}. -One of the most visible differences between Factor and Forth is the tendency to use these code quotations where one would use immediate words in Forth. For example, instead of Forth's \verb|if true stuff else false action then|, in Factor you would write \verb|[ true stuff ] [ false action ] ifte|. In order to do \verb|if true stuff then| in Factor, you can't just do \verb|[ true stuff ] ifte|; you have to use a different word instead of ifte, specifically \texttt{when}, in this case: \verb|[ true stuff ] when|. While Factor doesn't have words like \texttt{<>if}, it has a whole range of other if-related words, including \texttt{unless}, \texttt{when*}, \texttt{unless*}, \texttt{?ifte}, and \texttt{?}. Similarly, for a Forth construct like \verb|for blah next|, the Factor code is \verb|[ blah ] repeat|, and for the Forth construct \verb|begin test while action repeat|, the Factor code is \verb|[ test ] [ action ] while|. +One of the most visible differences between Factor and Forth is the tendency to use these code quotations where one would use immediate words in Forth. For example, instead of Forth's \verb|if true stuff else false action then|, in Factor you would write \verb|[ true stuff ] [ false action ] ifte|. In order to do \verb|if true stuff then| in Factor, you can't just do \verb|[ true stuff ] ifte|; you have to use a different word instead of ifte, specifically \texttt{when}, in this case: \verb|[ true stuff ] when|. While Factor doesn't have words like \texttt{<>if}, it has a whole range of other if-related words, including \texttt{unless}, \texttt{when*}, \texttt{unless*}, \texttt{?ifte}, and \texttt{?}. Similarly, for a Forth construct like \verb|for blah next|, the Factor code is \verb|[ blah ] repeat|, and the Factor code \verb|[ test ] [ action ] while| is for the Forth construct \verb|begin test while action repeat|. When doing operations on words in Forth, you tend to use immediate words, but in Factor, you usually use runtime words. This is possible because words are first-class objects in Factor, just like Forth, except in Factor, a word object holds much more information. As stated above, \texttt{\char'134} is used to refer to words without executing them. A good example of the different ways that Factor and Forth deal with words is \texttt{see}. Factor and Forth both have \texttt{see}, and they both do roughly the same thing, but in Factor, you would write \verb|\ word see| where you would write in Forth \verb|see word|. @@ -66,13 +66,13 @@ Common Lisp and Scheme don't have many similarities to Factor on the surface, bu To define a function (usually called a word in Factor), very simple syntax is used. Where you would write \verb|(define (function arg) body)| in Scheme or \verb|(defun function (arg) body)| in Lisp, you write \verb|: function body ;| in Factor. The lack of arg is possible because Factor implicitly passes around variables using the stack. -Factor's object system holds may similarites to CLOS. \texttt{TUPLE:} is vaguely analogous to defstruct, though it doesn't provide nearly as much flexibility. An equivalent example would be \verb|TUPLE: point x y z ;| is equivalent to \verb|(defstruct point x y z)|. No more advanced funtionality of \texttt{defstruct} is available. In Factor, you can define methods, but they can only be single-dispatch. In Factor, as in CLOS, generic functions/words must be explicitly declared before defining methods on them. \texttt{GENERIC:} is like \texttt{defgeneric} and \texttt{M:} is like \texttt{defmethod}. +Factor's object system holds may similarites to CLOS. \texttt{TUPLE:} is vaguely analogous to defstruct, though it doesn't provide nearly as much flexibility. An equivalent example would be \verb|TUPLE: point x y z ;| is approximately equal to \verb|(defstruct point x y z)|. No more advanced funtionality of \texttt{defstruct} is available. In Factor, you can define methods, but they can only be single-dispatch. In Factor, as in CLOS, generic functions/words must be explicitly declared before defining methods on them. \texttt{GENERIC:} is like \texttt{defgeneric} and \texttt{M:} is like \texttt{defmethod}. In Factor, code is represented as lists, just like in Scheme and Lisp. But when we say that, we mean it, right up to runtime, at least in interpreted mode. The syntax for creating the equivalent of a lambda is the same as the syntax for creating a list: \verb|[ blah blah blah ] for \verb|'(blah blah blah). (In Factor, symbols aren't automatically interred; you have to define them before refering to them. In case you're wondering, \verb|\ blah| is what you write for \verb|'blah|, but usually, we define special symbol words for this so that \verb|blah| is equivalent to \verb|\ blah|. You can do this with \verb|SYMBOL: blah|.) The reason this isn't as slow as the \texttt{eval} you're used to is because all the Factor runtime needs to do is iterate through the list, calling each word in it. These lists are known as quotations. Because they're just sitting there on the stack, an explicit \texttt{funcall} equivalent is needed, and that equivalent is \texttt{call}. In general, you use \verb|[ a ]| as a replacement for Lisp's \verb|#'a| and Scheme's \verb|a|. Because of the convienent syntax for quotations, they are used much more often than in Lisp and Scheme, which tend to use macros. For example, the equivalent of \verb|(if x y z)| is \texttt|x [ y ] [ z ] ifte|. Factor's builtin datatypes are also similar to Lisp and Scheme. Lists and vectors have correctly corresponding names and meanings in all three languages we're discussing, and so are hashtables between Lisp and Factor. The biggest difference is that lists are now immutable in Factor whereas they are mutable in Lisp and Scheme. Factor provides library functions for dealing with alists, though not plists. In Factor, you can use \texttt{cons} to cons things together, \texttt{car} to get the car of a list, and \texttt{cdr} to get the cdr of a list, pretty intuitive for a Lisp or Scheme programmer. The empty list is false (\texttt{f}), just like Lisp, and the car or cdr of f is f, just like Lisp. Factor's list literal syntax is \verb|[ 1 2 3 ]| for the equivalent of \verb|'(1 2 3)|. You can include symbols in your list literals too, for example \verb|[ blah ]| is like \verb|'(blah)|. Factor has some of the same basic list higher order functions as Lisp and Scheme. Lisp's \texttt{mapcar} and Scheme's \texttt{map} are \texttt{map} in Factor. Lisp's \texttt{remove-if-not} and Scheme's \texttt{filter} are \texttt{filter} in Factor. -Factor's \texttt{callcc0} and \texttt{callcc1} are essentially the same as Scheme's \texttt{call-with-current-continuation} (aka \texttt{call/cc}). The two versions in Factor exist because you need to explicitly specify the arity of your continuation, since factor has no variable argument mechanism. But usually in Factor, you would wrap up continuations in a library rather than use them directly. There is no equivalent of \texttt{dynamic-wind}, and instead, \texttt{catch} (the error handler) is used for most of those resource-handing places. +Factor's \texttt{callcc0} and \texttt{callcc1} is \texttt{call-with-current-continuation} (aka \texttt{call/cc}) in Scheme, basically. The two versions in Factor exist because you need to explicitly specify the arity of your continuation, since factor has no variable argument mechanism. But usually in Factor, you would wrap up continuations in a library rather than use them directly. There is no equivalent of \texttt{dynamic-wind}, and instead, \texttt{catch} (the error handler) is used for most of those resource-handing places. Though Factor's "macros" are actually extensions to the parser, similar to reader macros, certain things, such as the generic word system, are implemented like Common Lisp macros, generating a quote at parsetime and substituting it in. This is possible because Factor shares with Lisp and Scheme the principle that code is data, namely nested lists. A macro is created with the syntax \verb|: macro-name definition-body ; parsing|. But, as stated above, macros in general are used less frequently in Factor than Forth. @@ -149,6 +149,6 @@ Factor has no direct equivalent of Python's lists, but instead it has several ot Another collection type in Factor is the vector. Vectors are used when you want to index it with a number. The syntax is similar to lists: \verb|{ 1 2 3 }| is like \verb|[1, 2, 3]|. There is no equivalent of make-list for vectors. Vectors are fixed-length but they are mutable. Factor has hashtables in place of Pythons's dictionaries. \verb|{"hi": 1, "hello": 2}| is \verb|{{ [[ "hi" 1 ]] [[ "hello" 2 ]] }}|. -Factor supports the same basic math functions as Python, \texttt{+}, \texttt{-}, \texttt{*}, \texttt{/} and \texttt{**} (only renamed \texttt{^}) as well as a larger math library than standard Python. But in Factor, those aritmetic functions are regular functions, not special operators. In addition to all the number types Python has, Factor also has a rational number type. +Factor supports the same basic math functions as Python, \texttt{+}, \texttt{-}, \texttt{*}, \texttt{/} and \texttt{**} (only renamed \texttt{\^{}}) as well as a larger math library than standard Python. But in Factor, those aritmetic functions are regular functions, not special operators. In addition to all the number types Python has, Factor also has a rational number type. \end{document}