我正在编写一个函数来检查树是否是BST.我所尝试的只是按顺序遍历列表打印树,然后检查列表是否在增加.但是我遇到了这个错误:
Couldn't match expected type `a' against inferred type `[t]'
`a' is a rigid type variable bound by
the type signature for `checkList' at BST.hs:24:18
In the pattern: x : y : xs
In the pattern: [x : y : xs]
In the definition of `checkList':
checkList [x : y : xs] = x <= y && checkList (y : xs)
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止(只有一个checkList函数).
checkList :: (Ord a) => [a] -> Bool
checkList [] = True
checkList [x] = True
checkList [x:y:xs] …Run Code Online (Sandbox Code Playgroud) 我正在测试一个小程序,它基本上比较了2个输入字符串是否相同(就像strcmp那样).然而,我不断得到结果说2串不管是什么.如果有人能发现一些错误,我会很感激
int comp(char str1[], char str2[]) {
int i = 0;
while (str1[i] == str2[i]) {
if (str1[i] == '\0' || str2[i] == '\0')
break;
i++;
}
if (str1[i] == '\0' && str2[i] == '\0')
return 0;
else
return -1;
}
int main(int argc, char * * argv) {
int cmp;
char str1[1000], str2[1000];
cmp = comp(str1, str2);
if (cmp == 0)
printf("The two strings are identical.\n");
else
printf("The two strings are different.\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个BST的数据类型:
data tree a = Empty | Node a (tree a) (tree a)
deriving (Show, Read, Eq)
Run Code Online (Sandbox Code Playgroud)
我正在做一个简单的map函数来应用BST的每个元素.
treeMap :: (a -> b) -> tree a -> tree b
treeMap f (Empty) = Empty
treeMap f (Node left right) = Node (treeMap f left) (treeMap f right)
Run Code Online (Sandbox Code Playgroud)
但它给我一个错误说:
Constructor `Node' should have 3 arguments, but has been given 2
In the pattern: Node left right
In the definition of `treeMap':
treeMap f (Node left right)
= Node (treeMap f left) …Run Code Online (Sandbox Code Playgroud) 我正在测试一个小程序,它基本上比较了2个输入字符串是否相同(就像strcmp那样).
我想做类似的事情(用户在同一行输入2个所需的字符串).在这种情况下,它应该返回"两个字符串是不同的"
./a.out foo bar
Run Code Online (Sandbox Code Playgroud)
我应该这样做来读取用户的输入字符串吗?
scanf("%s %s", str1, str2);
Run Code Online (Sandbox Code Playgroud)
要么
gets(str1); gets(str2);
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止(由于某些原因似乎陷入无限循环)
int mystrcmp(char str1[], char str2[]) {
int i = 0;
while (str1[i] == str2[i]) {
if (str1[i] == '\0' || str2[i] == '\0') break;
i++;
}
if (str1[i] == '\0' && str2[i] == '\0')
return 0;
else
return -1;
}
int main(int argc, char * * argv) {
int cmp;
char str1[1000], str2[1000];
scanf("%s %s", str1, str2);
//gets(str1); gets(str2);
cmp = mystrcmp(str1, str2);
if (cmp == …Run Code Online (Sandbox Code Playgroud)