Skip to content

Instantly share code, notes, and snippets.

@Kontinuation
Created October 9, 2013 13:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kontinuation/6901540 to your computer and use it in GitHub Desktop.
Save Kontinuation/6901540 to your computer and use it in GitHub Desktop.
Some scheme-like routines in C++
#include <iostream>
#include <functional>
using namespace std;
template<typename _CAR,typename _CDR>
pair<_CAR,_CDR> cons(_CAR car,_CDR cdr)
{
return std::make_pair(car,cdr);
}
template<typename _CAR,typename _CDR>
_CAR car(const pair<_CAR,_CDR>& cons)
{
return cons.first;
}
template<typename _CAR,typename _CDR>
_CDR cdr(const pair<_CAR,_CDR>& cons)
{
return cons.second;
}
template <typename _CAR, typename ... _REST>
struct _list_type {
typedef pair<_CAR, typename _list_type<_REST...>::type> type;
};
template <typename _CAR>
struct _list_type <_CAR> {
typedef pair<_CAR, nullptr_t> type;
};
template <typename _CAR>
pair<_CAR, nullptr_t> list (_CAR car) {
return make_pair(car, nullptr);
}
template <typename _CAR, typename ... _REST>
typename _list_type<_CAR, _REST ...>::type list (_CAR car, _REST ... rest)
{
return cons(car, list(rest ...));
}
int main(int argc, char *argv[])
{
auto l = list(1, "blake", 3.1415, 4.3f, 5);
cout << car(l) << endl;
cout << car(cdr(l)) << endl;
cout << car(cdr(cdr(l))) << endl;
cout << car(cdr(cdr(cdr(l)))) << endl;
cout << car(cdr(cdr(cdr(cdr(l))))) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment