我问这个是因为我的程序有两个函数来乘法矩阵,它们只乘以4x4和4x1矩阵.标题是:
double** mult4x1(double **m1, double **m2);
double** mult4x4(double **m1, double **m2);
Run Code Online (Sandbox Code Playgroud)
它们执行m1*m2并将其返回**两倍,下面是4x4乘法的片段.
double** mult4x4(double **m1, double **m2){
double** result = (double**) malloc(sizeof(double)*4);
for (int i = 0; i < 4; i++) {
result[i] = (double*) malloc(sizeof(double)*4);
}
...multiply...
return result;
}
Run Code Online (Sandbox Code Playgroud)
mult4x1和mult4x4之间的区别仅在于它们内部使用的索引.
我有这3个矩阵:
double m1[4][4] = {
{2, 3, 5, 6},
{9, 8, 1, 7},
{5, 4, 3, 1},
{7, 6, 1, 2}
};
double m2[4][4] = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0}, …Run Code Online (Sandbox Code Playgroud) 例如,由于以下函数没有累加器,它是否仍然是尾递归的?
belong:: (Ord a) => a -> [a] -> Bool
belong a [] = False
belong a (h:t)
| a == h = True
| otherwise = belong a t
Run Code Online (Sandbox Code Playgroud)
在递归调用之前处理函数中的所有计算,它是否被认为是尾递归的充分条件?
为了对我的问题进行语境化,我使用的Matrix类具有以下定义:
Matrix(unsigned int, unsigned int); // matrix of the given dimension full of zeroes
Matrix(Matrix*); // creates a new matrix from another one
int &operator()(int, int); // used to access the matrix
int **matrix; // the matrix
Run Code Online (Sandbox Code Playgroud)
现在拿这两个代码片段:
第一:
Matrix test(4,4);
Matrix ptr = test;
ptr(0,0) = 95;
Run Code Online (Sandbox Code Playgroud)
第二:
Matrix test(4,4);
Matrix *ptr = &test;
(*ptr)(0,0) = 95;
Run Code Online (Sandbox Code Playgroud)
两个代码都有相同的效果,(0,0)位置的元素接收95(第一个代码片段与Java非常相似,这是导致我提出这个问题的原因).问题是,两种方式都正确地分配了对象吗?
我正在做一个程序来将所有奇数加到n:
oddSum' n result | n==0 = result
| otherwise = oddSum' (n-1) ((mod n 2)*(n)+result)
oddSum n = oddSum' n 0
Run Code Online (Sandbox Code Playgroud)
我为我的输入获得了两个错误(我把它们放在下面),我正在使用尾递归,为什么堆栈溢出发生?(注意:我在Ubuntu上使用Hugs)
oddSum 20000 ERROR - 控制堆栈溢出
oddSum 100000 ERROR - 垃圾收集无法回收足够的空间
我正在学习尾递归,我在确定我的函数是否是尾递归方面遇到了一些困难(主要是关于我使用其他函数的函数).
我已经实现了以下两个函数,但我不确定它们是否是尾递归的.
第一个是连接两个列表的函数.
conca list [] = list
conca [] result = result
conca (h:t) result = conca (init (h:t)) ( last(h:t):result )
concatenate::[a]->[a]->[a]
concatenate list1 list2 = conca list1 list2
Run Code Online (Sandbox Code Playgroud)
函数中的计算在递归调用之前处理,但是它使用last和init,它们不是尾递归的(我在http://ww2.cs.mu.oz.au/172/Haskell/tourofprelude中检查了它们的定义.html)
第二个功能是删除给定列表中给定数字的第一次出现.
invert [] result = result
invert (h:t) result = invert t (h:result)
remov n [] aux result = invert result []
remov n (h:t) aux result
| aux==1 = remov n t 1 (h:result)
| n==h = remov n t 1 (result)
| …Run Code Online (Sandbox Code Playgroud) 我在Haskell中有两个相同的代码,都必须在给定位置(参数n)拆分列表,但是当一个工作时另一个不工作,为什么会发生这种情况?
divide [] _ = ([],[])
divide (h:t) n
| n>0 = ( h:list1 , list2 )
| otherwise = ([],h:t)
where (list1, list2) = divide t (n-1)
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,但下面的代码没有.
divide [] _ = ([],[])
divide (h:t) n
| n>0 = ( h:( divide t (n-1) ) , divide t (n-1) )
| otherwise = ([],h:t)
Run Code Online (Sandbox Code Playgroud)
ghci给出以下消息:
divide.hs:3:29:
Run Code Online (Sandbox Code Playgroud)Couldn't match expected type '[a0]' with actual type '([a0], [a1])' In the return type of a call of 'divide' In the second argument …
我试图将一个字符串拆分成一个字符串数组,问题是.split()返回一个空元素.("test").split会回来的["","t","e","s","t"].
此问题中的解决方案将字符串拆分为字符串数组解决了问题(使用.split("(?!^)")).
但是我仍然无法理解为什么会这样,并且我不会使用一段我无法理解的代码,因为它完成了工作.
我读过这两页http://www.regular-expressions.info/lookaround.html和http://ocpsoft.org/opensource/guide-to-regular-expressions-in-java-part-2/约负面的预测仍然无法理解.有人可以澄清一下吗?