From cf964e43777bcce2e938964a5c764d1318e52ea6 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sun, 10 Mar 2024 10:54:28 -0700 Subject: [PATCH] (fizzbuzz) Modernized --- projects/examples/exercises/fizzbuzz.tal | 45 +++++++++--------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/projects/examples/exercises/fizzbuzz.tal b/projects/examples/exercises/fizzbuzz.tal index a53eaa2..704fecd 100644 --- a/projects/examples/exercises/fizzbuzz.tal +++ b/projects/examples/exercises/fizzbuzz.tal @@ -1,33 +1,20 @@ -( FizzBuzz: From 1 to 100, for multiples of 3 print "Fizz", of 5 "Buzz" and for both "FizzBuzz" ) +( 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 ) -@on-reset ( -> ) - #6400 - &loop ( -- ) - DUP - #2018 DEO - DUP #03 DIVk MUL SUB ?{ ;dict/fizz / } - DUP #05 DIVk MUL SUB ?{ ;dict/buzz / } +@fizzbuzz ( -> ) + #1e00 + &>loop ( length i -- ) + DUP fizz OVR buzz ORA ?{ DUP } #0a18 DEO - INC GTHk ?&loop - POP2 - ( exit ) #800f DEO - BRK + INC GTHk ?&>loop + POP2 BRK -@ ( num -- ) - ( x0 ) DUP #0a DIV - ( 0x ) #0a DIVk MUL SUB - ( >> ) - -@ ( num -- ) - #30 ADD #18 DEO - JMP2r - -@ ( addr* -- ) - LDAk #18 DEO - INC2 & LDAk ? - POP2 JMP2r - -@dict ( strings ) - &fizz "Fizz $1 - &buzz "Buzz $1 +@fizz ( n -- ) #03 DIVk MUL SUB ?{ #01 ;Dict/fizz ! } #00 JMP2r +@buzz ( n -- ) #05 DIVk MUL SUB ?{ #01 ;Dict/buzz ! } #00 JMP2r +@ ( n -- ) DUP #0a DIV /d #0a DIVk MUL SUB &d #30 ADD #18 DEO JMP2r +@ ( s* -- ) LDAk #18 DEO INC2 LDAk ? POP2 JMP2r +@Dict &fizz "Fizz $1 &buzz "Buzz $1