(define (accumulate op initial sequence)
(if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))
(define (flatmap proc seq)
(accumulate append nil (map proc seq)))
Run Code Online (Sandbox Code Playgroud)
以上是来自 SICP 的代码片段,在 Scheme 中。为什么flatmap需要这个程序?flatmap和 和有map什么区别?
#include<iostream>
#include<deque>
struct testStr {
std::deque<int> queue;
};
testStr arr[1] = {
testStr()
};
testStr t = testStr();
void func() {
testStr s = arr[0];
s.queue.push_back(5);
t.queue.push_back(6);
}
void func2() {
std::cout << arr[0].queue.empty() << ' ';
std::cout << t.queue.empty() << '\n';
}
int main() {
func();
func2();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
1 0
Run Code Online (Sandbox Code Playgroud)
我花了一个小时来追踪这个"错误"并且不明白为什么会出现这种行为以及如何克服它.感谢这里的任何和所有帮助.