什么是“累加器”?在 HQ9+?

发布于 2024-08-31 06:09:37 字数 612 浏览 7 评论 0原文

我刚刚阅读了一些有关 HQ9+ 编程语言的内容:

它告诉我一些关于所谓的“累加器”的信息可以增加,但不能访问。另外,使用 + 不会操纵结果,因此输入

H+H

会给出结果:

Hello World
Hello World

任何人都可以解释一下它是如何工作的、它的作用是什么以及它是否有意义?谢谢。

I was just reading a bit about the HQ9+ programming language:

and it tells me something about a so-called “accumulator” which can be incremented, but not accessed. Also, using + doesn't manipulate the result, so that the input

H+H

gives the result:

Hello World
Hello World

Can anyone explain me how this works, what it does, and whether it makes any sense? Thanks.

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

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

发布评论

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

评论(7

得不到的就毁灭 2024-09-07 06:09:37

最近在 Clojure 中完成了一个实现(如下),我可以有把握地说累加器对于 HQ9+ 的成功实现绝对至关重要。如果没有它,人们将留下 HQ9 的实现,尽管它本身无疑是有价值的,但显然是不同的,因此没有累加器的 HQ9+ 以及递增它的指令,因此将 HQ9+ 的实施。

编者注:Bob 今天已经服药了,但还没有完全发挥作用;因此,可能需要进一步的解释。我相信 Bob 想说的是,HQ9+ 作为一种编程语言是无用的,根据se;但是,在学习如何用新语言成功实现某些内容时,实现它实际上很有用,好吧,我现在就去安静地蜷缩在鲍勃的大脑后面,让他回去做。 ...当我不介意商店时他会做什么...)。

无论如何... Clojure 中的实现如下:

(defn hq9+ [& args]
  "HQ9+ interpreter"

  (loop [program      (apply concat args)
         accumulator  0]
    (if (not (empty? program))
      (case (first program)
        \H (println "Hello, World!")
        \Q (println (first (concat args)))
        \9 (apply println (map #(str % " bottles of beer on the wall, "
                                      % " bottles of beer, if one of those bottles should happen to fall, "
                                      (if (> % 0) (- % 1) 99) " bottles of beer on the wall") (reverse (range 100))))
        \+ (inc accumulator)
            (println "invalid instruction: " (first program)))) ; default case
    (if (> (count program) 1)
       (recur (rest program) accumulator))))

请注意,此实现仅接受作为参数传递到函数中的命令;它不读取其程序的文件。这可能会在未来的版本中得到解决。另请注意,这是该语言的“严格”实现 - 原始页面(位于 Wayback Machine) 清楚地表明只应接受大写的“H”和“Q”,尽管它暗示小写字母也可能被接受。由于实现任何编程语言的部分目的是严格遵守所编写的规范,因此此版本的 HQ9+ 只接受大写字母。如果有需要的话,我完全准备好建立一个宗教,暂定名为“圣大写锁会议”,它将宣布使用大写字母是由弗雷德命令的(我们的上帝 - 弗雷德 - 这似乎是一个<强>友好的神的名字,不是吗?),并且会认为使用小写字母是令人厌恶的……我的意思是,是令人厌恶的!

分享并享受。

Having recently completed an implementation in Clojure (which follows) I can safely say that the accumulator is absolutely central to a successful implementation of HQ9+. Without it one would be left with an implementation of HQ9 which, while doubtless worthy in and of itself, is clearly different, and thus HQ9+ without an accumulator, and the instruction to increment it, would thus NOT be an implementation of HQ9+.

(Editor's note: Bob has taken his meds today but they haven't quite kicked in yet; thus, further explanation is perhaps needed. What I believe Bob is trying to say is that HQ9+ is useless as a programming language, per se; however, implementing it can actually be useful in the context of learning how to implement something successfully in a new language. OK, I'll just go and curl up quietly in the back of Bob's brain now and let him get back to doing...whatever it is he does when I'm not minding the store...).

Anyways...implementation in Clojure follows:

(defn hq9+ [& args]
  "HQ9+ interpreter"

  (loop [program      (apply concat args)
         accumulator  0]
    (if (not (empty? program))
      (case (first program)
        \H (println "Hello, World!")
        \Q (println (first (concat args)))
        \9 (apply println (map #(str % " bottles of beer on the wall, "
                                      % " bottles of beer, if one of those bottles should happen to fall, "
                                      (if (> % 0) (- % 1) 99) " bottles of beer on the wall") (reverse (range 100))))
        \+ (inc accumulator)
            (println "invalid instruction: " (first program)))) ; default case
    (if (> (count program) 1)
       (recur (rest program) accumulator))))

