Created
October 9, 2013 13:44
-
-
Save Kontinuation/6901540 to your computer and use it in GitHub Desktop.
Some scheme-like routines in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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