HQ9+, H9+, KL esoterical languages and the beer song
Let's first sing a beer song (in R6RS Scheme):
-
#!r6rs
-
(library (sing-beer-song)
-
(export sing-beer-song)
-
(import (rnrs))
-
-
(define (sing-beer-song n)
-
(define (n-bottles i capital?)
-
(string-append
-
(cond
-
((eqv? i -1) (number->string 99))
-
((and (eqv? i 0) capital?) "No more")
-
((eqv? i 0) "no more")
-
(else (number->string i)))
-
" bottle" (if (eqv? i 1) "" "s")
-
" of beer"))
-
(define (where?) " on the wall")
-
(define (what-to-do? n)
-
(if (> n 0) "Take one down and pass it around, "
-
"Go to the store and buy some more, "))
-
(string-append
-
"\n" (n-bottles n #t) (where?) ", " (n-bottles n #f) ".\n"
-
(what-to-do? n) (n-bottles (- n 1) #f) (where?) ".\n"
-
(if (> n 0) (sing-beer-song (- n 1)) ""))))
Then let's make an extra library:
-
#!r6rs
-
(library (language-utilities)
-
(export glue)
-
(import (rnrs))
-
-
(define (glue los) (fold-left string-append "" los)))
... and we are ready to write yet-another HQ9+ interpreter:
-
#!r6rs
-
(import (rnrs) (sing-beer-song) (language-utilities))
-
-
; HQ9+ interpreter v0.1 (Ivan Lakhturov)
-
; http://esolangs.org/wiki/HQ9
-
-
(define i 0)
-
-
(define (run-program s)
-
(define (run-command c)
-
(cond
-
((eqv? c #\H) "Hello, World!")
-
((eqv? c #\Q) s)
-
((eqv? c #\9) (sing-beer-song 99))
-
((eqv? c #\+) (set! i (+ i 1)) "") ; (number->string i))
-
(else (string c))))
-
(glue (map run-command (string->list s))))
-
-
(display (run-program "HQ9+"))
HQ9+ is a joke language, featuring "Hello world" command, quine command, beer-song command and a counter increment (counter cannot be accessed or printed out). Quine implementation here is the classical "quine-cheating", where a program has access to its source. To make the quine more 'honest' somebody designed H9+. This is the same as HQ9+, but without "Q" command, and additionally, all characters on input are ignored, except for H, 9 and +. Then, obviously, "Hello, World!" program will be a quine. Let's implement H9+:
-
#!r6rs
-
(import (rnrs) (sing-beer-song) (language-utilities))
-
-
; H9+ interpreter v0.1 (Ivan Lakhturov)
-
; http://esolangs.org/wiki/H9
-
-
(define i 0)
-
-
(define (run-program s)
-
(define (run-command c)
-
(cond
-
((eqv? c #\H) "Hello, World!")
-
((eqv? c #\9) (sing-beer-song 99))
-
((eqv? c #\+) (set! i (+ i 1)) "") ; (number->string i))
-
(else "")))
-
(glue (map run-command (string->list s))))
-
-
(display (run-program "Hello, World!"))
And let's implement also a variation of this theme, the esoterical language KL:
-
#!r6rs
-
(import (rnrs) (language-utilities))
-
-
; KL interpreter v0.1 (Ivan Lakhturov)
-
; http://ivanguide.ru/kl/
-
-
(define (run-program s)
-
(define (run-command c)
-
(cond
-
((eqv? c #\+) "Привет, мир!")
-
((eqv? c #\-) s)
-
((eqv? c #\*)
-
"Я узнал, что у меня
-
Есть огpомная семья –
-
И тpопинка, и лесок,
-
В поле каждый колосок!
-
-
Речка, небо голубое –
-
Это все мое, pодное!
-
Это Родина моя!
-
Всех люблю на свете я!")
-
((eqv? c #\/) "\n")
-
(else (string c))))
-
(glue (map run-command (string->list s))))
-
-
(display (run-program "+/-/*/extras"))
The semantics is as following: + is printing "Hello, world!" in Russian, - is printing a program's source, / is making a newline, and * print outs a poem from Russian movie "Brother".
To complete the picture, we can mention other close related to HQ9+ joke languages: HQ9++, CHIQRSX9, HQ9+B, HQ9+2D. HQ9++ is 'an object-oriented extension of HQ9+'; not interesting. CHIQRSX9+ adds eval, ROT-13 and sorting of input lines. ROT-13 (Caesar cipher) is a nice exercise to implement, but let's leave it for later. HQ9+B adds Brainfuck: this is definitely a thing to implement, but I will deal with Brainfuck later. HQ9+2D is not properly specified (even for a joke language), but commands it adds remind me 2D Turing-machine, so called Langton's ant. I want to implement and play with different Turing-machines, but later.
Later I will also look through the list of joke languages. For example, the first there is a 'language' 99, which just prints out '99 bottles of beer' song. Anyways, I hope, there could be something exciting in the list.
































