Concatenative language
Concatenative topics
Concatenative meta
Other languages
Meta
Examples of different ways to do the same thing in Listack. [{ 9 8 inc 1 2 + 3 * 1 .+ 2 .* 3 +: 1 2 3 * *(+(1 2) 3) }] Computing the summation of 0..number [{ def: "summ" [Int] {dup #<=> # That was a single-word comment. It's quite handy. {dup {inc dup 0 <} {dup raise + swap} while} # do if negative {dup} # do if zero {dup {dec dup 0 >} {dup raise + swap} while} # do if 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 #<=> {dup dec * -2 //} {nop} {dup inc * 2 //} <=> } timer_start 100_000 fast_summ timer_check "fast_summ(100,000) time: " print 1_000 // print " us" println println }] The following are the actual definitions of 'while' and 'until'. [{ # while helpers create_global: "_while_cond" create_global: "_while_body" alias: "_while_" {@!_while_cond # eval condition {@!_while_body # eval loop body _begin_loop_ # prep for cont _while_ } # recurse when} # 'when' is an 'if' with no 'else' clause # {condition to continue} {body to execute} while # Executes body zero or more times. # {true} {println:"infinite loop"} while def: "while" [Executable, Executable] {@>_while_body @>_while_cond # set up variables _while_ # call helper _end_loop_ # prep for break @<_while_body drop @<_while_cond drop} # tear down variables def: "while" [Bool, Executable] {@>_while_body @>_while_cond _while_ _end_loop_ @<_while_body drop @<_while_cond drop} # {body} {condition} until # Always executes body at least once. # {println:"infinite loop"} false until def: "until" [Executable, Executable] { {not} concat @>_while_cond @>_while_body @!_while_body _begin_loop_ _while_ _end_loop_} def: "until" [Executable, Bool] { {not} concat @>_while_cond @>_while_body @!_while_body _begin_loop_ _while_ _end_loop_} }]
Describe this revision:
Save