Concatenative topics
Concatenative meta
Other languages
Meta
Print the first 30 numbers using the following rules:
n is divisible by 3, print "fizz"n is divisible by 5, print "buzz"n is divisible by both, print "fizzbuzz": fizzify ( sbuf m n string -- ) [ divisor? ] dip '[ _ append! ] when drop ; : fizzbuzz* ( n -- ) 0 <sbuf> swap { [ 3 "fizz" fizzify ] [ 5 "buzz" fizzify ] [ '[ _ >dec ] when-empty print ] } 2cleave ; : fizzbuzz ( n -- ) [1..b] [ fizzbuzz* ] each ;
Retrieved from: https://github.com/evincarofautumn/kitten/blob/master/examples/fizz-buzz.ktn
define divisible (Int32, Int32 -> Bool +Fail):
(%) 0 (=)
define fizzbuzz (Int32 -> List<Char>):
-> n;
do (with (+Fail)):
n 5 divisible
n 3 divisible
if:
if: "FizzBuzz"
else: "Fizz"
else:
if: "Buzz"
else: n show
define fizzbuzzes (Int32, Int32 -> +IO):
-> c, m;
c fizzbuzz say
if (c < m): (c + 1) m fizzbuzzes
1 30 fizzbuzzesRetrieved from: https://github.com/rtulip/haystack/blob/main/examples/fizzbuzz.hay
fn fizzbuzz(u64: n) { n 15 % 0 == if { "FizzBuzz" println } else n 5 % 0 == if { "Buzz" println } else n 3 % 0 == if { "Fizz" println } else { n println } } fn main() { 0 while dup 30 < do { as [i] "i: " print i print " -> " print i fizzbuzz i 1 + } drop }
@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 $1using std.manip
using std.quote
using std.cond
using std.out
fizzbuzz == [
: 15 % 0 =
["FizzBuzz" .nl /]
[
: 5 % 0 =
["Buzz" .nl /]
[
: 3 % 0 =
["Fizz" .nl /]
[.nl]
⍉ ?
]
⍉ ?
]
⍉ ?
]
1 30 .. [: fizzbuzz] ∀ /
This revision created on Thu, 21 Mar 2024 21:22:20 by vatsjijj (Add a Titan example.)