How to resolve the algorithm Comma quibbling step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Comma quibbling step by step in the Prolog programming language
Table of Contents
Problem Statement
Comma quibbling is a task originally set by Eric Lippert in his blog.
Write a function to generate a string output which is the concatenation of input words from a list/sequence where:
Test your function with the following series of inputs showing your output here on this page:
Note: Assume words are non-empty strings of uppercase characters for this task.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Comma quibbling step by step in the Prolog programming language
Source code in the prolog programming language
words_series(Words, Bracketed) :-
words_serialized(Words, Serialized),
atomics_to_string(["{",Serialized,"}"], Bracketed).
words_serialized([], "").
words_serialized([Word], Word) :- !.
words_serialized(Words, Serialized) :-
append(Rest, [Last], Words), %% Splits the list of *Words* into the *Last* word and the *Rest*
atomics_to_string(Rest, ", ", WithCommas),
atomics_to_string([WithCommas, " and ", Last], Serialized).
test :-
forall( member(Words, [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]]),
( words_series(Words, Series),
format('~w ~15|=> ~w~n', [Words, Series]))
).
?- test.
[] => {}
[ABC] => {ABC}
[ABC,DEF] => {ABC and DEF}
[ABC,DEF,G,H] => {ABC, DEF, G and H}
true.
You may also check:How to resolve the algorithm Chinese zodiac step by step in the 11l programming language
You may also check:How to resolve the algorithm Polyspiral step by step in the C++ programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Maxima programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Tailspin programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the C++ programming language