Prolog Text squares_ex

/**
 * Prolog code for the squares example 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).

:- multifile goal_expansion/2.

goal_expansion(forthem(C,A), \+ (C, \+ A)).

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

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

Comments