uxn/projects/examples/exercises/fizzbuzz.tal

21 lines
647 B
Tal

( Print the first 30 numbers using the following rules:
| If n is divisible by 3, print "fizz"
| If n is divisible by 5, print "buzz"
| If n is divisible by both, print "fizzbuzz"
| Else, print the number )
@fizzbuzz ( -> )
#1e00
&>loop ( length i -- )
DUP fizz OVR buzz ORA ?{ DUP <dec> }
#0a18 DEO
INC GTHk ?&>loop
POP2 BRK
@fizz ( n -- ) #03 DIVk MUL SUB ?{ #01 ;Dict/fizz !<str> } #00 JMP2r
@buzz ( n -- ) #05 DIVk MUL SUB ?{ #01 ;Dict/buzz !<str> } #00 JMP2r
@<dec> ( n -- ) DUP #0a DIV /d #0a DIVk MUL SUB &d #30 ADD #18 DEO JMP2r
@<str> ( s* -- ) LDAk #18 DEO INC2 LDAk ?<str> POP2 JMP2r
@Dict &fizz "Fizz $1 &buzz "Buzz $1