Skip to content

Instantly share code, notes, and snippets.

@viercc
Last active November 11, 2019 13:36
Show Gist options
  • Save viercc/375807102a56bc0e64fb1f70b09771e2 to your computer and use it in GitHub Desktop.
Save viercc/375807102a56bc0e64fb1f70b09771e2 to your computer and use it in GitHub Desktop.
tmp
[In Haskell]
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr = ......
(example)
foldr (+) 0 [a,b,c] = a + (b + (c + 0))
[In C++]
typedef B (*fun_a_b_to_b)(A, B);
B foldr(fun_a_b_to_b func, B zero, const vector<A> &array) {
B answer = zero;
for (i = array.size() - 1; i >= 0; i--) {
answer = func(array[i], answer);
}
return answer;
}
(example)
// A = int
// B = int
int add(int a, int b) {
return (a + b);
}
vector<int> array = {3, 5, 7};
int result = foldr(add, 0, array);
// result = 3 + (5 + (7 + 0))
// If your task were in C++...
struct Result {
// blah
};
struct Attack {
char attack_column;
int attack_row;
};
Result attackOne(Attack a, Result board) {
// blah
}
Result ships(Result board, vector<Attack> array) {
// Probably you can do it manually,
// but can you implement it using foldr( , , )?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment