Prolog Text squares

/**
 * Prolog code for the squares example non-expanding.
 *
 * Copyright 2011, XLOG Technologies GmbH, Switzerland
 * Jekejeke Prolog 0.9.1 (a fast and small prolog interpreter)
 */

between(Lo,Hi,_) :- Lo>Hi, !, fail.
between(Lo,_,Lo).
between(Lo,Hi,X) :- Lo2 is Lo+1, between(Lo2,Hi,X).

spaces(N) :- N=<0, !.
spaces(N) :- write(' '), M is N-1, spaces(M).

radjust(H) :-
    number_codes(H,C),
    atom_codes(A,C),
    atom_length(A,L),
    R is 3-L,
    spaces(R),
    write(A).

forthem(C,A) :-
    \+ (C, \+ A).

printf(S,[]) :-
    write(S).
printf(S,[E|T]) :-
    sub_atom(S,B,_,A,'%'),
    sub_atom(S,0,B,_,P),
    write(P),
    H is E,
    radjust(H),
    sub_atom(S,_,A,0,Q),
    printf(Q,T).

squares :-
    forthem(between(1,10,X),printf(' % * % = %\n',[X,X,X*X])).

Comments