Question

I was just reading a bit about the HQ9+ programming language ( http://esolangs.org/wiki/HQ9 , http://en.wikipedia.org/wiki/HQ9%2B and http://www.cliff.biffle.org/esoterica/hq9plus.html ), and it tells me something about a so called 'accumulator' which can be incremented, but not be accessed. Also, using the + doesn't manipulate the result:

code

H+H

result

Hello World
Hello World

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top