new operators mod k and is mod k

Just a couple more simple arithmetic operators, mod[k] and is-mod[k]. They do pretty much what they say they do. One for example usage, is the fizz-buzz exercise.

Quick look in the console:
sa: mod[3] |number: 12>
|number: 0>

sa: mod[3] |8>
|2>

sa: is-mod[7] |50>
|no>

sa: is-mod[7] |96889010407>
|yes>

sa: is-mod[3] |category 1: category 2: category 3: 286725>
|yes>
And I guess we should note that they work independent of the category/data-type (as do the other simple arithmetic operators). Here the first one has data-type "number". Those in the middle have no data-type. And the last one has "category 1: category 2: category 3".

Now, on to FizzBuzz. Here are the exercise details:
-- task:
-- For numbers 1 through 100,

-- if the number is divisible by 3 print Fizz;
-- if the number is divisible by 5 print Buzz;
-- if the number is divisible by 3 and 5 (15) print FizzBuzz;
-- else, print the number.
Here are three alternate potential ways to do it, but the incomplete parser meant only the third version actually worked:
|context> => |context: Fizz Buzz exercise in BKO: v1>
|list> => range(|1>,|100>)
fizz-buzz-0 |*> #=> |_self>
fizz-buzz-1 |*> #=> if(arithmetic(|_self>,|%>,|3>) == |0>,|Fizz>,|>)
fizz-buzz-2 |*> #=> if(arithmetic(|_self>,|%>,|5>) == |0>,|Buzz>,|>)
fizz-buzz-3 |*> #=> if(arithmetic(|_self>,|%>,|15>) == |0>,|FizzBuzz>,|>)

map[fizz-buzz-0,fizz-buzz] "" |list>
map[fizz-buzz-1,fizz-buzz] "" |list>
map[fizz-buzz-2,fizz-buzz] "" |list>
map[fizz-buzz-3,fizz-buzz] "" |list>



|context> => |context: Fizz Buzz exercise in BKO: v2>
|list> => range(|1>,|100>)
is-zero |*> => |False>
is-zero |0> => |True>
fizz-buzz-0 |*> #=> |_self>
-- current parser can't handle the nested ()
fizz-buzz-1 |*> #=> if(is-zero arithmetic(|_self>,|%>,|3>),|Fizz>,|>)
fizz-buzz-2 |*> #=> if(is-zero arithmetic(|_self>,|%>,|5>),|Buzz>,|>)
fizz-buzz-3 |*> #=> if(is-zero arithmetic(|_self>,|%>,|15>),|FizzBuzz>,|>)

map[fizz-buzz-0,fizz-buzz] "" |list>
map[fizz-buzz-1,fizz-buzz] "" |list>
map[fizz-buzz-2,fizz-buzz] "" |list>
map[fizz-buzz-3,fizz-buzz] "" |list>



|context> => |context: Fizz Buzz exercise in BKO: v3>
-- define our list
|list> => range(|1>,|100>)

-- define is-zero function
is-zero |*> => |False>
is-zero |0> => |True>

-- define is-mod functions
is-mod-3 |*> #=> is-zero arithmetic(|_self>,|%>,|3>)
is-mod-5 |*> #=> is-zero arithmetic(|_self>,|%>,|5>)
is-mod-15 |*> #=> is-zero arithmetic(|_self>,|%>,|15>)

-- apply them
map[is-mod-3] "" |list>
map[is-mod-5] "" |list>
map[is-mod-15] "" |list>

-- define fizz-buzz functions
fizz-buzz-0 |*> #=> |_self>
fizz-buzz-1 |*> #=> if(is-mod-3 |_self>,|Fizz>,|>)
fizz-buzz-2 |*> #=> if(is-mod-5 |_self>,|Buzz>,|>)
fizz-buzz-3 |*> #=> if(is-mod-15 |_self>,|FizzBuzz>,|>)

-- apply them
map[fizz-buzz-0,fizz-buzz] "" |list>
map[fizz-buzz-1,fizz-buzz] "" |list>
map[fizz-buzz-2,fizz-buzz] "" |list>
map[fizz-buzz-3,fizz-buzz] "" |list>
Now we have is-mod[k] we can give a nice pretty and tidy version:
|context> => |context: Fizz Buzz exercise, is-mod version>
the |list> => range(|1>,|100>)

fizz-buzz-0 |*> #=> |_self>
fizz-buzz-1 |*> #=> if(is-mod[3] |_self>,|Fizz>,|>)
fizz-buzz-2 |*> #=> if(is-mod[5] |_self>,|Buzz>,|>)
fizz-buzz-3 |*> #=> if(is-mod[15] |_self>,|FizzBuzz>,|>)

|null> => map[fizz-buzz-0,fizz-buzz] the |list>
|null> => map[fizz-buzz-1,fizz-buzz] the |list>
|null> => map[fizz-buzz-2,fizz-buzz] the |list>
|null> => map[fizz-buzz-3,fizz-buzz] the |list>
And here is the result:
sa: load fizz-buzz--is-mod.sw
sa: dump
----------------------------------------
|context> => |context: Fizz Buzz exercise, is-mod version>

