What is the 'accumulator' in 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.


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.