我试图使用折叠来取得Haskell中整数之和的平方.但是,我从GHCi那里得到了一个神秘的错误.这是我的单行:
((^2) . foldl) (+) 0 [1..100]
Run Code Online (Sandbox Code Playgroud)
我从GHCi得到的是:
Prelude> ((^2) . foldl) (+) 0 [1..100]
<interactive>:19:3:
No instance for (Num (b0 -> [b0] -> b0))
arising from a use of `^'
Possible fix:
add an instance declaration for (Num (b0 -> [b0] -> b0))
In the first argument of `(.)', namely `(^ 2)'
In the expression: (^ 2) . foldl
In the expression: ((^ 2) . foldl) (+) 0 [1 .. 100]
Run Code Online (Sandbox Code Playgroud)
我认为问题出在我最后根据此类型声明传入的列表中.
Prelude> :t ((^2) . foldl) (+) 0 …Run Code Online (Sandbox Code Playgroud) 我正在使用Gforth来尝试实现取幂.从理论上讲,我理解基于堆栈的语言应该如何运作.但是,我在Gforth上实现它时遇到了困难.
这就是我现在所拥有的:
: myexp
1 swap ?do rot dup * rot rot loop ;
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,我看到一个堆栈下溢,如:
3 2 myexp
:1: Stack underflow
3 2 >>>myexp<<<
Backtrace:
$7F645EFD6EF0 rot
$2
$1
Run Code Online (Sandbox Code Playgroud)
Gforth的循环结构是否在循环时操纵堆栈?
我对Forth的工作方式一无所知,因为我在网上看到的大多数循环示例都让Foss的新人感到困惑和困惑.
我的实施有什么问题?
我在我的类中写了一个简短的静态方法Hashtable,按顺序迭代我的一些s,string但是我得到了一个奇怪的编译器错误.这是有问题的方法:
public static DictionaryEntry inorderHashtable(Hashtable ht) {
string[] keys = ht.Keys.Cast<string>().ToArray<string>();
Array.Sort(keys);
foreach (string key in keys) {
yield return new DictionaryEntry(key, ht[key]);
}
}
Run Code Online (Sandbox Code Playgroud)
这稍后在类中使用,如下所示:
foreach(DictionaryEntry dentry in inorderHashtable(myTable)) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)
这是我从VS2008得到的错误: 'ns.myclass.inorderHashtable(System.Collections.Hashtable)' cannot be an iterator block because 'System.Collections.DictionaryEntry' is not an iterator interface type
有什么方法可以解决这个错误?提前致谢.
假设在x86_64系统上运行以下程序:
int main() {
//sizeof(int) == 4
//sizeof(int*) == 8
//sizeof(long) == 8
// I would like 2 distinct memory locations to hold these two integers
int* mem1 = (int*)malloc(2 * sizeof(int));
mem1[0] = 1;
mem1[1] = 2;
//I would like 1 distinct memory location to hold this one long
long* mem2 = (long*)malloc(1 * sizeof(long));
mem2[0] = 3;
free(mem1);
free(mem2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于malloc接收要分配的字节数,因此对malloc的两次调用看起来完全相同.malloc如何知道实际分配16个字节来存储两个整数数组并且只为一个长度分配8个字节?
为清晰起见进行编辑:基于以下假设,存储这两个不同的阵列将需要为每个阵列提供不同的空间量.但是,malloc似乎每次在此程序中保留相同的空间量.然而,对于长度不同于longs 的数据类型的数组,正确地确定了数组大小.
有人可以帮我识别内存的这种理解中的缺陷,还是指出malloc/libc在后台做的事情?以下是我正在进行的假设
mem[idx]指MEM的地址加上IDX的偏移量,并在存储器中该地址不能指向数据中的另一个项目的中间(因此mem1[0]不能参考的下半字mem1和mem1[1] …我有以下程序.我想要完成的是创建一个对unordered_map的可变包装器的常量引用,我可以传递它以进行只读查找.但是,由于operator []重载,我无法编译以下代码.
从这段代码中,有谁知道我做错了什么?
#include <unordered_map>
#include <string>
using std::unordered_map;
using std::string;
class M {
private:
unordered_map<string, string> m;
public:
string const& operator[](string const& s) const {
return m[s]; // line 13
}
string& operator[](string const& s) {
return m[s];
}
};
int main() {
M m;
m[string("a")] = string("answer_a");
M const& m1 = m;
string const& test = m1[string("a")];
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误(第13行)是
错误:将'const std :: unordered_map,std :: basic_string>'作为'this'参数传递给'std :: __ detail :: _ Map_base <_Key,_Pair,std :: _ Select1st <_Pair>,true,_Hashtable> …