User:A/H spec
H Reference
Credits to Code Golf & Coding Challenges StackExchange entry by @Adám.
H is a text-based, weakly-typed string concatenation language.
General H syntax
Scripts: One or more lines, each containing zero or more statements, optionally followed by a comment.
White-space: H supports whitespace inserting as long as that inserting retains the lexical items.
Operators: There's only one, +
, which is string concatenation.
Comments begin with #
and continue until the end of the line. #
may be prefixed by one or more spaces and/or tabs.
name: a sequence of ASCII letters [A-Z]|[a-z]
value: a
+
-delimited sequence of one or more strings (see below) and/or previously defined names.
Strings
Opened and closed by " supporting all ASCII characters and with the following escape sequences(for readability):
\\
: the literal backslash character; \
\n
: a line break; CR, LF, CRLF, or LFCR (you decide)
\"
: a quotes symbol; "
\t
: a tab character;
(HT)
Any unbounded string that looks like an integer (e.g. -2147483648
or 2147483647
) may be left unquoted. ,
s may be inserted anywhere in these strings.
Statements
Statements are terminated by ;
. There are only three types of H statements:
def name=value;
sets the variable name to the given value.
print(value);
prints value without trailing line break.
input(value;name);
prints value without trailing line break, allows the user to enter a sequence of characters that extend that line, and assigns the characters to name. Any subsequent output begins on the next line.
Test script
def hEllo = "Hello, "; def HSMO = hEllo + "strange"+-1; print(""); input(HSMO+"what is your name?"; name); #enter "User A" via stdin #print(-123) # nope print(hEllo+"\""+name+"\"\n\tthis isn't APL\\"+360+"!"); #done print(-12+34) # note the tab before #
Running the appropriate script, and entering User A
should leave the console/screen/window showing:
Hello, strange-1what is your name?User A Hello, "User A" this isn't APL\360!-1234
H Quine via adding one operator
H can implement a quine program via adding just one operator.
Here a quine created via adding []
, the long string grouping operator.
def x=["def x=["+x+"];print("+x);];print("def x=["+x+"];print("+x);
Here is another quine that barely adds anything to H(numbers represent their ASCII values here):
def s="print(100+101+102+32+115+61+34+s+34+59+s);";print(100+101+102+32+115+61+34+s+34+59+s);
It is unclear whether standard H can implement a Quine program, though.
Extended H Reference
EH is a text-based, weakly-typed string concatenation language. All code is case-sensitive.
General H syntax
Bool values: All values excluding "" and "0" are true values.
Scripts: One or more lines, each containing zero or more statements, optionally followed by a comment.
White-space: H supports whitespace inserting as long as that inserting retains the lexical items.
Operators: There are multiple operators:
+
, which is string concatenation[[]]
, which is multi-line string grouping operators%
, which is replacing all occurences of%%
in a string to the string as the second parameter.\
, which is newline insertion()
, which is the grouping operator==
, which is string equality (returns "0" if two strings are not equal; "1" otherwise)!=
, which is string inequality(returns "0" if two strings are equal; "1" otherwise)> < >= <=
, which represents >, <, <=, >= in other languages; compares the order of two strings in the dictionary<=>
, which returns 3 types of values (Greater than(1), Equal to(0), Less than(-1));
, which is the sequential execution operator*
, which multiplies the string number times's
access the right-operand-th character of left-operand (indexes start at 1)
Comments begin with #
and continue until the end of the line. #
may be prefixed by one or more spaces and/or tabs.
Multi-line comments may use `...`
. They cannot be nested.
name: a sequence of alphabetic ASCII letters
value: a +
-delimited sequence of one or more strings (see below) and/or previously defined names.
Strings
Opened and closed by " supporting all ASCII characters and with the following escape sequences:
\\
: the literal backslash character; \
\n
: a line break; CR, LF, CRLF, or LFCR (you decide)
\"
: a quotes symbol; "
\t
: a tab character;
(HT)
Any unbounded string that looks like an integer (e.g. -2147483648
or 2147483647
) may be left unquoted. ,
s may be inserted anywhere in these strings.
Statements
Statements are terminated by ;
. There are only three types of H statements:
def name=value;
sets the variable name to the given value.
print(value);
prints value without trailing line break.
input(value;name);
prints value without trailing line break, allows the user to enter a sequence of characters that extend that line, and assigns the characters to name. Any subsequent output begins on the next line.
n2c(value);
converts all numbers in a string to their ASCII character values
c2n(value);
converts all non-numbers in a string to their ASCII values
line();
returns a number presenting the current line
file();
returns a string presenting the current file
width(value);
returns the length of a string in strings
min(a;b;...);
returns the smallest string in the dictionary
max(a;b;...);
returns the largest string in the dictionary
tup(value);
converts all lower case values in value to uppercase
tlo(value);
converts all upper case values in value to lowercase
up(value);
checks if all values in value are upper case values
lo(value);
checks if all values in value are lower case values
sub(a;n;m);
chooses a substring of a in the range n to m(including n and m)
find(a;n);
locates the first occurance of n in the string a
rfind(a;n);
locates the first occurance of n in the string a (right-first)
rev(value);
reverses value
rot13(value);
ROT13 encodes value
sort(value);
sorts all items in value
rsh(value;x);
deletes x elements in value in the right
lsh(value;x);
deletes x elements in value in the left
append(value;value2);
appends value2 to the right of value
insert(value;value2);
appends value2 to the left of value