473,802 Members | 1,960 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LISP generalized lists in C

I want to work with "generalize d 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
5 3091


Jean-Guillaume Pyraksos wrote On 04/11/06 10:00,:
I want to work with "generalize d 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
Jean-Guillaume Pyraksos wrote:
I want to work with "generalize d 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
On Tue, 11 Apr 2006 16:00:32 +0200, Jean-Guillaume Pyraksos wrote:
I want to work with "generalize d 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

In article <11************ *********@i40g2 000cwc.googlegr oups.com>, "tmp123" <tm****@menta.n et> writes:
Jean-Guillaume Pyraksos wrote:
I want to work with "generalize d 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

Michael Wojcik wrote:
In article <11************ *********@i40g2 000cwc.googlegr oups.com>, "tmp123" <tm****@menta.n et> writes:
Jean-Guillaume Pyraksos wrote:
I want to work with "generalize d 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
8086
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities as a standard portable part of the core language, the LISP package, and the java.lang package, respectively. Both have big integers, although only LISP has rationals as far as I can tell. Because CL supports keyword arguments, it has a wider range...
699
34272
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
10
2024
by: mike420 | last post by:
Earlier Ed Schofield (thanks, man) warned us that flist = for i in range(3) f = lambda x: x + i flist.append(f)
82
5396
by: nobody | last post by:
Howdy, Mike! mikecoxlinux@yahoo.com (Mike Cox) wrote in message news:<3d6111f1.0402271647.c20aea3@posting.google.com>... > I'm a C++ programmer, and have to use lisp because I want to use > emacs. I've gotten a book on lisp, and I must say lisp is the ugliest > looking language syntax wise. What is up with this: (defun(foo()). (DEFUN FOO () NIL) > What were the lisp authors thinking? Why did Stallman use lisp in
32
2016
by: nobody | last post by:
This article is posted at the request of C.W. Yang who asked me to detail my opinion of Lisp, and for the benefit of people like him, who may find themselves intrigued by this language. The opinions expressed herein are my personal ones, coming from several years of experience with Lisp. I did plenty of AI programming back in the day, which is what would now be called "search" instead.
761
28981
by: Neo-LISPer | last post by:
Hey Recently, I researched using C++ for game programming and here is what I found: C++ game developers spend a lot of their time debugging corrupted memory. Few, if any, compilers offer completely safe modes. Unsurprisingly, there is a very high failure rate among projects using C++ for modern game development.
4
1703
by: Piotr Filip Mieszkowski | last post by:
Hello, I like both C++ and Lisp and sometimes try to mix their ideas in the code I write. Recently I started to think about writing a pair class similar to the CONS in Lisp. (For those of you who don't know Lisp: lists in Lisp are constructed of pairs whose second element is always the next pair or NIL, a special symbol.) So I've tried something like this: template <class A, class B>
852
28787
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for my general education. Mark
3
1369
by: | last post by:
I am enjoying making generalized methods to serve common needs, such as: ImageProcessing.MakeQuickResize(); ImageProcessing.Sharpen(); FileOps.CreateYearAndMonthAndDayDirectoryBasedOnDate() Sender.EmailCommaDelimitedList() What would be really cool is if I could make a generalized function that would accept other methods as parameters, and would perform the methods on another parameterized item that's fed to the function. So we might...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9114
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.