473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1674
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
2
by: Ryan Ternier | last post by:
I'm currently run into a snag on one of my projects. We need to create an ordered list (Mutli levels). Ie: 1. Some Title ....A....Something here ....B....Something Else 2. Another Title
1
by: Girish Sahani | last post by:
hello ppl, Consider a list like . Here 'a','b','c' are objects and 1,3,4,2 are their instance ids and they are unique e.g. a.1 and b.1 cannot exist together. From this list i want to generate...
6
by: yomgui | last post by:
Hi, I have a list of data (type A) my list can includes element of type A or a lists, these list can includes element of type A or a lists, and so on ... is there a simple way to obtain a...
2
by: Evan | last post by:
Is there a simple way to to identify and remove matching pairs from 2 lists? For example: I have a= b=
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
3
by: Gabriel Zachmann | last post by:
Well, could some kind soul please explain to me why the following trivial code is misbehaving? #!/usr/bin/python lst = s =
15
by: mcjason | last post by:
I saw something interesting about a grid pair puzzle problem that it looks like a machine when you find each that work out the way it does and say with all the others that work out the way they...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.