小编Has*_*ase的帖子

只要数组C++,就可以在一个语句中比较布尔变量

假设我有一个布尔数组,其中5个bool变量都为真

bool boo[10];
for(int i = 0; i < 5; i++){
    boo[i] = true;
}
Run Code Online (Sandbox Code Playgroud)

我希望它们一次比较到一个NAND逻辑门,因为我的问题是如果我总是比较两个变量并将合并的布尔值与i + 1布尔变量进行比较.这给出了错误的结果.

    bool NANDGate(bool array[]){
    bool at;
    for(int i = 1; i < 5; i++){
        if(i == 1){
            at = !(array[i-1] && array[i]);
        }else{
            at = !(at && array[i]);
        }
    }
    return at;
}
// result here is true even though it should be false
Run Code Online (Sandbox Code Playgroud)

当我将每个变量从boo放入NAND门时,我想要的是一个正确的结果,所以这可能是这样的:

bool func(bool array[]){
// some loop
result = !(array[0] && array[1] && array[2] && array[3] && array[4]);
return …
Run Code Online (Sandbox Code Playgroud)

c++ boolean-logic c++11

13
推荐指数
4
解决办法
621
查看次数

数据构造函数haskell中的访问类型字段

我有一个问题,如何访问数据构造函数中的某些类型。假设我得到了这个代码示例

data Object = Object Type1 Type2 Type3 Type4
  deriving(Eq,Show)
type Type1 = Float
type Type2 = Bool
type Type3 = Int
type Type4 = String
Run Code Online (Sandbox Code Playgroud)

我定义了一个名为的函数

construct = Object 5.6 True 10 "World"
Run Code Online (Sandbox Code Playgroud)

我如何从构造打印某些类型,例如我想从构造打印“世界”我如何获取该信息。

Type4 construct 
Run Code Online (Sandbox Code Playgroud)

不起作用

先感谢您

syntax haskell types output

3
推荐指数
1
解决办法
1879
查看次数

Haskell类型错误 - 使用"打印"引起的模糊类型变量

所以我试着写一些Haskell,我遇到了这个让我想把头撞在墙上的问题.

printGrade points = case points of            
    points | 0 <= points && points < 50 -> 5.0
    points | 50 <= points && points < 54 -> 4.0
    points | 54 <= points && points < 58 -> 3.7      
    points | 58 <= points && points < 62 -> 3.3
    points | 62 <= points && points < 66 -> 3.0
    points | 66 <= points && points < 70 -> 2.7
    points | 70 <= points && points …
Run Code Online (Sandbox Code Playgroud)

debugging haskell

1
推荐指数
1
解决办法
198
查看次数

Haskell整数不充当整数

所以我要定义一个变量/函数,它接受两个输入并显示一系列的1和0

bin 0 0 = '0'
bin 0 1 = '1'
bin 0 2 = '1'
bin 0 3 = '0'
bin 0 4 = '1'
Run Code Online (Sandbox Code Playgroud)

现在我想创建一个bin变量的副本,只是在0 3处应该有一个1,所以我试图在一个新函数中实现这一点

changeBin w z = binNew where 
                binNew w z = '1'
                binNew x y = bin x y
Run Code Online (Sandbox Code Playgroud)

但是,如果我这样做,它会给我一个模式匹配冗余警告,当我调用changeBin 0 3时会进入循环,但是当我将函数更改为

changeBin w z = binNew where 
                binNew 0 3 = '1'
                binNew x y = bin x y
Run Code Online (Sandbox Code Playgroud)

这行得通,但我想做第一种方法,这样我就可以随时更改它而无需编写整个函数,但是我不知道为什么当我只用有效数字编写相同代码时,为什么会给我带来冗余错误

我是Haskell的新手,谢谢

感谢我对第一个功能的错误的任何帮助

haskell variable-assignment

1
推荐指数
1
解决办法
83
查看次数

Python - 只替换字符串中的字母一次

我需要弄清楚如何在 python 中只替换一次字母。

例子:

s = "a b c d b"

# change letter 'a' to "bye" and letter 'b' to "hay"
# .replace function is problematic because:

s = s.replace('a', "bye")
print(s)

# will print to "bye b c d b" now if I try to replace letter b 
# it will replace the first b of "bye" aswell thats not what I want
# output I want: "bye hay c d hay"
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏

python string python-3.x

1
推荐指数
1
解决办法
100
查看次数

在haskell中表示

所以我试图在haskell中编写一个函数,它执行以下操作:1.输入是字符串2.函数首先删除字符串的所有字母,只保留数字3.函数将字符串数转换为int数4.函数和字符串中的数字一起打印出来

我的代码直到'第3步

func str = 
    do 
    str <- filter (< 'a') str
    str2 <- map digitToInt str
    return str2
Run Code Online (Sandbox Code Playgroud)

不知何故,如果我删除第4行地图digitToInt它工作直到第2步罚款但这不起作用但我不知道这里的问题是什么

错误是与实际类型Char无法匹配的预期类型[Char]

先感谢您

haskell

0
推荐指数
1
解决办法
130
查看次数