Download code
From LiteratePrograms
Back to Fibonacci_numbers_(Mercury)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
fibonacci.m
1 % Copyright (c) 2010 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_(Mercury)?action=history&offset=20081029210206 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_(Mercury)?oldid=15357 25 26 :- module fibonacci. 27 28 :- interface. 29 30 :- func fibonacci(int) = int. 31 32 /* an alternative way, using predicates would be 33 * :- pred fibonacci( int, int). 34 */ 35 36 /* mode first_param as input '=' result as output is deterministic (has one solution) 37 */ 38 39 :- mode fibonacci(in) = out is det. 40 41 :- implementation. 42 43 :- import_module int. 44 45 /* fibonacci(N) = F implies ( cond_goal -> then_goal 46 * ; cond_goal -> then_goal 47 * ; else_goal 48 * ) 49 */ 50 51 fibonacci(N) = F :- ( N = 0 -> F = 0 52 ; 53 N = 1 -> F = 1 54 ; 55 /* else */ 56 F = fibonacci(N - 1) + fibonacci(N - 2) 57 ). 58
fibonacci_tr.m
1 % Copyright (c) 2010 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_(Mercury)?action=history&offset=20081029210206 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_(Mercury)?oldid=15357 25 26 :- module fibonacci_tr. 27 28 :- interface. 29 30 :- func fibonacci(int) = int. 31 :- mode fibonacci(in) = out is det. 32 33 :- implementation. 34 35 :- import_module int. 36 37 fibonacci(N) = F :- F = fibo_tr( N, 1, 0). 38 39 /* tail recursive private function */ 40 41 :- func fibo_tr(int, int, int) = int. 42 :- mode fibo_tr(in, in, in) = out is det. 43 44 fibo_tr(N, Next, Result) = F :- 45 46 ( N = 0 -> F = Result 47 48 ; 49 /* else */ 50 F = fibo_tr(N - 1, Result + Next, Next) 51 ). 52
fibotest.m
1 % Copyright (c) 2010 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_(Mercury)?action=history&offset=20081029210206 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_(Mercury)?oldid=15357 25 26 27 :- module fibotest. 28 29 :- interface. 30 31 :- import_module io. 32 33 :- pred main(io.state, io.state). 34 :- mode main(di, uo) is det. 35 36 :- implementation. 37 38 :- import_module fibonacci. 39 40 main --> 41 io.write_string("fibonacci(7) = "), 42 io.write_int(fibonacci(7)), 43 io.nl. 44
