标签: boolean-expression

Prolog一阶逻辑 - 打印真值表

我必须编写打印表达式真值表的程序.所以,我写了以下函数:

bool(true).
bool(fail).

tableBody(A,B,E) :-
    bool(A),
    bool(B) ,
    write(A) ,
    write('    '),
    write(B),
    write('    '),
    write(E),nl, fail.
Run Code Online (Sandbox Code Playgroud)

我的问题是E(包含A和B的表达式)没有被评估,而是按原样打印.例如:

296 ?- table(A,B,and(A,B)).
A    B    expr(A,B)
true    true    and(true, true)
true    fail    and(true, fail)
fail    true    and(fail, true)
fail    fail    and(fail, fail)
false.
Run Code Online (Sandbox Code Playgroud)

我有兴趣编写and(true, true)(" and(X,Y)"是我之前定义的仿函数)的评估值,而不是当前显示的值.我想过编写一个eval仿函数,但它不会产生同样的效果吗?我怎么解决这个问题?

我正在使用SWI-Prolog 5.8.谢谢.

prolog boolean-expression truthtable

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

布尔表达式,为什么只有两个术语?

鉴于它的写作是有效的

a = b = c = 2;
Run Code Online (Sandbox Code Playgroud)

它也会很好,而不是

bool allTwo = a == 2 && b == 2 && c == 2;
Run Code Online (Sandbox Code Playgroud)

改为写

bool allTwo = a == b == c == 2;
Run Code Online (Sandbox Code Playgroud)

但我不能,因为a == b计算到一个布尔值,然后不能将其与整数进行比较.

是否有语言设计原因以这种方式实现?

c# language-features boolean-expression

4
推荐指数
2
解决办法
373
查看次数

C#解析"(真实和真实)或(真或假)"

C#:我有一个字符串变量,如下所示:

 string a = "(true and true) or (true or false)";
Run Code Online (Sandbox Code Playgroud)

这可以是任何东西,它可以变得更复杂,如:

 string b = "((true and false) or (true or false) and not (true and false)) and false";
Run Code Online (Sandbox Code Playgroud)

我所知道的是它是正确的.不能发生这种表达不能被"评估".

有什么方法可以以某种方式评估这个吗?我只想知道该字符串的结果(结果).这意味着我需要"true"或"false"而不是此字符串.

我想我可以做一个解析方法来做到这一点,逐步减少字符串,直到我们得到最终值,但我想知道是否有更好的方法.

c# string eval boolean-expression

4
推荐指数
2
解决办法
2385
查看次数

SSIS条件拆分布尔结果

在条件拆分组件中,我需要知道Age是否等于-1并使用以下语句. 在此输入图像描述

但是,运行时会出现错误.我该如何修改我的陈述?这是一个loooooooot!

Error: The expression "Age == -1" on "output "Unknown" (12743)" evaluated to NULL, but the "component "Age Conditional Split 1" (12740)" requires a Boolean results. Modify the error row disposition on the output to treat this result as False (Ignore Failure) or to redirect this row to the error output (Redirect Row).  The expression results must be Boolean for a Conditional Split.  A NULL expression result is an error.
Run Code Online (Sandbox Code Playgroud)

conditional ssis split boolean-expression

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

Python或字符串中的布尔表达式?始终返回'True'

我正在尝试基于(不可预测的)用户输入构建布尔表达式.我发现我正在构建一个看起来合适但不起作用的字符串.我看过python.org,Google和Stackoverflow,但在这里找不到什么问题.

代码示例:

    print stringDing
    newVariable = stringDing.replace('False','0')
    print newVariable
    print bool(newVariable)
Run Code Online (Sandbox Code Playgroud)

输出来自:

    False or False or False
    0 or 0 or 0
    True
Run Code Online (Sandbox Code Playgroud)

然而,当字符串粘贴到python中时,python会按预期响应:

    >>> False or False or False
    False
Run Code Online (Sandbox Code Playgroud)

我认为我需要将其构建为一个字符串,因为用户可以添加"OR","AND"和括号,我需要在适当的位置放入.

如何进行?

python boolean-expression

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

为什么我没有得到布尔值?

