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 4 1703
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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 collection that I need.
To begin, I am using xsltproc that conmes with Cygwin as my processor.
I have no particular affinity to this processor except that it is open
source and standards compliant. I don't like M$, but if using a M$
processing...
|
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
{
|
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
|
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
multiple lists such that each list must have one and only one instance of
every object.
Thus, for the above list, my output should be:
,]
Another example: Let l = . Then
|
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 single list of all the elemets
of type A ?
thanks
| |
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=
|
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 for
linked lists, I couldn't find one. I read something about Mcilroy's
"Optimistic Merge Sort" and studied some implementation, but they
were for arrays. Does anybody know if Mcilroys optimization is applicable to
truly linked lists at all?
|
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 =
|
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 do
together overlapping common pieces but say connected each working out
as connected, but together as connected it's connected with the others
connected. a whole machine where connected together is a condition of
the machine together as...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| | |