Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:36:28+00:00 2026-05-25T22:36:28+00:00

I am writing a simulator of the SECD machine in C# guided by the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:36:29+00:00Added an answer on May 25, 2026 at 10:36 pm

    SECD is not tail recursive, although you can build a tail recursive SECD machine.

    The AP instruction is used to compile a ‘let’ binding whereas the RAP instruction is used to compile a ‘letrec’ binding.

    ‘letrec’ is different from ‘let’ because you can ‘see’ the environment where the recursive function is defined, so that you can call it recursively (so, for instance, you define a ‘factorial’ function and you can call it in the body of the function).

    RAP modifies the environment by calling rplaca (similar to setcar! in Scheme). A previous DUM instruction adds a "dummy" car to the environment (which is never used), and RAP removes this "dummy" ‘car’ in the environment and replaces it with the appropriate one.

    State transitions are like so:

    S E C D S’ E’ C’ D’
    ((c'.e')v.s) e (AP.c) d NIL (v.e') c' (s e c.d)
    ((c'.e')v.s) (?.e) (RAP.c) d NIL (setcar! e',v) c' (s e c.d)

    See also Revisiting SECD and the power of Lisp, quoting:

    The RAP instruction is used to support recursive function calls and works by replacing a previously created dummy environment on the stack, called OMEGA, by one which contains all the functions that are visible in the recursive scope. The specification uses RPLACA to denote that replacement operation, and that is what we used in our Lisp implementation of SECD, too: …

    and

    When trying to implement RAP in Erlang, I got stuck because there are no destructive operations on lists. Not in the standard API, and seemingly not in the system API either. So, the Erlang SECD looks nice, only it does not run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a football manager simulator with php, [ HARD AlGORITHMS !] i have
Usually I don't have problems writing assembly and testing and debugging. But then sometimes
This code works on simulator but not on device. I am positive this has
BACKGROUND: I'm writing a single level cache simulator in C for a homework assignment,
We are writing apps for Android and so far only use the simulator. What
I'm writing a client and a server for a realtime offshore simulator, and, as
I am writing a small shooting game using cocos2d. It's working good in simulator.
I'm writing some data to a plist file but it fails on the device
I am writing a dice game simulator in Python. I represent a roll by
I am practicing techniques learned by writing a small battle simulator. In my hero

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.