Note that this implementation only accepts commands passed into the function as parameters; it doesn't read a file for its program. This may be remedied in future releases. Also note that this is a "strict" implementation of the language - the original page (at the Wayback Machine) clearly shows that only UPPER CASE 'H's and 'Q's should be accepted, although it implies that lower-case letters may also be accepted. Since part of the point of implementing any programming language is to strictly adhere to the specification as written this version of HQ9+ is written to only accept upper-case letters. Should the need arise I am fully prepared to found a religion, tentatively named the CONVOCATION OF THE HOLY CAPS LOCK, which will declare the use of upper-case to be COMMANDED BY FRED (our god - Fred - it seems like such a friendly name for a god, doesn't it?), and will deem the use of lower-case letters to be anathema...I MEAN, TO BE ANATHEMA!

Share and enjoy.

奈何桥上唱咆哮 2024-09-07 06:09:37

在编写了实现之后,我想我可以毫无疑问地说它毫无意义全部。我建议你不要担心;毕竟这是一种非常愚蠢的语言。

Having written an implementation, I think I can say without a doubt that it makes no sense at all. I advise you to not worry about it; it's a very silly language after all.

ㄟ。诗瑗 2024-09-07 06:09:37

这是一个笑话。

HQ9+ 还有一个面向对象的扩展,称为 HQ9++。它有一个新命令++,它实例化一个对象,并且出于向后兼容性的原因,还将累加器寄存器递增两次。再说一遍,由于无法存储、检索、访问、操作、打印或以其他方式影响对象,因此它完全没有用。

It's a joke.

There's also an object-oriented extension of HQ9+, called HQ9++. It has a new command ++ which instantiates an object, and, for reasons of backwards-compatibility, also increments the accumulator register twice. And again, since there is no way to store, retrieve, access, manipulate, print or otherwise affect an object, it's completely useless.

德意的啸 2024-09-07 06:09:37

它增加了一些不可访问的、不是规范定义的、甚至显然没有真正使用过的东西。我想说你可以随心所欲地实现它,也可能根本不实现它。

It increments something not accessible, not spec-defined, and apparently not really even used. I'd say you can implement it however you want or possibly not at all.

荒芜了季节 2024-09-07 06:09:37

正确的答案是其他答案已经暗示但没有明确说明的答案:语言规范未定义累加器递增的效果,而是作为实现的选择。

The right answer is one that has been hinted at by the other answers but not quite stated explicitly: the effect of incrementing the accumulator is undefined by the language specification and left as a choice of the implementation.

童话里做英雄 2024-09-07 06:09:37

事实上,我错了。

累加器是存储最后计算结果的寄存器。在 Intel x86 中,任何寄存器都可以指定为累加器,MUL 除外。

资料来源:

http://en.wikipedia.org/wiki/Accumulator_(computing)

当我第一次访问您问题中的第三个网站来寻找同学时,我感到非常惊讶我的 OCaml 实现写在页面底部。

(已更新网站链接)

Actually, I am mistaken.

The accumulator is the register to which the result of the last calculation is stored. In an Intel x86, any register may be specified as the accumulator, except in the case of MUL.

Source:

http://en.wikipedia.org/wiki/Accumulator_(computing)

I was quite surprised the first time I visited the third site in your question to find out a schoolmate of mine wrote the OCaml implementation at the bottom of the page.

(updated site link)

真心难拥有 2024-09-07 06:09:37

我认为这个累加器及其最重要的操作是有的,或者一定有的,它的原因——增量:未来的兼容性。

我们经常看到一种语言的发明,通常是受到其他语言的启发,当然还有一些盐(新概念,或者至少是一些改进)。后来,当语言传播时,就会出现问题,并会进行修改、添加或其他任何内容。这相当于说“我们错了,这件事是必要的,但我们当时并没有意识到”。

嗯,HQ9+ 中的累加器想法恰恰相反。将来,当该语言被传播时,没有人会说“我们需要一个累加器,但 HQ9+ 缺少它”,因为该语言的标准,即使在其初稿中,也声明存在一个累加器,并且它甚至是可以修改的(否则,这将是无意义的)。

I think that there is, or there must be, a reason for this accumulator and the most important operation on it - increment: future compatibility.

Very often we see that a language is invented, often inspirated by some other language, with of course some salt (new concepts, or at least some improvement). Later, when the language spreads, problems arise and modifications, additions or whatever are introduced. That is the same as saying "we were wrong, this thing was necessary but we didn't tought of it at the time".

Well, this accumulator idea in HQ9+ is exactly the opposite. In the future, when the language will be spread, nobody will be able to say "we need an accumulator, but HQ9+ lacks it", because the standard of the language, even in its first draft, states that an accumulator is present and it is even modifiable (otherwise, it would be a non-sense).

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