Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary Files in Prolog

Status
Not open for further replies.

ErzenWald

Programmer
Nov 2, 2010
1
0
0
RO
Hello.

Does anyone know how to write to a binary in Prolog? For reference, I'm using Turbo Prolog.
 
I don't know specifics about Turbo Prolog. In SWI-Prolog you write binary data using put_byte.

Code:
put_byte(+Byte)                                   [ISO]
    Write a single byte  to the output.  Byte must be an integer between
    0 and 255.

put_byte(+Stream, +Byte)                          [ISO]
    Write a single byte  to a stream.  Byte must be an integer between 0
    and 255.
 
this program calculates the binary of any number. all you must do is write binario(7,X) for example.

% |0 se x<y
% div(x,y)|
% |1+div(x-y,y) se x>=y

% div(4,2,X).

div(X,Y,0):-X<Y.
div(X,Y,Z):-X>=Y,
X1 is X-Y,
div(X1,Y,Z1),Z is 1+Z1.


%calculates the quotients of a divisao
% lstQuo(120,2,L).

lstQuo(X,Y,[]):-X < Y.
lstQuo(X,Y,R):-X >= Y,
div(X,Y,Z),add(X,L,R),lstQuo(Z,Y,L).


%rest of the divisao of natural numbers with successives subtractions


% |x se x<y
% mod(x,y)|
% |mod(x-y,y) se x>=y


%resto(234,24,X).
resto(X,Y,X):-X<Y.
resto(X,Y,Z):-X>=Y,
X1 is X-Y,
resto(X1,Y,Z).



%calculates the remains of a list
% lstResto([1,2,3,4,5,6],[2],S).

lstResto([],_,[]).
lstResto([E|T],R,[N|S]):-resto(E,R,N),lstResto(T,R,S).

%insert in the begining of the list
add(X,L,[X|L]).


%calculates the binary number
% binario(7,S).

binario(0,[0]):-!.
binario(1,[0,1]):-!.
binario(X,R):-lstQuo(X,2,L),lstResto(L,2,S),reverse(S,B),add(1,B,R).


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top