Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active March 30, 2020 05:46
Show Gist options
  • Save rcook/3f4e02543c09e24e4b0e to your computer and use it in GitHub Desktop.
Save rcook/3f4e02543c09e24e4b0e to your computer and use it in GitHub Desktop.
#pragma once
template<typename T>
class Cons
{
public:
const T &value() const
{ return m_value; }
const Cons<T> *next() const
{ return m_next; }
public:
static const Cons *nil()
{ return nullptr; }
const Cons *cons(T value) const
{ return new Cons<T>(value, this); }
private:
Cons(T value, const Cons<T> *next)
: m_value(value), m_next(next) { }
private:
const T m_value;
const Cons<T> * const m_next;
Cons() = delete;
Cons(const Cons &) = delete;
Cons(Cons &&) = delete;
~Cons() = delete;
Cons &operator=(const Cons &) = delete;
Cons &operator=(Cons &&) = delete;
};
#include "Cons.h"
#include "Util.h"
int main()
{
auto l0 = Cons<int>::nil()->cons(613)->cons(69)->cons(42);
auto l1 = l0->cons(1)->cons(2)->cons(3);
auto l2 = l0->cons(10)->cons(20)->cons(30);
auto l3 = Cons<const Cons<int> *>::nil()->cons(l0)->cons(l1)->cons(l2);
cout << length(l0) << endl << toString(l0) << endl;
cout << length(l1) << endl << toString(l1) << endl;
cout << length(l2) << endl << toString(l2) << endl;
cout << length(l3) << endl << toString(l3) << endl;
return 0;
}
The MIT License (MIT)
Copyright (c) 2014 Richard Cook
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once
template<typename T>
int length(const Cons<T> *head)
{
return head == nullptr ?
0 :
1 + length(head->next());
}
template<typename T>
void toStringHelper(const Cons<T> *obj, string &acc)
{
if (obj == nullptr)
{
return;
}
else
{
acc.append(toString(obj->value()));
if (obj->next() != nullptr)
{
acc.append(", ");
}
toStringHelper(obj->next(), acc);
return;
}
}
template<typename T>
string toString(T obj);
template<typename T>
string toString(const Cons<T> *obj)
{
string acc("[");
toStringHelper(obj, acc);
acc.append("]");
return acc;
}
template<>
string toString(int obj)
{
return to_string(obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment