我正在尝试为给定的布尔表达式生成真值表.我可以通过创建一个新的数据类型BoolExpr来做到这一点,但我想用匿名函数来做.它应该像这样工作:
> tTable (\x y -> not (x || y))
output:
F F | T
F T | F
T F | F
T T | F
Run Code Online (Sandbox Code Playgroud)
我的方法:
tbl p = [(uncurry p) tuple | tuple <- allval]
where allval=[(x,y) | x <- [False,True], y <- [False,True]]
Run Code Online (Sandbox Code Playgroud)
这有效,但仅适用于2个参数.我想为任意数量的参数做这件事.所以我想我会创建一个从List获取Arguments的函数:
argsFromList f [] = f
argsFromList f (x:xs) = argsFromList (f x) xs
Run Code Online (Sandbox Code Playgroud)
这不起作用:
Occurs check: cannot construct the infinite type: t = t1 -> t
Expected type: t -> [t1] -> …Run Code Online (Sandbox Code Playgroud) 函数式编程对我来说是一个新手,似乎无法理解如何使用函数作为另一个函数的参数.finalvalue应该计算一段时间后的最终值,以及2个时期后的finalvalue2.
interest :: Float -> Float -> Float
interest capital rate = capital * rate * 0.01
finalvalue :: Float -> Float -> Float
finalvalue capital rate = capital + interest capital rate
finalvalue2 :: Float -> Float -> Float
finalvalue2 capital rate = finalvalue capital rate + interest finalvalue capital rate rate
Run Code Online (Sandbox Code Playgroud)
我明白了:
Couldn't match expected type `Float'
against inferred type `Float -> Float -> Float'
In the first argument of `interest', namely `finalvalue'
In the second argument of …Run Code Online (Sandbox Code Playgroud) 我的PHP脚本有一个奇怪的问题.我有一个在脚本开头定义的数组$ keys:
$keys = array("name","date","event","location","address","description","link","linkname");
Run Code Online (Sandbox Code Playgroud)
在某些时候,我正在循环数组,尝试打印键:
foreach ($keys as $key_show) {
echo ($key_show);
}
Run Code Online (Sandbox Code Playgroud)
实际上并没有打印出来.我在循环之前放了一个var_dump($ keys),看起来这个数组在脚本的这一点上仍然填充了上面的条目.有趣的是,只要我把var_dump放在那里,键也出现在foreach循环中.
完整的脚本可以在这里看到
来自Java我试图用C++实现一个简单的Battleships游戏,但已经陷入了这个阵列:
#include <iostream>
#include <utility>
using namespace std;
class Ship{
private:
int length;
bool direction; //false = left, true = down
pair <int,int> coords[];
public:
Ship(int x, int y, bool, int);
void printship();
};
Ship::Ship(int x, int y, bool dir, int l){
pair <int,int> coords[l];
length = l;
if (dir){
for (int i = 0; i < l; i++){
coords[i] = make_pair(x, y+i);
}
}
else{
for (int i = 0; i < l; i++){
coords[i] = make_pair(x+i, y);
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用链接列表实现一个简单的堆栈.当我运行下面的代码时,我明白了
6
<some random integer>
Run Code Online (Sandbox Code Playgroud)
我一直在寻找我的错误几个小时而没有成功.我想某处有一个未经传播的变量,但我似乎无法找到它.
#include <iostream>
using namespace std;
class node {
public:
node operator = (const node&);
node(int d,node* n): data(d), prev(n) {}
node(){}
node* prev;
int data;
};
node node::operator = (const node &n) {
node r(n.data, n.prev);
return r;
}
class stack {
public:
stack();
void push(int);
int pop();
bool empty();
private:
node* top;
};
stack::stack() {
top = 0;
}
bool stack::empty() {
return top == 0;
}
void stack::push(int x) {
node n(x,top); …Run Code Online (Sandbox Code Playgroud)