Concatenative topics
Concatenative meta
Other languages
Meta
Examples of different ways to do the same thing in Listack.
10 @>n # set local variable n to 10 # The following all set the local variable a to (n+1)*2 n 1 .+ 2 .* @>a n + 1 * 2 @>a (n + 1 * 2) @>a (n + 1) * 2 @>a n.inc * 2 @>a +: n 1 2 .* @>a +(n 1) * 2 @>a +(n,1)*2@>a set(‘a’, (n + 1 * 2)) set: `a (n + 1 * 2) “a” set (+: n 1 * 2) \a (n 1 .+ 2 .*) .set set(\a, *(+(n, 1), 2)) set(\a *(+(n 1) 2)) \a set (+: @<n 1 2 .*) \n.get 1 .+ 2 .* @>a {n + 1 * 2} eval @>a +: @*n 1 * 2 \a swap .set # And many more, with increasing levels of obfuscation.
Computing the summation of 0..number
def: "summ" [Int] {dup <=> # would be a bit faster if this were postfix {dup {1 .+ dup 0 .<} {dup roll .+ swap} .while} # negative {dup} # zero {dup {1 .- dup 0 .>} {dup roll .+ swap} .while} # positive drop } timer_start 100_000 summ timer_check # timer works in nanoseconds "summ(100,000) time: " print 1_000_000 .// print " ms" println println cr def: "fast_summ" [Int] { dup # <=> This is a good way to indicate what’s going on with postfix words. {dup 1 .- .* -2 .//} {nop} {dup 1 .+ .* 2 .//} .<=> } timer_start 100_000 fast_summ timer_check "fast_summ(100,000) time: " print 1_000 .// print " us" println println
The following is the actual definition of 'while'.
# while create_global: "_while_cond" create_global: "_while_body" # internal use only def: "_while_" [] {@!_while_cond {@!_while_body _begin_loop_ _while_} .iff} # {condition to continue} while {body to execute} # {true} while {println:"infinite loop"} def: "while" [Blocky, Blocky] {@>_while_body @>_while_cond _while_ _end_loop_ @<_while_body drop @<_while_cond drop} def: "while" [Bool, Blocky] {@>_while_body @>_while_cond _while_ _end_loop_ @<_while_body drop @<_while_cond drop} def: "while" [Bool, Word] {>block .while} def: "while" [Blocky, Word] {>block .while}
[3 5 7] {dup .+} {dup .*} bi_map # produces: [6 10 14] [9 25 49] [3 5 7] {dup .+} {dup .*} bi_each # produces: 6 10 14 9 25 49 3 4 \sqr both .+ sqrt # produces 5.0
This revision created on Fri, 13 Oct 2023 17:03:35 by CFout