Align columns: Difference between revisions

Content added Content deleted
(→‎{{header|Prolog}}: Marked incorrect due to the presence of the extra quotes in the output.)
Line 1,468: Line 1,468:


=={{header|Prolog}}==
=={{header|Prolog}}==
Works with SWI-Prolog.
{{incorrect|Prolog|Due to the presence of the extra quotes in the output.}}
Works with SWI-Prolog.<BR>
Prolog has a particular treatment for strings : any string beginning by an uppercase letter is enclosed with quotes, ditto for a word ending with a comma or a point.
<lang Prolog>aligner :-
<lang Prolog>aligner :-
L ="Given$a$text$file$of$many$lines,$where$fields$within$a$line$
L ="Given$a$text$file$of$many$lines,$where$fields$within$a$line$
Line 1,484: Line 1,482:
% each line is a list of words
% each line is a list of words
parse(L, 0, N, LP, []),
parse(L, 0, N, LP, []),

% Because of the particular treatment of the strings by Prolog
% we need to add 3 and not 1
% we need to add 1 to aligned
N1 is N+3,
N1 is N+1,
% words will be left aligned
% words will be left aligned
sformat(AL, '%~wl', [N1]),
sformat(AL, '~~w~~t~~~w|', [N1]),
% words will be centered
% words will be centered
sformat(AC, '%~wc', [N1]),
sformat(AC, '~~t~~w~~t~~~w|', [N1]),
% words will be right aligned
% words will be right aligned
sformat(AR, '%~wr', [N1]),
sformat(AR, '~~t~~w~~~w|', [N1]),

write('Left justified :'), nl,
maplist(affiche(AL), LP), nl,
maplist(affiche(AL), LP), nl,
write('Centered justified :'), nl,
maplist(affiche(AC), LP), nl,
maplist(affiche(AC), LP), nl,
write('Right justified :'), nl,
maplist(affiche(AR), LP), nl.
maplist(affiche(AR), LP), nl.


affiche(F, L) :-
affiche(F, L) :-
maplist(my_writef(F), L),
maplist(my_format(F), L),
nl.
nl.


my_writef(_F, [13]) :-
my_format(_F, [13]) :-
nl.
nl.


my_writef(F, W) :-
my_format(F, W) :-
string_to_atom(W,AW),
writef(F, [W]).
sformat(AF, F, [AW]),
write(AF).




Line 1,548: Line 1,552:
Output :
Output :
<lang Prolog> ?- aligner.
<lang Prolog> ?- aligner.
Left justified :
'Given' a text file of many 'lines,' where fields within a line
are delineated by a single '''dollar''' 'character,' write a program
Given a text file of many lines, where fields within a line
that aligns each column of fields by ensuring that words in each
are delineated by a single 'dollar' character, write a program
column are separated by at least one 'space.'
that aligns each column of fields by ensuring that words in each
'Further,' allow for each word in a column to be either left
column are separated by at least one space.
'justified,' right 'justified,' or center justified within its 'column.'
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.

Centered justified :
Given a text file of many lines, where fields within a line
are delineated by a single 'dollar' character, write a program
that aligns each column of fields by ensuring that words in each
column are separated by at least one space.
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.


Right justified :
'Given' a text file of many 'lines,' where fields within a line
are delineated by a single '''dollar''' 'character,' write a program
Given a text file of many lines, where fields within a line
that aligns each column of fields by ensuring that words in each
are delineated by a single 'dollar' character, write a program
column are separated by at least one 'space.'
that aligns each column of fields by ensuring that words in each
'Further,' allow for each word in a column to be either left
column are separated by at least one space.
'justified,' right 'justified,' or center justified within its 'column.'
Further, allow for each word in a column to be either left
justified, right justified, or center justified within its column.


true .
'Given' a text file of many 'lines,' where fields within a line
are delineated by a single '''dollar''' 'character,' write a program
that aligns each column of fields by ensuring that words in each
column are separated by at least one 'space.'
'Further,' allow for each word in a column to be either left
'justified,' right 'justified,' or center justified within its 'column.'
</lang>
</lang>