469,890 Members | 2,058 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Pairs, lists and a recursive type

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>
struct cons {
A car;
B cdr;
};

// I thought it might serve as the last pair.
template <class A>
struct cons<A, void> {
A car;
};

But I have no idea what to do next -- how to use this type. I've
redefined the basic cons to be:

template <class A, class C>
struct cons {
A car;
cons<A, C> cdr;
};

But still I have no idea how to use it. I think there must be some way
to get over the recursive nature of that type. But... it's signature
would be recursive, wouldn't it? Or maybe something's wrong with my
idea and I should re-think it?

Any ideas?
Thanks in advance.

Regards,
pfm

Oct 13 '05 #1
4 1529
Piotr Filip Mieszkowski wrote:
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>
struct cons {
A car;
B cdr;
};
You could use std::pair instead.
// I thought it might serve as the last pair.
template <class A>
struct cons<A, void> {
A car;
};
Then the second type would be different for the last element.
But I have no idea what to do next -- how to use this type. I've
redefined the basic cons to be:

template <class A, class C>
struct cons {
A car;
cons<A, C> cdr;
};

But still I have no idea how to use it. I think there must be some way
to get over the recursive nature of that type.
Use a pointer. The special 'nil' value could simply be a null pointer.
But... it's signature would be recursive, wouldn't it?
Yes.
Or maybe something's wrong with my idea and I should re-think it?


Basically, what you want to do is a singly linked list, which is similar to
std::list, but the elements are only linked in one direction.

Oct 13 '05 #2
Piotr Filip Mieszkowski wrote:
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>
struct cons {
A car;
B cdr;
};

// I thought it might serve as the last pair.
template <class A>
struct cons<A, void> {
A car;
};

But I have no idea what to do next -- how to use this type. I've
redefined the basic cons to be:

template <class A, class C>
struct cons {
A car;
cons<A, C> cdr;
};

But still I have no idea how to use it. I think there must be some way
to get over the recursive nature of that type. But... it's signature
would be recursive, wouldn't it? Or maybe something's wrong with my
idea and I should re-think it?
I think you are confused. In your scheme list of different lengths have
different types. This makes them awkward to use (to say the least)
although I'm not saying that you scheme has no uses. For general use,
and compatibility with Lisp, you first need a variant type. I.e. a type
whose value can be any of the types you are interested in (including
lists and nil).

struct Variant
{
...
};

Then you can make a cons pair like this

struct Cons
{
Variant car;
Variant cdr;
};

But it's not really the C++ way, you'll have to do quite a lot of work
to get this to what you are used to in Lisp.

john

Any ideas?
Thanks in advance.

Regards,
pfm

Oct 13 '05 #3

Piotr Filip Mieszkowski wrote:
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>
struct cons {
A car;
B cdr;
};

// I thought it might serve as the last pair.
template <class A>
struct cons<A, void> {
A car;
};

But I have no idea what to do next -- how to use this type. I've
redefined the basic cons to be:

template <class A, class C>
struct cons {
A car;
cons<A, C> cdr;
};

But still I have no idea how to use it. I think there must be some way
to get over the recursive nature of that type. But... it's signature
would be recursive, wouldn't it? Or maybe something's wrong with my
idea and I should re-think it?


Your ideas are on the right track. In fact you are well on your way to
inventing C++ template metaprogramming. :)

A "typelist" is probably close to what are trying to create. For
inspiration, you should look at the "tuple" class template the tr1
library. And if you are really ambitious, look into the metaprogramming
library (mpl) for implementations of typelists and a great many other
interesting templates. (visit boost.org for more information about
tuple or the mpl).

Greg

Oct 13 '05 #4
Thanks for your replies. I have to admit that it was a stupid idea.
Though it would be cool to be able to implement Cons in C++. (-:

Regards,
pfm

Oct 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Jon Slaughter | last post: by
2 posts views Thread by Ryan Ternier | last post: by
1 post views Thread by Girish Sahani | last post: by
6 posts views Thread by yomgui | last post: by
2 posts views Thread by Evan | last post: by
51 posts views Thread by Joerg Schoen | last post: by
3 posts views Thread by Gabriel Zachmann | last post: by
15 posts views Thread by mcjason | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.