Skip to content

Instantly share code, notes, and snippets.

@m-renaud
Created November 15, 2014 06:38
Show Gist options
  • Save m-renaud/fdd92b0f36827ea1ec92 to your computer and use it in GitHub Desktop.
Save m-renaud/fdd92b0f36827ea1ec92 to your computer and use it in GitHub Desktop.
A simple problem in C++ and Haskell.
template <typename T>
bool f(const vector<vector<T>>& matrix) {
if (matrix.size() == 0) {
return true;
}
size_t m = matrix.size();
size_t n = matrix[0].size();
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
if (matrix[r][c] != matrix[m-r-1][n-c-1]) {
return false;
}
}
}
return true;
}
f :: [[a]] -> Bool
f matrix = flattenedMatrix == (reverse flattenedMatrix)
where flattenedMatrix = concat matrix
-- Note that concat (defined in the default environment) simply flattens a list of lists into a list.
concat :: [[a]] -> [a]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment