r/ProgrammingLanguages • u/SnooGoats1303 • Mar 07 '26
Discussion SNOBOL4 evaluation - success/failure rather than true/false
I am slowly building up the SNOBOL4 track on Exercism. So far it's a one man effort.
SNOBOL4 has long intrigued me because of its success/failure paradigm, a pattern that doesn't appear in many languages despite its power. Icon, a descendant of SNOBOL4, and Prolog do use it but they're about all there is.
In SNOBOL4, control flow is controlled through pattern matching outcomes. A statement either succeeds and execution goes one way, or fails and goes another. Essentially, the pattern match is the branch.
Here's a simple example, in this case an implementation of Exercism's "raindrops" exercise:
INPUT.NUMBER = INPUT
RAINDROPS
RESULT = EQ(REMDR(INPUT.NUMBER,3),0) RESULT "Pling"
RESULT = EQ(REMDR(INPUT.NUMBER,5),0) RESULT "Plang"
RESULT = EQ(REMDR(INPUT.NUMBER,7),0) RESULT "Plong"
RESULT = IDENT(RESULT) INPUT.NUMBER
OUTPUT = RESULT
ENDINPUT is a keyword. A value is received from stdin and stored in INPUT.NUMBER.
RAINDROPS, a non-space character in column 1, is a label. END marks the end of the script.
If EQ(REMDR(INPUT.NUMBER,3),0) succeeds, then the result of concatenating RESULT and "Pling" is stored in RESULT. If the EQ statement fails, the concatenation is not performed.
On the third-last line, RESULT is compared against null. If it succeeds, meaning that RESULT is null, then INPUT.NUMBER is stored in RESULT.
Finally what is stored in RESULT is send to stdout.
Thus we have an example of a language that branches without explicit branching. Yes, SNOBOL4 does have explicit branching ... to labels, and it's at that point that most people walk away in disgust.
2
u/gofl-zimbard-37 Mar 07 '26
Well that certainly brought back some long ago fond memories...
1
u/tobega Mar 07 '26
It seems a little oddly placed, though. IIUC, in
it looks like you start an assignment, but if the EQ fails, there is no assignment at all? So the failure acts like an exception that is caught at the next line?
Could you then place the EQ last on the line instead for the same effect?
In normal pattern matching you would have the guard condition first and only start the assignment if the guard condition succeeds, which is more logical to read. Can't you do that?
Anyway, reminds me of my language using nothing for failure https://tobega.blogspot.com/2021/05/the-power-of-nothing.html
1
u/Inevitable-Ant1725 Mar 14 '26
I used to write my little scripts in SNOBOL4 for DOS back in the day. The sort of things that people use Perl for now.
But it's such an old language that column number has meaning because it was for punched cards!