From: Yuan-chi Chiu
Subject: Lisp like C libraries
Date: 
Message-ID: <2su0p3$cb3@pandora.sdsu.edu>
Hello Lisp,

   I have spend the last 3-4 monthes programming in Lisp and now I really
like the language.  One thing that impress me most is it hidden link list
manipulations.

   This summer I need to program in C (or C++).  What would be a good way
to implement the equivelent of Lisp's cons, append, car, cdr?

   Also, can I pass functions as function argument?  How to do it?

Thanks for any help!!
--
+-------------------------------------------------------------------------+
|  Yuan-chi (Bill) Chiu.    ·····@ucssun1.sdsu.edu.    Amiga Enthusiast.  |
|-------------------------------------------------------------------------|
| A process cannot be understood by stopping it.  Understanding must move |
| with the flow of the process, must join it and flow with it.            |
+-------------------------------------------------------------------------+

From: David Gadbois
Subject: Re: Lisp like C libraries
Date: 
Message-ID: <2t0d76$if7@peaches.cs.utexas.edu>
Yuan-chi Chiu <·····@ucssun1.sdsu.edu> wrote:
>
>This summer I need to program in C (or C++).  What would be a good
>way to implement the equivelent of Lisp's cons, append, car, cdr?

Check out some of the implementations of Scheme in C.  Details of
where to get them are in the FAQ.  SIOD is a good one to look at since
it is so small.  The general idea is that you attach a tag (perhaps
via an extra struct field) to the relevant data objects so you can
identify what they are at runtime.

>Also, can I pass functions as function argument?  How to do it?

That is hard to do portably in C in the general case and impossible to
do very transparently.  The problem is that C does not have nested
functions, so you have to roll your own closures.

--David Gadbois
From: Yuan-chi Chiu
Subject: Re: Lisp like C libraries
Date: 
Message-ID: <2t2t8i$ec0@pandora.sdsu.edu>
David Gadbois (·······@cs.utexas.edu) wrote:

: Check out some of the implementations of Scheme in C.  Details of
: where to get them are in the FAQ.  SIOD is a good one to look at since
: it is so small.  The general idea is that you attach a tag (perhaps
: via an extra struct field) to the relevant data objects so you can
: identify what they are at runtime.

David, or anyone who is reading this... Could you please point me at the
right direction where Lisp like C functions can be found on the internet?

Preferably if the implementation use the similar keywords as the Lisp
counterparts.

Thanks!!

--
+-------------------------------------------------------------------------+
|  Yuan-chi (Bill) Chiu.    ·····@ucssun1.sdsu.edu.    Amiga Enthusiast.  |
|-------------------------------------------------------------------------|
| A process cannot be understood by stopping it.  Understanding must move |
| with the flow of the process, must join it and flow with it.            |
+-------------------------------------------------------------------------+
From: Barry Margolin
Subject: Re: Lisp like C libraries
Date: 
Message-ID: <2td8atINNivp@early-bird.think.com>
In article <··········@pandora.sdsu.edu> ·····@ucssun1.sdsu.edu (Yuan-chi Chiu) writes:
>   This summer I need to program in C (or C++).  What would be a good way
>to implement the equivelent of Lisp's cons, append, car, cdr?

Doing this in a fully general way, as Lisp does, would be very tedious and
error-prone.  You'd need an "object" union and then a struct containing two
pointers to this union, something like:

typedef enum Type_enum { INT, CONS, FLOAT, ... } Type;
struct Object_struct;
typedef struct Cons_struct {
  struct Object_struct *car;
  struct Object_struct *cdr;
}
typedef struct Object_struct {
  Type type_code;
  union {
   int i;
   float f;
   struct Cons_struct c;
   ...
  }
} Object;

This isn't a very efficient representation.  Also, you'll have to do all
your own memory management, since C doesn't have a garbage collector.

>   Also, can I pass functions as function argument?  How to do it?

This is done in C using function pointers.  Any C reference or textbook
should describe this.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

······@think.com          {uunet,harvard}!think!barmar