% Author:   Sallam
% http://sallamah.weebly.com
% Date: 5/5/2014

% ****--- read- write operations

mysum:-read(X), read(Y), Z is X+Y, write(Z).

% pretty print pp


/*
[1,2,3, [2,4], 7, 8]

         1
         2
         3
            2
            4
         7
         8
*/
pp(X):- pp(X,0).
pp([H|T], I):-!, J is I+3, pp(H,J), ppx(T, J).
pp(H,I):-space(I), write(H), nl.

ppx([],_).
ppx([H|T], I):- pp(H,I),ppx(T,I).

space(0):-!.
space(I):- write(' '), J is I-1, space(J).


/*spaces(0) :- !.
spaces(N) :- write(' '), N1 is N - 1, spaces(N1).

pp(X):- pp(X, 0).           % why we need this line

pp([H|T], I) :- !, J is I + 3, pp(H, J), ppx(T, J), nl.
pp(X, I) :- spaces(I), write(X), nl.

ppx([],_).                  % what this predicate should return?
ppx([H|T], I) :- pp(H, I), ppx(T, I). */


% ****---------------- write , write_canonical
% try ?- write(a+b*c*c), nl, write_canonical(a+b*c*c), nl.

% what happen with ?- write("Hello"). what about ?- write('Hello\n').

% ****--- characters

isO:- get_char(X), checkO(X).

checkO(X):- X='O'; X='o'.


checkO(_):- write('Opssss.....try again!'),isO.




/*isO :- write('Enter O:'), get_char(X), checkO(X).

checkO('o'):-!.
checkO('O'):-!.

checkO(_):- write('Opsssss, again ..\n'), isO. */

% check put_char(x).



% ***** files

read_file:- get_char(C), show(C).

show('end_of_file'):- !.
show(C):- put_char(C),read_file.

program:-
         open('d:\\a.txt',read, X ),
         current_input(S),
         set_input(X),
         read_file,
         set_input(S),
         close(X).
         
         

/*program :-
        open('c:\\a.txt',read, X),
        current_input(Stream),
        set_input(X),
        read_file,
        %consult(X),
        close(X),
        set_input(Stream).*/
        





  db:-consult('d:\\db').
