Download code
From LiteratePrograms
Back to Fibonacci_numbers_(occam)
Download for Windows: single file, zip
Download for UNIX: single file, zip, tar.gz, tar.bz2
fibnum.occ
1 -- Copyright (c) 2008 the authors listed at the following URL, and/or 2 -- the authors of referenced articles or incorporated external code: 3 -- http://en.literateprograms.org/Fibonacci_numbers_(occam)?action=history&offset=20080113094659 4 -- 5 -- Permission is hereby granted, free of charge, to any person obtaining 6 -- a copy of this software and associated documentation files (the 7 -- "Software"), to deal in the Software without restriction, including 8 -- without limitation the rights to use, copy, modify, merge, publish, 9 -- distribute, sublicense, and/or sell copies of the Software, and to 10 -- permit persons to whom the Software is furnished to do so, subject to 11 -- the following conditions: 12 -- 13 -- The above copyright notice and this permission notice shall be 14 -- included in all copies or substantial portions of the Software. 15 -- 16 -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 -- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 -- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 -- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 -- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 -- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 -- 24 -- Retrieved from: http://en.literateprograms.org/Fibonacci_numbers_(occam)?oldid=12090 25 26 INT FUNCTION fib (VAL INT n) 27 INT fib.num, next.fib.num: 28 VALOF 29 SEQ 30 fib.num, next.fib.num := 0, 1 31 SEQ i = 0 FOR n 32 fib.num, next.fib.num := next.fib.num, (fib.num + next.fib.num) 33 RESULT fib.num 34 : 35 36 PROTOCOL FIB 37 CASE 38 quit 39 reset 40 give.num 41 : 42 43 PROC fib.gen (CHAN FIB in, CHAN INT out) 44 INT fib.num, next.fib.num: 45 46 PROC init () 47 fib.num, next.fib.num := 0, 1 48 : 49 50 SEQ 51 init() 52 INITIAL BOOL done IS FALSE: 53 WHILE NOT done 54 in ? CASE 55 quit 56 done := TRUE 57 reset 58 init() 59 give.num 60 SEQ 61 out ! fib.num 62 fib.num, next.fib.num := next.fib.num, (fib.num + next.fib.num) 63 64 : 65 66 PROC fib.test (CHAN BYTE scr!) 67 CHAN FIB tell.fib: 68 CHAN INT from.fib: 69 70 PROC fib.gen.print (VAL INT max.fib.num) 71 INT fib.num: 72 SEQ n = 0 FOR max.fib.num 73 SEQ 74 tell.fib ! give.num 75 from.fib ? fib.num 76 out.int(fib.num, 0, scr) 77 scr ! ' ' 78 : 79 80 81 SEQ 82 out.string("Function:*n", 0, scr) 83 SEQ n = 0 FOR 40 84 SEQ 85 out.int(fib(n), 0, scr) 86 scr ! ' ' 87 88 out.string("*n*nGenerator:*n", 0, scr) 89 PAR 90 fib.gen(tell.fib, from.fib) 91 SEQ 92 fib.gen.print(40) 93 scr ! '*n' 94 tell.fib ! reset 95 fib.gen.print(40) 96 scr ! '*n' 97 tell.fib ! quit 98 99 : 100
