Concatenative language/Rest parameters

Stack languages do not immediately support variadic functions (like Common Lisp's &rest parameters) because there is no way to tell how many parameters must be taken from the stack; whereas all of the following is valid Lisp,

(+ a)
(+ a b)
(+ a b c)
(apply #'+ list)

In a stack language, the number of inputs to + must be fixed, and it only makes sense to have two inputs.

One approach is to make a new word which takes an integer, and operates on that many items at the top of the stack together. Factor's generalizations vocabulary has many such words. They are mainly intended for use in macros:

"a" 1 narray
"a" "b" 2 narray
"a" "b" "c" 3 narray

Another approach that is often used is to make a word which takes a sequence of values. If all the values are literal:

{ 5 } sum
{ 5 6 } sum
{ 5 6 7 } sum

or if not:

2 3 + 6 7 3array sum

This revision created on Sun, 9 Aug 2009 17:27:14 by mncharity (expanded "takes a sequence" example to clarify { } only works with literals)