Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > LISP generalized lists in C

Reply
Thread Tools

LISP generalized lists in C

 
 
Jean-Guillaume Pyraksos
Guest
Posts: n/a
 
      04-11-2006
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      04-11-2006


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/

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
tmp123
Guest
Posts: n/a
 
      04-11-2006
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.

 
Reply With Quote
 
Duncan Muirhead
Guest
Posts: n/a
 
      04-11-2006
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


 
Reply With Quote
 
Michael Wojcik
Guest
Posts: n/a
 
      04-12-2006

In article <(E-Mail Removed). com>, "tmp123" <(E-Mail Removed)> 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 (E-Mail Removed)

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
 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      04-13-2006

Michael Wojcik wrote:
> In article <(E-Mail Removed). com>, "tmp123" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nice historical Musical - VERY RELAXING - about LISP history -fundamental ideas of LISP nanothermite911fbibustards C++ 0 06-16-2010 09:47 PM
Nice historical Musical - VERY RELAXING - about LISP history -fundamental ideas of LISP nanothermite911fbibustards Python 0 06-16-2010 09:47 PM
pat-match.lisp or extend-match.lisp in Python? ekzept Python 0 08-10-2007 06:08 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
Generalized Linear Least Squares Problems venkat Python 3 06-01-2005 08:06 PM



Advertisments