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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:24:11+00:00 2026-06-14T17:24:11+00:00

I need to define a data structure recursively in Javascript. Here is a simple

  • 0

I need to define a data structure recursively in Javascript. Here is a simple example of a circular linked list:

// List a very simplified example of what the actual (non list) code does.
function List(f, r) {
    return function(){ return [f, r]; };
}

var first = function (l){ return l()[0]; }
var rest = function (l){ return l()[1]; }

var head = List('a', List('b', List('c', head)));

When this is executed, head in List ‘c’ is resolved to undefined, not List ‘a’ as I need. List is an example function that returns a function (It is not an Javascript list that I can append to).

I tried to wrap the definition of head is a self executing named function, but that blew the stack when head was resolved.

What is the Javascript style solution that I am overlooking?


Attempt

Fooling around, I came up with some code that may work:

var f = function(){
    var value;
    return function(v){
        if (value === undefined)
            value = v
        return value.apply(undefined, arguments);
    };
};

var tempHead = f();
var head = List('a', List('b', List('c', tempHead)));
tempHead(head);

first(head); // a
first(rest(head)) // b
first(rest(rest(head))) // c
first(rest(rest(rest(head)))) // a
first(rest(rest(rest(rest(head))))) // b
...

But this is really ugly. Any better solutions?


Solution

user1689607 came up with a good solution which I have encapsulated to hide some of the implementation:

var def = function(name, impl) {
    var value;
    return value = impl.apply(Object.defineProperty({}, name, {
       'value': function() { return value.apply(this, arguments); }
    }));
};

function List(f, r) {
    return function(){ return [f, r]; };
}

function first(l){ return l()[0]; }
function rest(l){ return l()[1]; }

var circle = def('head', function() {
    return List('a', List('b', List('c', this.head)));
});

first(circle); // 'a'
first(rest(circle)); // 'b'
first(rest(rest(circle))); // 'c'
first(rest(rest(rest(circle)))); // 'a'
first(rest(rest(rest(rest(circle))))); // 'b'

One more update, I ended up going with passing the self reference explicitly instead of changing the scope:

var def = function(impl) {
    var value;
    return (value = impl(function() { return value.apply(this, arguments); }));
};

var circle = def(function(self) {
    return List('a', List('b', List('c', self)));
});

This code is used in parse.js.

  • 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-14T17:24:13+00:00Added an answer on June 14, 2026 at 5:24 pm

    This is what you want?

    var headCaller = function() { return head.apply(this, arguments); };
    
    var head = List('a', List('b', List('c', headCaller)));
    

    It gives the result you seem to want…

    DEMO: http://jsfiddle.net/ruNY3/

    var results = [
        first(head), // a
        first(rest(head)), // b
        first(rest(rest(head))), // c
        first(rest(rest(rest(head)))), // a
        first(rest(rest(rest(rest(head))))) // b
    ];
    

    [
        "a",
        "b",
        "c",
        "a",
        "b"
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written my own linked-list type data structure, and would like to create an
I need a data structure of some sort to do the following: One set
I need to implement a bag data structure using the interface java.util.Collection . I'm
Simple example: I have VM with hierarchical item structure and I have designed View
I'm building an application and need a data structure of interconnected objects that can
I need help to design the best data structure for a GRAPH. The following
I've defined a DataTemplate in a ResourceDictionary. The template need some data (for populating
I need to define a Structuring Element of a mask size 30 by 105
I need to define the constant in the module that use the method from
I need to define a Wix file component that may not exist in certain

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.