How to resolve the algorithm Morse code step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Morse code step by step in the Prolog programming language
Table of Contents
Problem Statement
Morse code is one of the simplest and most versatile methods of telecommunication in existence. It has been in use for more than 175 years — longer than any other electronic encoding system.
Send a string as audible Morse code to an audio device (e.g., the PC speaker).
As the standard Morse code does not contain all possible characters, you may either ignore unknown characters in the file, or indicate them somehow (e.g. with a different pitch).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Morse code step by step in the Prolog programming language
Source code in the prolog programming language
% convert text to morse
% query text2morse(Text, Morse)
% where
% Text is string to convert
% Morse is Morse representation
% There is a space between chars and double space between words
%
text2morse(Text, Morse) :-
string_lower(Text, TextLower), % rules are in lower case
string_chars(TextLower, Chars), % convert string into list of chars
chars2morse(Chars, MorseChars), % convert each char into morse
string_chars(MorsePlusSpace, MorseChars), % append returned string list into single string
string_concat(Morse, ' ', MorsePlusSpace). % Remove trailing space
chars2morse([], "").
chars2morse([H|CharTail], Morse) :-
morse(H, M),
chars2morse(CharTail, MorseTail),
string_concat(M,' ', MorseSpace),
string_concat(MorseSpace, MorseTail, Morse).
% space
morse(' ', " ").
% letters
morse('a', ".-").
morse('b', "-...").
morse('c', "-.-.").
morse('d', "-..").
morse('e', ".").
morse('f', "..-.").
morse('g', "--.").
morse('h', "....").
morse('i', "..").
morse('j', ".---").
morse('k', "-.-").
morse('l', ".-..").
morse('m', "--").
morse('n', "-.").
morse('o', "---").
morse('p', ".--.").
morse('q', "--.-").
morse('r', ".-.").
morse('s', "...").
morse('t', "-").
morse('u', "..-").
morse('v', "...-").
morse('w', ".--").
morse('x', "-..-").
morse('y', "-.--").
morse('z', "--..").
% numbers
morse('1', ".----").
morse('2', "..---").
morse('3', "...--").
morse('4', "....-").
morse('5', ".....").
morse('6', "-....").
morse('7', "--...").
morse('8', "---..").
morse('9', "----.").
morse('0', "-----").
% common punctuation
morse('.', ".-.-.-").
morse(',', "--..--").
morse('/', "-..-.").
morse('?', "..--..").
morse('=', "-...-").
morse('+', ".-.-.").
morse('-', "-....-").
morse('@', ".--.-.").
text2morse("Hello World", Morse).
You may also check:How to resolve the algorithm Camel case and snake case step by step in the C++ programming language
You may also check:How to resolve the algorithm Additive primes step by step in the Ksh programming language
You may also check:How to resolve the algorithm Longest common subsequence step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the DBL programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the C# programming language