How to resolve the algorithm Abbreviations, easy step by step in the Prolog programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abbreviations, easy step by step in the Prolog programming language

Table of Contents

Problem Statement

This task is an easier (to code) variant of the Rosetta Code task:   Abbreviations, simple.

For this task, the following   command table   will be used:

Notes concerning the above   command table:

For a user string of: the computer program should return the string:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abbreviations, easy step by step in the Prolog programming language

Source code in the prolog programming language

:- initialization(main).

command_table("Add ALTer  BAckup Bottom  CAppend Change SCHANGE  CInsert CLAst COMPress COpy
COUnt COVerlay CURsor DELete CDelete Down DUPlicate Xedit EXPand EXTract Find
NFind NFINDUp NFUp CFind FINdup FUp FOrward GET Help HEXType Input POWerinput
Join SPlit SPLTJOIN  LOAD  Locate CLocate  LOWercase UPPercase  LPrefix MACRO
MErge MODify MOve MSG Next Overlay PARSE PREServe PURge PUT PUTD  Query  QUIT
READ  RECover REFRESH RENum REPeat  Replace CReplace  RESet  RESTore  RGTLEFT
RIght LEft  SAVE  SET SHift SI  SORT  SOS  STAck STATus  TOP TRAnsfer Type Up").

validate("", _, "") :- !.

validate(Word, Commands, Result) :-
    member(Command, Commands),
    string_concat(Prefix, Suffix, Command),
    string_upper(Prefix, Prefix),
    string_lower(Suffix, Suffix),
    string_lower(Word, LowWord),
    string_lower(Prefix, LowPrefix),
    string_concat(LowPrefix, _, LowWord),
    string_lower(Command, LowCommand),
    string_concat(LowWord, _, LowCommand),
    string_upper(Command, Result),
    !.

validate(_, _, "*error*").

main :-
    current_prolog_flag(argv, Args),
    command_table(Table),
    split_string(Table, " \t\n", "", Commands),
    forall(member(Arg, Args), (
        atom_string(Arg, Word),
        validate(Word, Commands, Result),
        format("~w ", Result)
    )),
    nl.


  

You may also check:How to resolve the algorithm Euler's constant 0.5772... step by step in the Scheme programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the Ruby programming language
You may also check:How to resolve the algorithm Word search step by step in the Java programming language
You may also check:How to resolve the algorithm Tic-tac-toe step by step in the ERRE programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the bc programming language