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 100 fizzbuzzes
This revision created on Fri, 8 Mar 2024 18:47:34 by CapitalEx (Add Kitten)