TL;DR is val name <-
more readable than name val <-
?
I'm designing a semi-concatenative, postfix language. I haven't given much thought to the style in which variable are assigned, and now I see that there are two (main) ways to do it. Originally, a variable assignment looks like:
variable value <-
So, here's an example program for a fisher-yates algorithm implemented in this language:
{ arr :
n arr size <-
0 n 2 - i [
j i n .. randin <-
arr arr [i j nswap] apply <-
] for
arr isolate
} shuf def
You don't precisely need to know the specifics, but I want to point out three lines where variable assignment takes place:
n arr size <-
j i n .. randin <-
arr arr [i j nswap] apply <-
I've added whitespace to compensate what I think is a tad unreadable. However, having not utilized many readable postfix languages, I am not sure what is considered readable. So, I initially defined variable assignment so that it would more closely represent conventional variable assignment, like so:
my language: a 4 <-
conventional: a <- 4
However, upon actually writing some relative short programs, I feel like the readability could be improved by having the name adjacent to the assignment, in the same way how 2 3 * 4 +
is more readable than 4 2 3 * +
, since having only 2 items on the "stack of your mind" at a time allows you to evaluate it like a regular math problem. So, the program revised would be:
{ arr :
arr size n <-
0 n 2 - i [
i n .. randin j <-
arr [i j nswap] apply arr <-
] for
arr isolate
} shuf def
Which seems to look more readable.
(Skip to the bottom for the actual question, I'm just explaining the original program in detail from here to there for those who want it.)
Alright, so a comment looks like (* ... *)
. Now, I've added comments explaining the program.
(* { name : ...} begins a lambda that takes a single argument
`name` from the stack when executed. *)
{ arr :
(* let n be the length of the array *)
n arr size <-
(* from 0 to n - 2, using `i` as a variable... *)
0 n 2 - i
(* ...execute a simple func, which has `i` in scope *)
[
(* set j to a random index, i <= j < n *)
j i n .. randin <-
(* applies the stack operation `nswap` to the array,
swapping elements `i` and `j`. *)
arr arr [i j nswap] apply <-
] for
(* make arr the only thing on the stack for a return value *)
arr isolate
(* close the lambda and define as a function `shuf` *)
} shuf def
(* example usage *)
(1 2 3 4 5) shuf out
The question
In a postfix language such as this, would it be better style in the long run to have the variable name adjacent to the variable, or as the first argument? I chose the latter in hopes it would be more readable, but now it seems that the former is more readable.
(I realize I didn't gave the full specification for the language, and that's simply because I have none. Feel free to "define" your own examples for the sake of argument.)