Created
July 28, 2018 03:06
-
-
Save cqfd/859680dbfab56d12f485fe692ae8e7b1 to your computer and use it in GitHub Desktop.
C++ experiments
This file contains 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
template <typename T> | |
struct LinkedList { | |
T head; | |
LinkedList *tail; | |
}; | |
template<typename Monoid> | |
typename Monoid::T fold(LinkedList<typename Monoid::T> *l) { | |
typename Monoid::T acc = Monoid::mempty(); | |
while (l) { | |
acc = Monoid::mappend(acc, l->head); | |
l = l->tail; | |
} | |
return acc; | |
}; | |
struct IntMonoid { | |
using T = int; | |
static T mempty() { return 0; } | |
static T mappend(T x, T y) { return x + y; } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment