Pairs, lists and a recursive type

  • Thread starter Piotr Filip Mieszkowski
  • Start date
P

Piotr Filip Mieszkowski

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
 
R

Rolf Magnus

Piotr said:
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.
 
J

John Harrison

Piotr said:
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
 
G

Greg

Piotr said:
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
 
P

Piotr Filip Mieszkowski

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top