summaryrefslogtreecommitdiff
path: root/doc/0_05_fizzbuzz.adoc
blob: c6f439d90264bfe6568671c6621cce42628483b4 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
FizzBuzz
========

== Slightly modified example of thesis: Figure 4.4, page 87

The colm language has evolved since it has been described in the thesis.
With some modifications we can reactivate this example.

[source,chapel]
.figure_44.lm
----
i: int = 0
j: int = i

while ( i < 10 ) {
    if ( i * ( 10 - i ) < 20 ) {
        print ( "hello ", i, ' ', j , '\n' )
        j = j+ 1
    }
    i = i + 1
}
----

Please Note:

* the syntax is very c-ish
* the variables are defined with their type
* there is no postfix increment operator (i = i +1)

[source,bash]
----
/opt/colm/bin/colm fizzbuzz.lm
./fizzbuzz
----

That gives us:
----
hello 0 0
hello 1 1
hello 2 2
hello 8 3
hello 9 4
----

== Real FizzBuzz

The fizzbuzz test is often used to check is someone has programming skils.
It is the next logical step to 'hello world'.

[source,chapel]
.fizzbuzz.lm
----
int modulo( value:int, div:int) {
    times:int = value / div
    return value - ( times * div )
}

i:int = 0
while( i < 20 ) {
    mod5:int = modulo( i, 5 )
    mod3:int = modulo( i, 3 )
    if ( mod5 == 0 && mod3 == 0 ) {
        print( "FizzBuzz\n" )
    } elsif( mod5 == 0 ) {
        print( "Buzz\n" )
    } elsif( mod3 == 0 ) {
        print( "Fizz\n" )
    } else {
        print( i, "\n" )
    }
    i = i + 1
}
----

It appears that there is no modulo operator ('%').
Therefor we'll resort to a function.
Writing a function seems rather straight forward

Please note:
* that '&&' is working.
* The return type is needed, but if 'nil' is retuned by default.

[source,bash]
----
/opt/colm/bin/colm fizzbuzz.lm
./fizzbuzz
----

That gives us
----
FizzBuzz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
----