Download code

Jump to: navigation, search

Back to Fibonacci_numbers_(Oz)

Download for Windows: single file, zip

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

Fib.oz

 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_(Oz)?oldid=16948
14 
15 functor
16 import
17    System
18    Application
19 define
20    fun {RecursiveFib N}
21       if N < 2 then N else {RecursiveFib N - 1} + {RecursiveFib N - 2} end
22    end
23    Memo = {NewDictionary}
24    fun {MemoizedFib N}
25       if {Dictionary.member Memo N}
26       then {Dictionary.get Memo N}
27       else
28          FibN
29       in
30          if N < 2 then FibN = N else FibN = {MemoizedFib N - 1} + {MemoizedFib N - 2} end
31          {Dictionary.put Memo N FibN}
32          FibN
33       end
34    end
35    fun {IterativeFib N}
36       fun {Fib N F1 F2}
37         if N < 1 then F1 else {Fib (N - 1) F2 (F1 + F2)} end
38       end
39    in
40       {Fib N 0 1}
41    end
42    proc {RunFib F}
43       for X in 0..30 do
44          {System.showInfo "{" # {System.printName F} # " " # X # "} = " # {F X}}
45       end
46    end
47    {RunFib RecursiveFib}
48    {RunFib MemoizedFib}
49    {RunFib IterativeFib}
50    {Application.exit 0}
51 end
52 


hijacker
hijacker
hijacker
hijacker