B-Prolog 7.4 #3
Version of implementation B-Prolog of programming language PrologA version of B-Prolog, released on August 18, 2010.
Changes:
- A bugfix in tabling
- An improvement in the unification procedure for domain variables
- Improve the implementation of mode-directed tabling
Examples:
Factorial - Prolog (238):
Almost identical to Poplog Prolog example, except for the syntax of compiling a file (doesn’t have a -
before file name). However, the results of execution depend on the implementation. SWI-Prolog handles large numbers just fine, while in GNU Prolog and B-Prolog 12! overflows the numeric data type, so all values after 11! are incorrect.
| ?- [fact].
compiling /home/nickolas/Desktop/progopedia/prolog/fact.pl for byte code…
/home/nickolas/Desktop/progopedia/prolog/fact.pl compiled, 3 lines read — 1372 bytes written, 5 ms
yes
| ?- fact(16,X).
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = -57869312
13! = -215430144
14! = 205203456
15! = -143173632
16! = -143294464
X = -143294464 ?`
% fact.pl
fact(X, F) :-
( X=0, F=1;
Y is X-1, fact(Y, Z), F is X*Z),
write(X), write('! = '), write(F), nl.
% interactive
[fact].
fact(16,X).
Fibonacci numbers - Prolog (239):
Once again, the example is almost identical to Poplog Prolog one, except for the syntax of compiling/consulting a file.
% fibonacci.pl
:- dynamic(stored/1).
memo(Goal) :-
stored(Goal) -> true;
Goal, assertz(stored(Goal)).
fib(1,1) :- !, write('1, ').
fib(2,1) :- !, write('1, ').
fib(N,F) :-
N1 is N-1, memo(fib(N1,F1)),
N2 is N-2, memo(fib(N2,F2)),
F is F1 + F2,
write(F), write(', ').
% interactive
[fibonacci].
fib(16,X), write('...'), nl.
Hello, World! - Prolog (49):
This example doesn’t need any facts or rules to be evaluated. The query is executed in interactive mode, and results in the following output:
Hello, World!
yes
First line is the actual output of write
predicate, and second line is the result of query evaluation.
Note that replacing single-quotes with double-quotes makes Prolog output the string as an array of ASCII-codes of individual characters:
| ?- write("Hello, World!").
[72,101,108,108,111,44,32,87,111,114,108,100,33]
yes
write('Hello, World!'), nl.
Comments
]]>blog comments powered by Disqus
]]>