我在使这个功能工作时遇到了很多麻烦:
concatenate :: [String] -> String
Run Code Online (Sandbox Code Playgroud)
它被设计为简单地获取字符串列表并返回单个字符串,该字符串是列表中每个元素从头到尾连接的结果.我想留在内部map,foldl和foldr功能.我觉得我知道这些函数的概念做得很好,但我遇到的最常见问题是我遇到类型冲突.例如,GHC会期待一个[Char],并且在我不知情的情况下,我会放入显然试图使用[[Char]]的代码.
例如: concatenate (x:xs) = foldr (++) x (concatenate xs)
我得到以下编译错误:
Couldn't match type `Char' with `[Char]'
Expected type: [[Char]]
Actual type: String
In the return type of a call of `concatenate'
In the third argument of `foldr', namely `(concatenate xs)'
In the expression: foldr (++) x (concatenate xs)
Run Code Online (Sandbox Code Playgroud)
我对Haskell 很新,所以请随意笑.只要还包括适合新手的解释,预计会受到苛刻,并受到欢迎.感谢您的帮助.
我正在阅读关于异常和断言的章节中的Java教科书,并且遇到了这个代码块,我有一个问题.
public boolean searchFor(String file, String word)
throws StreamException
{
Stream input = null;
try {
input = new Stream(file);
while (!input.eof())
if (input.next().equals(word))
return true;
return false; //not found
} finally {
if (input != null)
input.close();
}
}
Run Code Online (Sandbox Code Playgroud)
在下一段中,文本说"该searchFor方法声明它抛出,StreamException以便生成的任何异常在清理后传递给调用代码,包括StreamException调用close所引发的任何异常.
我的印象是,包含一个throws子句允许程序员抛出异常的特定类(或子类),并且当且仅当它或它的一个超类在throws子句中时,才能抛出一个类.但是在这里,有一个throws条款没有throw声明try.那么首先包括该条款有什么意义呢?在代码中的位置会StreamException被捕获?
假设我有这个星号列表,我说它以这种方式打印:
list = ['* *', '*', '* * *', '* * * * *', '* * * * * *', '* * * *']
for i in list:
print i
Run Code Online (Sandbox Code Playgroud)
所以在这里,输出是:
* *
*
* * *
* * * * *
* * * * * *
* * * *
Run Code Online (Sandbox Code Playgroud)
但我希望输出是垂直的,如下所示:
* * * * * *
* * * * *
* * * *
* * *
* *
*
Run Code Online (Sandbox Code Playgroud)
这样做的任何提示?我试图概念化如何使用list comprehension或for-loops这样的东西,但是没有把它弄得很正确.
这很难解释,因为整个代码有一个相当大的背景细节,需要知道才能真正了解我所说的功能.但是我会尽力让我的主要观点得到解决,并希望它足够了.如果没有,请告诉我,我会添加更多信息.所以:
我有一个Haskell函数eval :: WExp -> Memory -> WValue,它有一堆不同的自身实例用于不同的情况.现在,有关的知识WExp,Memory和WValue是不相关的.我的问题是,对于特定的实例eval,我使用的是一个lookup函数,它接受eval(在这种情况下是一个字符串)的参数搜索该字符串的键值对列表.请注意,此lookup功能不是Prelude中包含的功能; 它是在.hs文件中自定义的.如果找到该字符串,则返回与其关联的值,但如果未找到,Nothing则返回该值.由于的Nothing情况下,该类型的lookup其实是Maybe a,这里a将是一个WValue在这种情况下.因为eval会返回a Maybe WValue,编译器显然会抱怨类型不是WValue.
同样,如果您需要有关这些其他类型的更多信息,我可以提供.我只想到可能有某种通用方法a从任何返回的函数中提取值Maybe a.如果没有,我想我会在别处寻找解决方案:)
我在这里有这段代码:
class physics_vector
{
public:
double direction, magnitude;
int dir_mag(double dir, double mag) :direction(dir),
magnitude(dir) {return 0; };
};
int dir_mag(double dir, double mag)
{
cout << "Direction: " << dir << '\n';
cout << "Magnitude: " << mag << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
每当我尝试编译时,我都会收到错误消息,
13:39: error: only constructors take member initializers
Run Code Online (Sandbox Code Playgroud)
请问有什么帮助吗?
我在类中使用下面的代码Triangle来允许用户设置声明的第一点、第二点或第三点Triangle。
public Point get(String p) throws IllegalArgumentException {
IllegalArgumentException e = new IllegalArgumentException();
try {
if (p == "first") { return first; }
else if (p == "second") { return second; }
else if (p == "third") { return third; }
else { throw e; }
}
catch (e) {
System.out.println("Error: " + e);
}
}
Run Code Online (Sandbox Code Playgroud)
编译器告诉我:
Triangle.java:41: error: missing return statement
}
^
Run Code Online (Sandbox Code Playgroud)
但我认为 catch 语句的重点是能够捕获错误并返回描述错误的字符串,而不必担心匹配函数的返回类型。
我有这个初步代码:
#include "std_lib_facilities_4.h"
void numbers()
{
vector<int> first(9);
for (int i = 0; i <= 9; ++i)
{
cout << i << endl;
first.push_back(i);
}
for (int j = 0; j <= 9; ++j)
cout << first[j];
}
int main()
{
numbers();
}
Run Code Online (Sandbox Code Playgroud)
当我打印每个元素时,我希望得到1号,2号,3号,4号,5号,6号,7号,8号和9号打印输出.相反,我得到了这个:
0
1
2
3
4
5
6
7
8
9
0000000000
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?