Some of Sunday's edits have been lost. The edits from Saturday that were reverted have been restored. Site is now hosted on prgmr.com. Thank you for your patience. This notice will be removed one week from posting. --Michael Mol 18:12, 7 March 2010 (UTC)
Execute HQ9+
From Rosetta Code
Execute HQ9+ is a programming task. Visitors like you are encouraged to solve it according to the task description, using any language they may happen to know.
Implement a HQ9+ interpreter or compiler for Rosetta Code.
Contents |
[edit] Common Lisp
See RCHQ9+/Common Lisp.
[edit] E
See RCHQ9+/E.
[edit] Haskell
See RCHQ9+/Haskell.
[edit] Java
See RCHQ9+/Java.
[edit] JavaScript
The function below executes a HQ9+ program and returns the program output as a string.
function hq9plus(code) {
var out = '';
var acc = 0;
for (var i=0; i<code.length; i++) {
switch (code.charAt(i)) {
case 'H': out += "hello, world\n"; break;
case 'Q': out += code + "\n"; break;
case '9':
for (var j=99; j>1; j--) {
out += j + " bottles of beer on the wall, " + j + " bottles of beer.\n";
out += "Take one down and pass it around, " + (j-1) + " bottles of beer.\n\n";
}
out += "1 bottle of beer on the wall, 1 bottle of beer.\n" +
"Take one down and pass it around, no more bottles of beer on the wall.\n\n" +
"No more bottles of beer on the wall, no more bottles of beer.\n" +
"Go to the store and buy some more, 99 bottles of beer on the wall.\n";
break;
case '+': acc++; break;
}
}
return out;
}
[edit] Python
See RCHQ9+/Python.
[edit] Ruby
See RCHQ9+/Ruby.
[edit] Tcl
See RCHQ9+/Tcl.
[edit] Ursala
See RCHQ9+/Ursala.
Categories: Programming Tasks | Solutions by Programming Task | Common Lisp | E | Haskell | Java | JavaScript | Python | Ruby | Tcl | Ursala







