lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Dec 20, 2012, at 6:43 PM, Andres Perera wrote:

> On Thu, Dec 20, 2012 at 5:42 PM, Jay Carlson <nop@nop.com> wrote:
>> 
>> I really like this, but I hate numbers. Consider a function swap():
>> 
>>   swap(1, 2)
>>     -> return 2, 1
>>   swap(1, 2, 3)
>>     -> return 2, 1, 3
>>   swap(1, nil)
>>     -> return nil, 1
>> 
>>   -- I'm ultra-paranoid. Would you ever want this to work?
>>   swap(1)
>>     -> error()

> it's asking for a general rev() function
> 
> (collect '() (rev list))

> rev() can be implemented in terms of swap(), but now we are getting
> too Forthy.

Note that swap() works on argument lists and produces return values lists. In the Lua API this *is* a stack. 

The Forth word SWAP is not right because we're working at the bottom of the stack. In Forth this is where you would really want the PostScript "mark" operator....[1]

Given how the first argument/first return value is special, I think the generalization of backwards-SWAP is probably backwards-ROLL: take the nth item from the argument list and move it to be first, filling in gaps appropriately. But then you need a separate inverse operation (bury?); swap is its own inverse.

Jay

[1]: As soon as you want a mark operator, you realize you want the values on the stack to have types so the mark can be disjoint. But once you start designing a typed-Forth you end up with PostScript anyway. PostScript is another one of these languages where a small number of axioms are taken to their logical conclusion.