Align columns: Difference between revisions
Content added Content deleted
(→{{header|Clojure}}: replaced code with shorter version) |
|||
Line 1,465: | Line 1,465: | ||
end; |
end; |
||
</lang> |
</lang> |
||
=={{header|Prolg}}== |
|||
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 :- |
|||
L ="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.", |
|||
% read the lines and the words |
|||
% compute the length of the longuest word. |
|||
% LP is the list of lines, |
|||
% each line is a list of words |
|||
parse(L, 0, N, LP, []), |
|||
% Because of the particular treatment of the strings by Prolog |
|||
% we need to add 3 and not 1 |
|||
N1 is N+3, |
|||
% words will be left aligned |
|||
sformat(AL, '%~wl', [N1]), |
|||
% words will be centered |
|||
sformat(AC, '%~wc', [N1]), |
|||
% words will be right aligned |
|||
sformat(AR, '%~wr', [N1]), |
|||
maplist(affiche(AL), LP), nl, |
|||
maplist(affiche(AC), LP), nl, |
|||
maplist(affiche(AR), LP), nl. |
|||
affiche(F, L) :- |
|||
maplist(my_writef(F), L), |
|||
nl. |
|||
my_writef(_F, [13]) :- |
|||
nl. |
|||
my_writef(F, W) :- |
|||
writef(F, [W]). |
|||
parse([], Max, Max) --> []. |
|||
parse(T, N, Max) --> |
|||
{ parse_line(T, 0, N1, T1, L, []), |
|||
( N1 > N -> N2 = N1; N2 = N)}, |
|||
[L], |
|||
parse(T1, N2, Max). |
|||
parse_line([], NF, NF, []) --> []. |
|||
parse_line([H|TF], NF, NF, TF) --> |
|||
{code_type(H, end_of_line), !}, |
|||
[]. |
|||
parse_line(T, N, NF, TF) --> |
|||
{ parse_word(T, 0, N1, T1, W, []), |
|||
( N1 > N -> N2 = N1; N2 = N)}, |
|||
[W], |
|||
parse_line(T1, N2, NF, TF). |
|||
% 36 is the code of '$' |
|||
parse_word([36|T], N, N, T) --> |
|||
{!}, |
|||
[]. |
|||
parse_word([H|T], N, N, [H|T]) --> |
|||
{code_type(H, end_of_line), !}, |
|||
[]. |
|||
parse_word([], N, N, []) --> []. |
|||
parse_word([H|T], N1, NF, TF) --> |
|||
[H], |
|||
{N2 is N1 + 1}, |
|||
parse_word(T, N2, NF, TF). |
|||
</lang> |
|||
Output : |
|||
<lang Prolog> ?- aligner. |
|||
'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.' |
|||
'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.' |
|||
'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> |
|||