寻找可以让你改变真假的编程语言

发布于 2024-08-25 16:39:06 字数 250 浏览 12 评论 0原文

出于好奇,我正在寻找一种动态的面向对象语言,它允许您将 true 更改为 false,反之亦然。

像这样的事情:

true = false, false = true;

这也应该影响任何条件语句,因此 42 == 42 应该返回 False。 基本上,有了这个前提,语言中的任何内容对于程序员来说都是安全的。

有这样的语言吗?

For my curiosity sake I'm looking for a dynamic object oriented language that allows you to change true to false and vice versa.

Something like this:

true = false, false = true;

This should also affect any conditional statements, therefore 42 == 42 should return False.
Basically, with this premise, nothing in the language would be safe from the programmer.

Is there any language like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

七颜 2024-09-01 16:39:06

Smalltalk 可以让你做到这一点。

Smalltalk-80 中的一切都是
可从内部进行修改
一个正在运行的程序。这意味着,
例如,IDE 可以更改为
无需重新启动即可运行的系统
它。在一些实施方式中,
语言的语法或垃圾
集合实现也可以
即时改变。就连声明
true 变为: false 有效于
Smalltalk,虽然执行它是
不推荐。使用时
明智地,这种程度的灵活性
允许最短的之一
新代码输入所需时间
生产系统。

http://en.wikipedia.org/wiki/Smalltalk

Smalltalk will let you do this.

Everything in Smalltalk-80 is
available for modification from within
a running program. This means that,
for example, the IDE can be changed in
a running system without restarting
it. In some implementations, the
syntax of the language or the garbage
collection implementation can also be
changed on the fly. Even the statement
true become: false is valid in
Smalltalk, although executing it is
not recommended. When used
judiciously, this level of flexibility
allows for one of the shortest
required times for new code to enter a
production system.

http://en.wikipedia.org/wiki/Smalltalk

凉宸 2024-09-01 16:39:06

Python(显然在版本 3 之前):

True = False
print True, False

>>False False

但是:

True = False
print 1==1, 1!=2, 2!=2

>>True True False

有关详细信息,请参见,例如:

  1. 为什么 Python 不能按照我的预期处理 true/false 值?

  2. TRUE 是否始终具有非零值?

Python (before version 3, apparently):

True = False
print True, False

>>False False

But:

True = False
print 1==1, 1!=2, 2!=2

>>True True False

For details, see, for example:

  1. Why can’t Python handle true/false values as I expect?

  2. Has TRUE always had a non-zero value?

东北女汉子 2024-09-01 16:39:06

您的问题有两种可能的答案。

一种可能性:你可能仍然在使用 true 和 false,但你生活在异国他乡,用不同的名字称呼它们。在这种情况下,这只是编译器/调试器环境显示的内容的问题 - 这可以通过修改编程工具来更改。 (或者,您可以从 LISP 开始,缺少布尔原语。)

或者,也许你的意思是你希望它产生后果。例如,如果您希望它不打印任何内容:

if (42==42) {
   print("This is true");
}

这样做的后果超出了我的想象,但如果您要重新定义条件原语(if、switch 等),您可以做到。在简单的 LISP 中,通过创建这些条件的新版本可能是最简单的。布莱恩在谈论 Tcl 时似乎讨论过这一点。


旁白:假设您创建了一种语言,其中 false 为 true,true 为 false,if 为 if not,!=s 为 ==s,等等。也许如果你颠倒足够多的东西,你就会回到原来的语言。 :)

There are two possible answers to your question.

One possibility: you might still be using true and false, but you live in a foreign land where you call them by different names. In that case, it's just a matter of what the compiler/debugger environment displays - that's changeable by modifying the programming tools. (Or, you could start with LISP, which lacks a boolean primitive.)

Or, maybe what you're saying is that you want it to have consequences. For example, if you wanted this not to print anything:

if (42==42) {
   print("This is true");
}

The consequences of this are beyond my ability to imagine, but if you were to redefine conditional primitives (if, switch, etc.) you could do it. It would probably be easiest in a bare-bones LISP, by creating new versions of those conditionals. Looks like Bryan discussed that when talking about Tcl.


Aside: Let's say you create a language where false is true, true is false, ifs are if nots, !=s are ==s, and so on. Perhaps if you inverted enough things you'd get back to the original language. :)

玉环 2024-09-01 16:39:06

C/C++ 允许您定义宏,所以 TRUE==FALSE 和 FALSE==TRUE
您还可以将 == 运算符重载为相当于 != (反之亦然)作为单独的步骤。

C/C++ would allow you to define macros so TRUE==FALSE and FALSE==TRUE
You could also overload the == operator to be equivalent to != (and vice versa) as a separate step.

对不⑦ 2024-09-01 16:39:06

更改表示 true 和 false 的文字/常量与更改布尔表达式的含义之间存在差异。第一个很简单,可以使用程序源(例如 C:

#define TRUE 0
#define FALSE 1

或编译器源)轻松完成。

但是,改变布尔表达式在所有情况下的求值方式将不那么简单,但也不是太困难。不过,这将再次需要更改编译器或解释器源代码。

There is a difference between changing the literals/constants that represent true and false and changing the meaning of boolean expressions. The first is easy and can be done trivially with either the program source, for example in C:

#define TRUE 0
#define FALSE 1

or the compiler source.

But changing how boolean expressions are evaluated in all circumstances would be less trivial but not too difficult. It would once again require changes to the compiler or interpreter source, though.

屋顶上的小猫咪 2024-09-01 16:39:06

Tcl可能会做你想做的事。或许。您无法完全重新定义 true 和 false,因为 tcl 实际上没有 true 或 false 对象或值之类的东西,但您可以重新定义作用于表达式的所有命令以返回与通常情况相反的命令。例如,您可以重新定义“if”以返回与 if 核心定义相反的内容。

这是一个简单的、不完整的示例:

# keep the old if around
rename if ::_if

# make a new if with a negated condition
proc ::if {condition args} {
     set command [list _if !($condition) {*}$args]
     uplevel $command
}

交互式运行我得到:(

% if true {puts true} else {puts false}
false
% if 1==1 {puts true} else {puts false}
false

需要 tcl 8.5+)

Tcl might do what you want. Maybe. You can't exactly redefine true and false since tcl doesn't really have such a thing as a true or false object or value, but you can redefine all the commands that act on expressions to return the opposite of what it normally does. For example, you can redefine "if" to return the opposite of the core definition of if.

Here's a trivial, incomplete example:

# keep the old if around
rename if ::_if

# make a new if with a negated condition
proc ::if {condition args} {
     set command [list _if !($condition) {*}$args]
     uplevel $command
}

Running this interactively I get:

% if true {puts true} else {puts false}
false
% if 1==1 {puts true} else {puts false}
false

(requires tcl 8.5+)

尘世孤行 2024-09-01 16:39:06

万一我想要这个,我只会重新定义 if,(或者使 IF 成为一个宏,然后使用它......)

In the unlikely event that I wanted this, I would just re-define if, (or make IF a macro and then use it...)

榆西 2024-09-01 16:39:06

Swift 语言为 Bool 类型提供了一个很好的 toggle() 实例方法。

var myBool: Bool = true 
myBool.toggle

现在 myBool 为 false。

The Swift language has a nice toggle() instance method for the Bool type.

var myBool: Bool = true 
myBool.toggle

And now myBool is false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文