Morse code: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 212:
Morsebeep (Convert ("Science sans Conscience"));
end Morse_Tx;</lang>
 
=={{header|Arturo}}==
 
<lang arturo>// set the morse code
 
letters: #{
a: ".-", b: "-...", c: "-.-.", d: "-..", e: "."
f: "..-.", g: "--.", h: "....", i: "..", j: ".---"
k: "-.-", l: ".-..", m: "--", n: "-.", o: "---"
p: ".--.", q: "--.-", r: ".-.", s: "...", t: "-"
u: "..-", v: "...-", w: ".--", x: "-..-", y: "-.--"
z: "--.."
}
numbers: #("-----" ".----" "..---" "...--" "....-" "....." "-...." "--..." "---.." "----.")
 
// print an encoded message
 
str: "hello world 2019"
 
loop [chars str] {
if [not|isWhitespace &] {
if [isNumber &] {
prints numbers.[toNumber &]
} {
prints letters.[&]
}
}
}
 
print ""</lang>
 
{{out}}
 
<pre>......-...-..---.-----.-..-..-....--------.--------.</pre>
 
=={{header|AutoHotkey}}==
Line 357 ⟶ 391:
} ; ---End Function Morse---
</lang>
 
=={{header|Arturo}}==
 
<lang arturo>// set the morse code
 
letters: #{
a: ".-", b: "-...", c: "-.-.", d: "-..", e: "."
f: "..-.", g: "--.", h: "....", i: "..", j: ".---"
k: "-.-", l: ".-..", m: "--", n: "-.", o: "---"
p: ".--.", q: "--.-", r: ".-.", s: "...", t: "-"
u: "..-", v: "...-", w: ".--", x: "-..-", y: "-.--"
z: "--.."
}
numbers: #("-----" ".----" "..---" "...--" "....-" "....." "-...." "--..." "---.." "----.")
 
// print an encoded message
 
str: "hello world 2019"
 
loop [chars str] {
if [not|isWhitespace &] {
if [isNumber &] {
prints numbers.[toNumber &]
} {
prints letters.[&]
}
}
}
 
print ""</lang>
 
{{out}}
 
<pre>......-...-..---.-----.-..-..-....--------.--------.</pre>
 
 
=={{header|AWK}}==
Line 734 ⟶ 733:
}
</lang>
 
