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 7815429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:31:11+00:00 2026-06-02T05:31:11+00:00

I’m currently working on a LISP interpreter written in Java. Now I stuck at

  • 0

I’m currently working on a LISP interpreter written in Java. Now I stuck at the closures. I want to enable closures like this:

(define a 1000)
(define closure (lambda (a) (lambda (b) (+ a b))))
(define x (closure 10))
(x 20) --> 30

So, (x 20) should return 30. But, guess what, it returns 1020 in my interpreter. I think the mistake is in my lambda class. It looks like this:

public class LLambda extends LOperation {

    private LList parameters;
    private LList definitions;

    public LLambda(LList parameters, LList definitions) {

        this.parameters = parameters;
        this.definitions = definitions;
    } 

    public LObject eval(Environment environment, LObject tokens) {

        environment = environment.copy();

        for(int i = 0; i < parameters.size(); i++) {

            LSymbol key = LSymbol.create(parameters.get(i));
            LObject object = ((LList) tokens).get(i);
            object = object.eval(environment, tokens);  
            environment.put(key, object);
        }

        return definitions.eval(environment, tokens);
    }
}

This class works fine, but it doesn’t stores the environment values to enable closures. Has someone an idea how to do it? And where to do it? In the constructor or in the eval method?

And, if I don’t execute this:

environment = environment.copy();

The closures works, but it breaks some other tests.

Thanks.

(I can also upload the whole source or give it free in GIT).

  • 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-06-02T05:31:13+00:00Added an answer on June 2, 2026 at 5:31 am

    This class works fine, but it doesn’t stores the environment values to enable closures. Has someone an idea how to do it? And where to do it? In the constructor or in the eval method?

    Yes, the class should store the environment. Generally speaking, a member variable. 🙂

    It should be in the constructor because the environment is bound at the time the lambda is constructed, and not at eval time.

    At eval time, the original environment is not available: the new environment is.

    If your dialect is purely lexically scoped, then your lambda does not need an environment parameter. Remember, what is a lambda? It is a function. The evaluation of forms requires an environment. The evaluation of functions does not; the evaluation of a function is a function call, and that takes just arguments. Environments do not pass into functions; function bodies are evaluated in an encapsulated space with its own private environment. (The presence of an eval function on a lambda even seems wrong; you want this to be named call or something like that. The interpreted lambda uses the services of the evaluator; but it isn’t one.)

    Inside your lambda, the action will be to evaluate the forms of the lambda body. Those will use the stored environment (not anything that was passed in).

    You have to establish an environment in which the lambda parameters have bindings to the argument values. This is nested within the captured environment. (I.e. an argument called x shadows a captured variable called x).

    (You must already have some way of nesting environments; i.e constructing some new bindings which refer to an outer environment.)

    If you want to support dynamic scoping in addition to lexical, there shouldn’t be any need to pass an environment for that; it can be done implicitly. A thread-local variable can maintain the dynamic environment or something like that.

    (The details depend on interpreter design; in some designs, there is always some context object passed around (representing the interpreter). If you go to, say, a byte code virtual machine with an explicit stack, you will need that.)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't

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.