the |list> => |1> + |2> + |3> + |4> + |5> + |6> + |7> + |8> + |9> + |10> + |11> + |12> + |13> + |14> + |15> + |16> + |17> + |18> + |19> + |20> + |21> + |22> + |23> + |24> + |25> + |26> + |27> + |28> + |29> + |30> + |31> + |32> + |33> + |34> + |35> + |36> + |37> + |38> + |39> + |40> + |41> + |42> + |43> + |44> + |45> + |46> + |47> + |48> + |49> + |50> + |51> + |52> + |53> + |54> + |55> + |56> + |57> + |58> + |59> + |60> + |61> + |62> + |63> + |64> + |65> + |66> + |67> + |68> + |69> + |70> + |71> + |72> + |73> + |74> + |75> + |76> + |77> + |78> + |79> + |80> + |81> + |82> + |83> + |84> + |85> + |86> + |87> + |88> + |89> + |90> + |91> + |92> + |93> + |94> + |95> + |96> + |97> + |98> + |99> + |100>

fizz-buzz-0 |*> #=> |_self>
fizz-buzz-1 |*> #=> if(is-mod[3] |_self>,|Fizz>,|>)
fizz-buzz-2 |*> #=> if(is-mod[5] |_self>,|Buzz>,|>)
fizz-buzz-3 |*> #=> if(is-mod[15] |_self>,|FizzBuzz>,|>)

fizz-buzz |1> => |1>
fizz-buzz |2> => |2>
fizz-buzz |3> => |Fizz>
fizz-buzz |4> => |4>
fizz-buzz |5> => |Buzz>
fizz-buzz |6> => |Fizz>
fizz-buzz |7> => |7>
fizz-buzz |8> => |8>
fizz-buzz |9> => |Fizz>
fizz-buzz |10> => |Buzz>
fizz-buzz |11> => |11>
fizz-buzz |12> => |Fizz>
fizz-buzz |13> => |13>
fizz-buzz |14> => |14>
fizz-buzz |15> => |FizzBuzz>
fizz-buzz |16> => |16>
fizz-buzz |17> => |17>
fizz-buzz |18> => |Fizz>
fizz-buzz |19> => |19>
fizz-buzz |20> => |Buzz>
fizz-buzz |21> => |Fizz>
fizz-buzz |22> => |22>
fizz-buzz |23> => |23>
fizz-buzz |24> => |Fizz>
fizz-buzz |25> => |Buzz>
fizz-buzz |26> => |26>
fizz-buzz |27> => |Fizz>
fizz-buzz |28> => |28>
fizz-buzz |29> => |29>
fizz-buzz |30> => |FizzBuzz>
fizz-buzz |31> => |31>
fizz-buzz |32> => |32>
fizz-buzz |33> => |Fizz>
fizz-buzz |34> => |34>
fizz-buzz |35> => |Buzz>
fizz-buzz |36> => |Fizz>
fizz-buzz |37> => |37>
fizz-buzz |38> => |38>
fizz-buzz |39> => |Fizz>
fizz-buzz |40> => |Buzz>
fizz-buzz |41> => |41>
fizz-buzz |42> => |Fizz>
fizz-buzz |43> => |43>
fizz-buzz |44> => |44>
fizz-buzz |45> => |FizzBuzz>
fizz-buzz |46> => |46>
fizz-buzz |47> => |47>
fizz-buzz |48> => |Fizz>
fizz-buzz |49> => |49>
fizz-buzz |50> => |Buzz>
fizz-buzz |51> => |Fizz>
fizz-buzz |52> => |52>
fizz-buzz |53> => |53>
fizz-buzz |54> => |Fizz>
fizz-buzz |55> => |Buzz>
fizz-buzz |56> => |56>
fizz-buzz |57> => |Fizz>
fizz-buzz |58> => |58>
fizz-buzz |59> => |59>
fizz-buzz |60> => |FizzBuzz>
fizz-buzz |61> => |61>
fizz-buzz |62> => |62>
fizz-buzz |63> => |Fizz>
fizz-buzz |64> => |64>
fizz-buzz |65> => |Buzz>
fizz-buzz |66> => |Fizz>
fizz-buzz |67> => |67>
fizz-buzz |68> => |68>
fizz-buzz |69> => |Fizz>
fizz-buzz |70> => |Buzz>
fizz-buzz |71> => |71>
fizz-buzz |72> => |Fizz>
fizz-buzz |73> => |73>
fizz-buzz |74> => |74>
fizz-buzz |75> => |FizzBuzz>
fizz-buzz |76> => |76>
fizz-buzz |77> => |77>
fizz-buzz |78> => |Fizz>
fizz-buzz |79> => |79>
fizz-buzz |80> => |Buzz>
fizz-buzz |81> => |Fizz>
fizz-buzz |82> => |82>
fizz-buzz |83> => |83>
fizz-buzz |84> => |Fizz>
fizz-buzz |85> => |Buzz>
fizz-buzz |86> => |86>
fizz-buzz |87> => |Fizz>
fizz-buzz |88> => |88>
fizz-buzz |89> => |89>
fizz-buzz |90> => |FizzBuzz>
fizz-buzz |91> => |91>
fizz-buzz |92> => |92>
fizz-buzz |93> => |Fizz>
fizz-buzz |94> => |94>
fizz-buzz |95> => |Buzz>
fizz-buzz |96> => |Fizz>
fizz-buzz |97> => |97>
fizz-buzz |98> => |98>
fizz-buzz |99> => |Fizz>
fizz-buzz |100> => |Buzz>

 |null> => |map>
----------------------------------------
And I guess that is it for this post.


Home
previous: new feature list 2 sp
next: introducing the if then machine

updated: 19/12/2016
by Garry Morrison
email: garry -at- semantic-db.org