lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


El 19/01/14 08:38, lua-l-request@lists.lua.org escribió:
Date: Sat, 18 Jan 2014 22:35:38 -0600
From: "H. Conrad Cunningham" <hcc@cs.olemiss.edu>
Subject: Re: Design questions for a small s-expression library
To: Lua mailing list <lua-l@lists.lua.org>
Message-ID:
	<CAFAZ_7OPaOQ0+d_FjNQ9O5NNXTn7_indLr+MooQ7Quvs4pPeqg@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Sat, Jan 18, 2014 at 10:38 AM, Philipp Janda <siffiejoe@gmx.net> wrote:

Am 18.01.2014 15:04 schr=F6bte Antonio Vieiro:


I would like to build a small s-expression ([1]) library in Lua (5.1),
so I need to design cons-cells ([2]) first.

I would appreciate some advice in the design of the cons-cells. I am
thinking of two approaches:

A) Design cons-cells as Lua tables, in plain Lua, with a "car" and a
"cdr" being other Lua objects (possibly other cons-cells).
B) Design cons-cells as a C structure (Lua allocated and garbage
collected with a __gc metamethod), that keeps references to other Lua
objects using luaL_ref and luaL_unref (possibly with references to other
cons-cells).


C) Use closures for cons-cells. See [3].
D) Similar to A, but use the keys `1` and `2` (i.e. the array part of the
table) for head and tail.

`cons`, `car`, `cdr`, `setcar`, and `setcdr` are the only functions that
need to know the difference, so you can even change the implementation
later ...


I have a set of example modules I created for the course I taught in my
university's Fall semester (late August to mid-December) on my website at
http://www.cs.olemiss.edu/~hcc/csci658/notes/658lectureNotes.html#cellList


Hi,

My experiments are also available here:

https://github.com/vieiro/luacons
- Lua tables: https://github.com/vieiro/luacons/tree/master/tables
- in C: https://github.com/vieiro/luacons/tree/master/in_c

However, I intentionally did not include anything like a setcar or setcdr,
instead forcing creation or a new Cons cell. I wanted immutability of the
cells.

That's a great idea! if immutability is a design criteria I think the "in C" implementation above (using C structs and Lua references) would work correctly, as cycles are not possible by design.

I'll take a look at your implementations, thanks for the links.

Kind regards,
Antonio