Download code

Jump to: navigation, search

Back to Fibonacci_numbers_(occam)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

fibnum.occ

 1 -- The authors of this work have released all rights to it and placed it
 2 -- in the public domain under the Creative Commons CC0 1.0 waiver
 3 -- (http://creativecommons.org/publicdomain/zero/1.0/).
 4 -- 
 5 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 -- 
13 -- Retrieved from: http://en.literateprograms.org/Fibonacci_numbers_(occam)?oldid=15829
14 
15 INT FUNCTION fib (VAL INT n)
16   INT fib.num, next.fib.num:
17   VALOF
18     SEQ
19       fib.num, next.fib.num := 0, 1
20       SEQ i = 0 FOR n
21         fib.num, next.fib.num := next.fib.num, (fib.num + next.fib.num)
22     RESULT fib.num
23 :
24 
25 PROTOCOL FIB
26   CASE
27     quit
28     reset
29     give.num
30 :
31 PROC fib.gen (CHAN FIB in, CHAN INT out)
32   INT fib.num, next.fib.num:
33     
34   PROC init ()
35     fib.num, next.fib.num := 0, 1
36   :
37   SEQ
38     init()
39     INITIAL BOOL done IS FALSE:
40     WHILE NOT done
41       in ? CASE
42         quit
43           done := TRUE
44         reset
45           init()
46         give.num
47           SEQ
48             out ! fib.num
49             fib.num, next.fib.num := next.fib.num, (fib.num + next.fib.num)
50 :
51 PROC fib.test (CHAN BYTE scr!)
52   CHAN FIB tell.fib:
53   CHAN INT from.fib:
54   
55   PROC fib.gen.print (VAL INT max.fib.num)
56     INT fib.num:
57     SEQ n = 0 FOR max.fib.num
58       SEQ
59         tell.fib ! give.num
60         from.fib ? fib.num
61         out.int(fib.num, 0, scr)
62         scr ! ' '
63   :
64   
65   SEQ
66     out.string("Function:*n", 0, scr)
67     SEQ n = 0 FOR 40
68       SEQ
69         out.int(fib(n), 0, scr)
70         scr ! ' '
71     out.string("*n*nGenerator:*n", 0, scr)
72     PAR
73       fib.gen(tell.fib, from.fib)
74       SEQ
75         fib.gen.print(40)
76         scr ! '*n'
77         tell.fib ! reset
78         fib.gen.print(40)
79         scr ! '*n'
80         tell.fib ! quit    
81 :


hijacker
hijacker
hijacker
hijacker