General fizzbuzz in Praxis
Torbjörn Josefsson
60 steps
6 minutes
Toblotron
1
Navigate to [https://toblotron.com/praxis/0.1.0/](https://toblotron.com/praxis/0.1.0/)
As soon as you come to the IDE you will get a new, empty model. Let's name it and add a rules-page
2
Type "fizzbuzz" as project name, followed by [[Return]].
3
Right-click the "fizzbuzz" project-node in the navigation menu, and add a rules-page
4
Click "Add rules page"
5
Here we see the new rules-page, per default named "Page #0" Drag out a Predicate shape to make your first rule
6
Name the rule "fizzbuzz", and add "Nr" and "Answer" as arguments
7
Click "Ok"
Now we have a rule, in the simplest form, but it doesn't do anything yet. Let's add a data table to store our factors and responses.
8
Right-click the "fizzbuzz" project node Click "Add data table"
9
Name the table "factors"
10
Click this dropdown to set column 1 as integer.
11
Name it "factor"
12
Set column 2 to be an atom (a kind of enum-like thing)
13
Name it "Response"
14
Click "Ok"
15
And now we can enter our two rows of data:
16
3, fizz 5, buzz
17
Click "Page # 0" to go back to our rules-page
18
Drag out a Table shape to refer to the data we just entered
19
Select the desired table
20
Name the variables "Factor" and "Answer" Click "Ok"
21
Click on background to de-select shape Drag out a Formula shape to do some calculations Enter "0 [[tab]] is [[tab]] Nr mod Factor" This line will be true if Nr id evenly divisible by Factor Click "Ok"
Why the "is" operator? Prolog (which is what Praxis works with) does not per default give you 3 if you enter 1+2 - because "1+2" might be exactly what you want to use; there is no clear line between data and code in Prolog. - If you want a numeric calculation, you must make that clear by using the "is" operator
22
Connect the shapes
Now let's try querying our rule. It should receive a Nr, and then look in the table to see if it can find a Factor by which the number is evenly divisible, and in that case return the value of the Response column for that Factor.
23
Select the "fizzbuzz" predicate to pre-fill the query-text with the values of that shape. Click "Nr" to enter a Nr in the query-box.
24
Type "3"
25
Click "Query"
26
We got an answer! ("Answer = fizz", in the output panel) Click "Next" to see if there are more answers
27
Nope - "false" means that no more answers were found; 3 could only be evenly divided by one of the factors.
28
Click "3" to try another number
29
Type "5"
30
Click "Query"
31
We got "Answer = buzz"! Click "Next"
32
Same thing here - only one answer Click "5" to enter another test
33
Enter "15" and click "Query"
34
We get "fizz", and when we click "Next" we also get "buzz" - because 15 is evenly divisible by both 3 and 5
35
But we don't want to have to ask for several answers - we want an entire "fizzbuzz" to be generated - let's change our rule We select and move the rule a bit..
36
Select this connection and [[Delete]] it
37
We want to find all the solutions, so let's use a Findall shape - it will find as many answers as it can, and collect the desired results Click this image. Click here.
38
Enter "Response" as the variable we want to remember for every success
39
And "Answer" as the name of the list where the responses will be stored Click "Ok"
40
Connect the shapes
41
Rename the variable "Answer" to "Response", in the Table shape, so we will capture this value with the Findall shape
42
Click on background to de-select the shape, so we get to see the Palette again
43
We want to write to the output, so let's call the built-in write-predicate
44
Click this text field.
45
Type "write"
46
"write" only has one argument, so click "-" to get rid the one we don't need
47
Click this text field and enter "Response", to write that value to the output window
48
Click "Ok"
49
And connect
50
Click "Query"
51
We get an error, because we need to re-generate the code to get the new rule Click "Reset" to do this
52
Click "Query"
53
We get an error, because the rule needs to have a number as argument #1 Double-click "Nr" Enter "3" Click "Query" button
54
Now we got "fizz"! Click "3"
55
Click "3"
56
Type "15"
57
Click "Query", and you will get "fizzbuzz" in the output
58
Click the "factors /2" table node to go there
59
Click the bottom leftmost cell to add a new row. Type "10 [[tab]] boom [[tab]]" Click "Page # 0" to go there
60
Click "Reset" to generate the new code Click "15", and enter "30" Click "Query" Now you should get "fizzbuzzboom"