>>> 'a' in 'aeiou' or 'steve'
True
>>> 'S' in 'Sam' and 'Steve'
'Steve'
>>> 'a' in 'aeiou' and 'steve'
'steve'
>>> 's' in 'aeiou' or 'AEIOU'
'AEIOU'
Run Code Online (Sandbox Code Playgroud)

我正在为一些学生上课,并对最后三个输出感到惊讶.我期待一个布尔值.任何人都可以对此有所了解吗?

python operators boolean-expression

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

Bool方法返回错误的值

bool contains(string)为链表散列表创建了一个方法,用于检查值是否在散列中.我使用辅助函数来递归,但是当辅助函数返回时false,bool contains(string)仍然返回true.我通过调试器运行它,我可以清楚地看到它返回false,我不知道为什么.

这是当前搜索的节点:

"laccoliths"->"morbiferous"->"oculi"->"unscabbarded"

我正在寻找的价值是"typung".

这是代码:

bool contains_h(string x, node * p) //helper method
{
    if (p == NULL)
        return false;
    else if (x == p->data)
        return true;
    else
        contains_h(x, p->next);
}
Run Code Online (Sandbox Code Playgroud)

bool contains(string word) { return contains_h(word, head); }

c++ boolean-logic boolean return-value boolean-expression

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

突破JavaScript ES6功能无法使用return

问题

这感觉应该很简单,但我不是因为某种原因而得到它.我决定采用一个明显常见的编程面试问题来测试我的技能组合:

" 给定一个只包含1到a.length范围内的数字的数组a,找到第二个匹配的最小索引的第一个重复数.换句话说,如果有多个重复的数字,则返回数字第二次出现的索引小于另一次出现的第二次出现的索引. "

(来源:https://codefights.com/interview-practice/task/pMvymcahZ8dY4g75q)

我相信我有正确的代码,但我不能让循环停止运行.给定示例数组,函数应返回"3",因为这是第一个重复的数字(不是第一个看到的数字).但是,无论我尝试什么,它都会返回2或错误.

我的代码

console.clear();

// create new object to store numbers and counts
var myObj = {};

// array for testing output
var arr1 = [2, 3, 3, 1, 5, 2];

// this will store the "key" number
var firstDuplicateFound = "";

function firstDup(a) {

	// loop through each value in numerical array
	a.forEach(function(num, i) {
		
		// if associative array has property that is value of current num:
		if …
Run Code Online (Sandbox Code Playgroud)

javascript foreach break boolean-expression ecmascript-6

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

Python 中的基本布尔表达式产生令人惊讶的结果

在 Python 中,我2>3 == False给出了False. 但我很期待True。如果我使用括号 ie(2>3) == False那么我得到True. 这背后的理论是什么?

python boolean-expression python-3.x

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

为什么与“is”的布尔比较如此糟糕?

风格指南如下:

# Correct:
if greeting:

# Wrong:
if greeting == True:

# Worse:
if greeting is True:
Run Code Online (Sandbox Code Playgroud)

请参阅PEP 8,搜索单词“更糟”

为什么是这样?我习惯于尽可能明确地检查条件,以使代码更具可读性并捕获异常。

考虑这个函数:

def f(do_it):
   if do_it : print("doit")
   else : print("no don't") 
Run Code Online (Sandbox Code Playgroud)

很容易被滥用/监督,出现意想不到的行为

>>> f("False")
doit
>>> f([False])
doit
Run Code Online (Sandbox Code Playgroud)

例如,当您检查可能无意中传递 if 子句的返回值时,这是一个真正的问题。通过使用该构造可以避免这种情况is

显然,PEP 建议有充分的理由,但它是什么?

在评论者的推动下,进一步的研究使我得出以下发现:

if x:
Run Code Online (Sandbox Code Playgroud)

调用 x 类的 __bool 方法。该方法应返回 True 或 False,具体取决于对象认为自己是两者中的哪一个。

if x==True:
Run Code Online (Sandbox Code Playgroud)

调用 x 类的 __eq 方法。该方法应该能够将自身与 True(或 False)进行比较,并根据具体情况返回 True 或 False。

if x is True
Run Code Online (Sandbox Code Playgroud)

两者都不调用。这测试 x 是否是“True”对象。它完全绕过了 __eq 和 __bool …

python boolean boolean-expression

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