Download code

From LiteratePrograms

Jump to: navigation, search

Back to Fibonacci_numbers_(Alice_ML)

Download for Windows: single file, zip

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

fib.aml

 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_(Alice_ML)?action=history&offset=20081009163225
 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_(Alice_ML)?oldid=14918
25 *)
26 
27 import functor MkRedBlackImpMap from "x-alice:/lib/data/MkRedBlackImpMap"
28 
29 fun fib 0 = 0
30   | fib 1 = 1
31   | fib n if (n > 1) = fib(n - 1) + fib(n - 2)
32   | fib _ = raise Domain
33 
34 local
35    structure Memo = MkRedBlackImpMap Int
36    val table = Memo.map()
37 in
38    fun memo_fib n =
39       let
40          val previously_computed_result = Memo.lookup(table, n)
41       in
42          case previously_computed_result of
43             | SOME item => item
44             | NONE =>
45                let
46                   fun fib 0 = 0
47                     | fib 1 = 1
48                     | fib n if (n > 1) = memo_fib(n - 1) + memo_fib(n - 2)
49                     | fib _ = raise Domain
50                   val result = fib n
51                in
52                   Memo.insert(table, n, result);
53                   result
54                end
55       end
56 end
57 
58 fun iterative_fib n =
59    let
60       fun fib (a, b, 0) = b
61         | fib (a, b, count) if (n > 0) = fib(a + b, a, count - 1)
62         | fib _ = raise Domain
63    in
64       fib(1, 0, n)
65    end
66 
67 fun lazy fibgen (a, b) =
68    a :: fibgen(b, a + b)
69 
70 val fibs = fibgen(0, 1)
71 
72 val inf_0 = IntInf.fromInt 0
73 val inf_1 = IntInf.fromInt 1
74 
75 fun inf_iterative_fib n =
76    let
77       fun fib (a, b, count) if (count = inf_0) = b : IntInf.t
78         | fib (a, b, count) if (count > inf_0) = fib(a + b, a, count - inf_1)
79         | fib _ = raise Domain
80    in
81       fib(inf_1, inf_0, n)
82    end
83 


Views
Personal tools