Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the SECD Machine how does "rap" work?

Tags:

I am writing a simulator of the SECD machine in C# guided by the description on Wikipedia. I have the basic operations completed, but I am not sure how to implement the rap instruction.

At Wikipedia it says about rap:

rap works like ap, only that it replaces an occurrence of a dummy environment with the current one, thus making recursive functions possible

And for ap it says:

ap pops a closure and a list of parameter values from the stack. The closure is applied to the parameters by installing its environment as the current one, pushing the parameter list in front of that, clearing the stack, and setting C to the closure's function pointer. The previous values of S, E, and the next value of C are saved on the dump.

Here is my implementation of ap

    public void ap() 
    { 
        Push(S, ref D); 
        Push(E, ref D); 
        Push(C, ref D); 
        List closure = Pop(ref S);
        List paramlist = Pop(ref S);
        E = closure.Tail;
        Push(paramlist, ref E);
        C = closure.Head;
        S = List.Nil;
    }

Note that List is my implementation of a Lisp style "cons" cell.

What confuses me is exactly how rap differs from ap? For example what exactly happens to the environment register (E)? I find the Wikipedia definition a bit ambiguous, and haven't been able to find anything else that explains it well.