Javascript recursive data structure definition
Posted
by
Matt Bierner
on Stack Overflow
See other posts from Stack Overflow
or by Matt Bierner
Published on 2012-11-10T16:44:58Z
Indexed on
2012/11/10
17:00 UTC
Read the original article
Hit count: 225
JavaScript
|recursion
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 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?
© Stack Overflow or respective owner