By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,757 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,757 IT Pros & Developers. It's quick & easy.

LISP generalized lists in C

P: n/a
I want to work with "generalized lists" as in Lisp, say :

((23 () . 2) () ((10))) ; only pointers and integers

So the basic element is a node, a struct with two fields car and cdr.
Each of these fields can contain either a pointer (NULL or a pointer to
another node), or an int.

I want to define the functions cons, car and cdr of Lisp.

I tried this but gcc rejects it :

#include <stdio.h>
#include <stdlib.h>

typedef struct {
object car;
object cdr;
} node;

typedef struct {
int tag; // 0==int, 1==node*
union {
int value;
node *ptr;
} val;
} object;

node* cons(object a, object b) { ...}

Can you help ? Thanks...

JG
Apr 11 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a


Jean-Guillaume Pyraksos wrote On 04/11/06 10:00,:
I want to work with "generalized lists" as in Lisp, say :

((23 () . 2) () ((10))) ; only pointers and integers

So the basic element is a node, a struct with two fields car and cdr.
Each of these fields can contain either a pointer (NULL or a pointer to
another node), or an int.
[...]


This is Question 1.15 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.c-faq.com/

--
Er*********@sun.com

Apr 11 '06 #2

P: n/a
Jean-Guillaume Pyraksos wrote:
I want to work with "generalized lists" as in Lisp, say : [...]

See some inserted comments:

typedef struct {
object car;
object cdr;
} node;

In Lisp, the cdr of a list is a list, not an object. Thus:

object cdr => node *cdr;

that can be written in C like:

typedef struct node {
object car;
struct node *cdr;
} node;

typedef struct {
int tag; // 0==int, 1==node*
union {
int value;
node *ptr;
} val;
} object;

"object" must be declared before "node", because "node" uses it
node* cons(object a, object b) { ...}

"cons" in Lisp takes a list and an object, not two objects. And in C
better not to pass structures, but pointers to structures:

node *cons (object *a, node *b) { ... }
Kind regards.

Apr 11 '06 #3

P: n/a
On Tue, 11 Apr 2006 16:00:32 +0200, Jean-Guillaume Pyraksos wrote:
I want to work with "generalized lists" as in Lisp, say :

((23 () . 2) () ((10))) ; only pointers and integers

So the basic element is a node, a struct with two fields car and cdr.
Each of these fields can contain either a pointer (NULL or a pointer to
another node), or an int.

I want to define the functions cons, car and cdr of Lisp.

I tried this but gcc rejects it :

#include <stdio.h>
#include <stdlib.h>

typedef struct {
object car;
object cdr;
} node;

typedef struct {
int tag; // 0==int, 1==node*
union {
int value;
node *ptr;
} val;
} object;

node* cons(object a, object b) { ...}

Can you help ? Thanks...

JG


This compiles (gcc 3.4)
typedef struct
{
int tag; /* 0==int, 1==node* */
union {
int value;
struct node_tag *ptr;
} val;
} object;

typedef struct node_tag
{
object car;
object cdr;
} node;
Duncan
Apr 11 '06 #4

P: n/a

In article <11*********************@i40g2000cwc.googlegroups. com>, "tmp123" <tm****@menta.net> writes:
Jean-Guillaume Pyraksos wrote:
I want to work with "generalized lists" as in Lisp, say :


In Lisp, the cdr of a list is a list, not an object.


[OT] The cdr of a cons cell in a list is, by definition, a list
(possibly nil), but in general the cdr of a cons cell is not
necessarily a list. See "dotted pair". To implement general
LISP-style cons cells in C, you'd have to allow for cdrs that are
not pointers to other cons cells.

Thus:

[1]> (cons 'a 'b)
(A . B)
[2]> (cdr (cons 'a 'b))
B

--
Michael Wojcik mi************@microfocus.com

Auden often writes like Disney. Like Disney, he knows the shape of beasts --
(& incidently he, too, might have a company of artists producing his lines) --
unlike Lawrence, he does not know what shapes or motivates these beasts.
-- Dylan Thomas
Apr 12 '06 #5

P: n/a

Michael Wojcik wrote:
In article <11*********************@i40g2000cwc.googlegroups. com>, "tmp123" <tm****@menta.net> writes:
Jean-Guillaume Pyraksos wrote:
I want to work with "generalized lists" as in Lisp, say :


In Lisp, the cdr of a list is a list, not an object.


[OT] The cdr of a cons cell in a list is, by definition, a list
(possibly nil), but in general the cdr of a cons cell is not
necessarily a list. See "dotted pair". To implement general
LISP-style cons cells in C, you'd have to allow for cdrs that are
not pointers to other cons cells.

Thus:

[1]> (cons 'a 'b)
(A . B)
[2]> (cdr (cons 'a 'b))
B


Thanks for the clarification. I didn't known it.

Apr 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.