You should use loop for multiple iterations of addition. GOTO statement and labels can be used to design a loop of your requirement. please give me some ideas.
Write a short program in SNOBOL that calculate the sum of all even integers from 1 to 100
220 Views Asked by Abdul Qayyum At
2
There are 2 best solutions below
1

I think it's not really Snobol if it doesn't contain a pattern match...
This solution generates the even numbers 000 .. 100
by matching a pattern against the string "01012345678902468"
and adding the generated numbers through the function add()
. Looping is done through the FAIL
keyword at the end of the pattern, which forces the pattern scanning to continue looking for alternatives until the expression (*EQ(h t o,100) ABORT)
causes the scanning to abort.
&FULLSCAN = 1
DEFINE('add(x)') :(add.End);add sum = sum + x :(RETURN);add.End
"01012345678902468" (LEN(1) $ h *LEN(1 - h)) ARB (LEN(1) $ t
. *LEN(9 - t)) ARB LEN(1) $ o *add(h t o)
. (*EQ(h t o,100) ABORT) FAIL
OUTPUT = sum
END
Works both in Snobol and Spitbol (&FULLSCAN = 1
is needed for Snobol, it's a no-op in Spitbol)
*This works on the Spitbol version of Snobol.
loop m = le(n, 98) (n = n + 2) + m :s(loop); output = m ;end