=={{header|C sharp|C#}}==
<lang CSharp>using System;
using System.Collections.Generic;
 
namespace Morse
{
class Morse
{
static void Main(string[] args)
{
string word = "sos";
Dictionary<string, string> Codes = new Dictionary<string, string>
{
{"a", ".- "}, {"b", "-... "}, {"c", "-.-. "}, {"d", "-.. "},
{"e", ". "}, {"f", "..-. "}, {"g", "--. "}, {"h", ".... "},
{"i", ".. "}, {"j", ".--- "}, {"k", "-.- "}, {"l", ".-.. "},
{"m", "-- "}, {"n", "-. "}, {"o", "--- "}, {"p", ".--. "},
{"q", "--.- "}, {"r", ".-. "}, {"s", "... "}, {"t", "- "},
{"u", "..- "}, {"v", "...- "}, {"w", ".-- "}, {"x", "-..- "},
{"y", "-.-- "}, {"z", "--.. "}, {"0", "-----"}, {"1", ".----"},
{"2", "..---"}, {"3", "...--"}, {"4", "....-"}, {"5", "....."},
{"6", "-...."}, {"7", "--..."}, {"8", "---.."}, {"9", "----."}
};
 
foreach (char c in word.ToCharArray())
{
string rslt = Codes[c.ToString()].Trim();
foreach (char c2 in rslt.ToCharArray())
{
if (c2 == '.')
Console.Beep(1000, 250);
else
Console.Beep(1000, 750);
}
System.Threading.Thread.Sleep(50);
}
}
}
}
</lang>
 
=={{header|C++}}==
<lang C++>
Line 805 ⟶ 846:
}
 
</lang>
=={{header|C sharp|C#}}==
<lang CSharp>using System;
using System.Collections.Generic;
 
namespace Morse
{
class Morse
{
static void Main(string[] args)
{
string word = "sos";
Dictionary<string, string> Codes = new Dictionary<string, string>
{
{"a", ".- "}, {"b", "-... "}, {"c", "-.-. "}, {"d", "-.. "},
{"e", ". "}, {"f", "..-. "}, {"g", "--. "}, {"h", ".... "},
{"i", ".. "}, {"j", ".--- "}, {"k", "-.- "}, {"l", ".-.. "},
{"m", "-- "}, {"n", "-. "}, {"o", "--- "}, {"p", ".--. "},
{"q", "--.- "}, {"r", ".-. "}, {"s", "... "}, {"t", "- "},
{"u", "..- "}, {"v", "...- "}, {"w", ".-- "}, {"x", "-..- "},
{"y", "-.-- "}, {"z", "--.. "}, {"0", "-----"}, {"1", ".----"},
{"2", "..---"}, {"3", "...--"}, {"4", "....-"}, {"5", "....."},
{"6", "-...."}, {"7", "--..."}, {"8", "---.."}, {"9", "----."}
};
 
foreach (char c in word.ToCharArray())
{
string rslt = Codes[c.ToString()].Trim();
foreach (char c2 in rslt.ToCharArray())
{
if (c2 == '.')
Console.Beep(1000, 250);
else
Console.Beep(1000, 750);
}
System.Threading.Thread.Sleep(50);
}
}
}
}
</lang>
 
Line 1,209 ⟶ 1,210:
.... . .-.. .-.. --- --..-- .-- --- .-. .-.. -.. ---.
</pre>
 
=={{header|F#}}==
<lang fsharp>
open System
open System.Threading
 
let morse = Map.ofList
[('a', "._ "); ('b', "_... "); ('c', "_._. "); ('d', "_.. ");
('e', ". "); ('f', ".._. "); ('g', "__. "); ('h', ".... ");
('i', ".. "); ('j', ".___ "); ('k', "_._ "); ('l', "._.. ");
('m', "__ "); ('n', "_. "); ('o', "___ "); ('p', ".__. ");
('q', "__._ "); ('r', "._. "); ('s', "... "); ('t', "_ ");
('u', ".._ "); ('v', "..._ "); ('w', ".__ "); ('x', "_.._ ");
('y', "_.__ "); ('z', "__.. "); ('0', "_____ "); ('1', ".____ ");
('2', "..___ "); ('3', "...__ "); ('4', "...._ "); ('5', "..... ");
('6', "_.... "); ('7', "__... "); ('8', "___.. "); ('9', "____. ")]
 
let beep c =
match c with
| '.' ->
printf "."
Console.Beep(1200, 250)
| '_' ->
printf "_"
Console.Beep(1200, 1000)
| _ ->
printf " "
Thread.Sleep(125)
 
let trim (s: string) = s.Trim()
let toMorse c = Map.find c morse
let lower (s: string) = s.ToLower()
let sanitize = String.filter Char.IsLetterOrDigit
 
let send = sanitize >> lower >> String.collect toMorse >> trim >> String.iter beep
 
send "Rosetta Code"
</lang>
{{out}}
<pre>._. ___ ... . _ _ ._ _._. ___ _.. .</pre>
 
=={{header|Factor}}==
Line 1,388 ⟶ 1,429:
End
</lang>
 
=={{header|F#}}==
<lang fsharp>
open System
open System.Threading
 
let morse = Map.ofList
[('a', "._ "); ('b', "_... "); ('c', "_._. "); ('d', "_.. ");
('e', ". "); ('f', ".._. "); ('g', "__. "); ('h', ".... ");
('i', ".. "); ('j', ".___ "); ('k', "_._ "); ('l', "._.. ");
('m', "__ "); ('n', "_. "); ('o', "___ "); ('p', ".__. ");
('q', "__._ "); ('r', "._. "); ('s', "... "); ('t', "_ ");
('u', ".._ "); ('v', "..._ "); ('w', ".__ "); ('x', "_.._ ");
('y', "_.__ "); ('z', "__.. "); ('0', "_____ "); ('1', ".____ ");
('2', "..___ "); ('3', "...__ "); ('4', "...._ "); ('5', "..... ");
('6', "_.... "); ('7', "__... "); ('8', "___.. "); ('9', "____. ")]
 
let beep c =
match c with
| '.' ->
printf "."
Console.Beep(1200, 250)
| '_' ->
printf "_"
Console.Beep(1200, 1000)
| _ ->
printf " "
Thread.Sleep(125)
 
let trim (s: string) = s.Trim()
let toMorse c = Map.find c morse
let lower (s: string) = s.ToLower()
let sanitize = String.filter Char.IsLetterOrDigit
 
let send = sanitize >> lower >> String.collect toMorse >> trim >> String.iter beep
 
send "Rosetta Code"
</lang>
{{out}}
<pre>._. ___ ... . _ _ ._ _._. ___ _.. .</pre>
 
=={{header|Gambas}}==
Line 1,939 ⟶ 1,940:
 
[http://jsbin.com/orubaq/1/edit Live Version]
 
 
=={{header|Julia}}==
Line 2,201:
<lang lua>morse = require 'morse'
morse.beep "I am the very model of a modern major-general."</lang>
 
=={{header|Mathematica}}==
A Morse "codec" based on replacement rule programming.
Replacement rules also translate the text Morse code into audible Morse code.
The dots and dashes become clarinet middle-C notes of different lengths.
Unknown characters encode into "?" in Morse text and clarinet F# in Morse audio.
The function, sonicMorse[s_String], plays the Morse code audio translation of a string.
 
<lang Mathematica>Dictionary = Join[CharacterRange["a", "z"], CharacterRange["0", "9"]];
mark = 0.1; gap = 0.125; (* gap should be equal to mark. But longer gap makes audio code easier to decode *)
shortgap = 3*gap; medgap = 7*gap;
longmark = 3*mark;
MorseDictionary = {
".-", "-...", "-.-.", "-..",
".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--",
"-..-", "-.--", "--..",
"-----", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----."
};
 
MorseDictionary = # <> " " & /@ MorseDictionary; (* Force short gap silence after each letter/digit *)
 
Tones = {
SoundNote[None, medgap],
SoundNote[None, shortgap],
{SoundNote["C", mark, "Clarinet"], SoundNote[None, gap]},
{SoundNote["C", longmark, "Clarinet"], SoundNote[None, gap]},
{SoundNote["F#", mark, "Clarinet"], SoundNote[None, gap]} (* Use F# short mark to denote unrecognized character *)
};
 
codeRules = MapThread[Rule, {Dictionary, MorseDictionary}];
decodeRules = MapThread[Rule, {MorseDictionary, Dictionary}];
soundRules = MapThread[Rule, {{" ", " ", ".", "-", "?"}, Tones}];
(* The order of the rules here is important. Otherwise medium gaps and short gaps get confounded *)
 
morseCode[s_String] := StringReplace[ToLowerCase@s, codeRules~Join~{x_ /; FreeQ[Flatten@{Dictionary, " "}, x] -> "? "}]
morseDecode[s_String] := StringReplace[s, decodeRules]
sonicMorse[s_String] := EmitSound@Sound@Flatten[Characters@morseCode@s /. soundRules]</lang>
 
{{out|Text example}}
<pre>morseCode["SOS soS"]
morseDecode[%]
morseCode["s@os|"]</pre>
{{out}}
<pre>"... --- ... ... --- ... "
 
"sos sos"
 
"... ? --- ... ? "</pre>
 
=={{header|MATLAB}}==
Line 2,283 ⟶ 2,335:
 
-.-.|.-|.-..|.-..| |--|.| |..|...|....|--|.-|.|.-..|</lang>
 
 
=={{header|Mathematica}}==
A Morse "codec" based on replacement rule programming.
Replacement rules also translate the text Morse code into audible Morse code.
The dots and dashes become clarinet middle-C notes of different lengths.
Unknown characters encode into "?" in Morse text and clarinet F# in Morse audio.
The function, sonicMorse[s_String], plays the Morse code audio translation of a string.
 
<lang Mathematica>Dictionary = Join[CharacterRange["a", "z"], CharacterRange["0", "9"]];
mark = 0.1; gap = 0.125; (* gap should be equal to mark. But longer gap makes audio code easier to decode *)
shortgap = 3*gap; medgap = 7*gap;
longmark = 3*mark;
MorseDictionary = {
".-", "-...", "-.-.", "-..",
".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--",
"-..-", "-.--", "--..",
"-----", ".----", "..---", "...--", "....-", ".....",
"-....", "--...", "---..", "----."
};
 
MorseDictionary = # <> " " & /@ MorseDictionary; (* Force short gap silence after each letter/digit *)
 
Tones = {
SoundNote[None, medgap],
SoundNote[None, shortgap],
{SoundNote["C", mark, "Clarinet"], SoundNote[None, gap]},
{SoundNote["C", longmark, "Clarinet"], SoundNote[None, gap]},
{SoundNote["F#", mark, "Clarinet"], SoundNote[None, gap]} (* Use F# short mark to denote unrecognized character *)
};
 
codeRules = MapThread[Rule, {Dictionary, MorseDictionary}];
decodeRules = MapThread[Rule, {MorseDictionary, Dictionary}];
soundRules = MapThread[Rule, {{" ", " ", ".", "-", "?"}, Tones}];
(* The order of the rules here is important. Otherwise medium gaps and short gaps get confounded *)
 
morseCode[s_String] := StringReplace[ToLowerCase@s, codeRules~Join~{x_ /; FreeQ[Flatten@{Dictionary, " "}, x] -> "? "}]
morseDecode[s_String] := StringReplace[s, decodeRules]
sonicMorse[s_String] := EmitSound@Sound@Flatten[Characters@morseCode@s /. soundRules]</lang>
 
{{out|Text example}}
<pre>morseCode["SOS soS"]
morseDecode[%]
morseCode["s@os|"]</pre>
{{out}}
<pre>"... --- ... ... --- ... "
 
"sos sos"
 
"... ? --- ... ? "</pre>
 
=={{header|Modula-2}}==
Line 2,644 ⟶ 2,643:
2) pcspkr may be available but muted.
- Check your sound prefrences,usually a right click over the speaker icon</pre>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015-09-24}}
Here we use the user as the audio device.
Just read the output, leaving extra pauses where indicated
by either whitespace or underscore.
<lang perl6>my %m = ' ', '_ _ ',
|<
! ---.
" .-..-.
$ ...-..-
' .----.
( -.--.
) -.--.-
+ .-.-.
, --..--
- -....-
. .-.-.-
/ -..-.
: ---...
; -.-.-.
= -...-
? ..--..
@ .--.-.
[ -.--.
] -.--.-
_ ..--.-
0 -----
1 .----
2 ..---
3 ...--
4 ....-
5 .....
6 -....
7 --...
8 ---..
9 ----.
A .-
B -...
C -.-.
D -..
E .
F ..-.
G --.
H ....
I ..
J .---
K -.-
L .-..
M --
N -.
O ---
P .--.
Q --.-
R .-.
S ...
T -
U ..-
V ...-
W .--
X -..-
Y -.--
Z --..
>.map: -> $c, $m is copy {
$m.=subst(rx/'-'/, 'BGAAACK!!! ', :g);
$m.=subst(rx/'.'/, 'buck ', :g);
$c => $m ~ '_';
}
 
say prompt("Gimme a string: ").uc.comb.map: { %m{$_} // "<scratch> " }</lang>
Sample run:
<p>Gimme a string: <b>Howdy, World!</b>
<br>buck buck buck buck _ BGAAACK!!! BGAAACK!!! BGAAACK!!! _ buck BGAAACK!!! BGAAACK!!! _ BGAAACK!!! buck buck _ BGAAACK!!! buck BGAAACK!!! BGAAACK!!! _ BGAAACK!!! BGAAACK!!! buck buck BGAAACK!!! BGAAACK!!! _ _ _ buck BGAAACK!!! BGAAACK!!! _ BGAAACK!!! BGAAACK!!! BGAAACK!!! _ buck BGAAACK!!! buck _ buck BGAAACK!!! buck buck _ BGAAACK!!! buck buck _ BGAAACK!!! BGAAACK!!! BGAAACK!!! buck _
 
=={{header|Phix}}==
Line 2,917 ⟶ 2,843:
 
END MORSE;</lang>
 
 
=={{header|PowerShell}}==
Line 3,240 ⟶ 3,165:
(morse "Say something here")
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015-09-24}}
Here we use the user as the audio device.
Just read the output, leaving extra pauses where indicated
by either whitespace or underscore.
<lang perl6>my %m = ' ', '_ _ ',
|<
! ---.
" .-..-.
$ ...-..-
' .----.
( -.--.
) -.--.-
+ .-.-.
, --..--
- -....-
. .-.-.-
/ -..-.
: ---...
; -.-.-.
= -...-
? ..--..
@ .--.-.
[ -.--.
] -.--.-
_ ..--.-
0 -----
1 .----
2 ..---
3 ...--
4 ....-
5 .....
6 -....
7 --...
8 ---..
9 ----.
A .-
B -...
C -.-.
D -..
E .
F ..-.
G --.
H ....
I ..
J .---
K -.-
L .-..
M --
N -.
O ---
P .--.
Q --.-
R .-.
S ...
T -
U ..-
V ...-
W .--
X -..-
Y -.--
Z --..
>.map: -> $c, $m is copy {
$m.=subst(rx/'-'/, 'BGAAACK!!! ', :g);
$m.=subst(rx/'.'/, 'buck ', :g);
$c => $m ~ '_';
}
 
say prompt("Gimme a string: ").uc.comb.map: { %m{$_} // "<scratch> " }</lang>
Sample run:
<p>Gimme a string: <b>Howdy, World!</b>
<br>buck buck buck buck _ BGAAACK!!! BGAAACK!!! BGAAACK!!! _ buck BGAAACK!!! BGAAACK!!! _ BGAAACK!!! buck buck _ BGAAACK!!! buck BGAAACK!!! BGAAACK!!! _ BGAAACK!!! BGAAACK!!! buck buck BGAAACK!!! BGAAACK!!! _ _ _ buck BGAAACK!!! BGAAACK!!! _ BGAAACK!!! BGAAACK!!! BGAAACK!!! _ buck BGAAACK!!! buck _ buck BGAAACK!!! buck buck _ BGAAACK!!! buck buck _ BGAAACK!!! BGAAACK!!! BGAAACK!!! buck _
 
=={{header|Red}}==
Line 3,692 ⟶ 3,691:
 
}</lang>
 
=={{header|sed}}==
Translation of AWK:
Line 4,050:
H.... e. l.-.. l.-.. o--- ,--..-- w.-- o--- r.-. l.-.. d-.. !.-.-..
</pre>
 
=={{header|Yabasic}}==
Mixin classic/modern style. In Yabasic, line number is not mandatory.
10,327

edits