code ← ##.morse text ⍝ Conversion to/from Morse code.
text ← ##.morse code ⍝ Samuel Morse 1791-1872.
Given a text vector of characters from: "A-Z", "0-9", ".,:?'-/()"@= ", [morse]
returns a nested vector of the equivalent Morse codes.
Conversely, given a nested vector of Morse codes, the function returns the equi-
valent plain text.
Unrecognised characters and codes are ignored. In particular, [morse] ignores
lower case letters "a-z".
The Morse tree
--------------
Morse chooses shorter codes for more frequently occurring letters (in English).
The designers of old-style linotype type-setting machines judged the order of
this frequency to be: ETAOIN SHRDLU CMFWYP VBGKQJ XZ.
┌───5 .....
┌───H ....
│ └───4 ....-
┌───S ...
│ │ ┌───Ŝ ...-.
│ └───V ...-
│ └───3 ...--
┌───I ..
│ │ ┌───É ..-..
│ │ ┌───F ..-.
│ └───U ..-
│ │ ┌───? ..--..
│ │ ┌───Ð ..--.
│ │ │ └───_ ..--.-
│ └───Ü ..--
│ └───2 ..---
┌───E .
│ │ ┌───L .-..
│ │ │ │ ┌───" .-..-.
│ │ │ └───È .-..-
│ │ ┌───R .-.
│ │ │ │ ┌───+ .-.-.
│ │ │ │ │ └───. .-.-.-
│ │ │ └───Ä .-.-
│ └───A .-
│ │ ┌───þ .--..
│ │ ┌───P .--.
│ │ │ │ ┌───@ .--.-.
│ │ │ └───À .--.-
│ └───W .--
│ │ ┌───Ĵ .---.
│ └───J .---
│ │ ┌───' .----.
│ └───1 .----
│
│ ┌───6 -....
│ │ └───- -....-
│ ┌───B -...
│ │ └───= -...-
│ ┌───D -..
│ │ │ ┌───/ -..-.
│ │ └───X -..-
│ ┌───N -.
│ │ │ ┌───Ç -.-..
│ │ │ ┌───C -.-.
│ │ │ │ │ ┌───; -.-.-.
│ │ │ │ └───┤
│ │ │ │ └───! -.-.--
│ │ └───K -.-
│ │ │ ┌───( -.--.
│ │ │ │ └───) -.--.-
│ │ └───Y -.--
└───T -
│ ┌───7 --...
│ ┌───Z --..
│ │ └───┐
│ │ └───, --..--
│ ┌───G --.
│ │ │ ┌───Ĝ --.-.
│ │ └───Q --.-
│ │ └───Ñ --.--
└───M --
│ ┌───: ---...
│ ┌───8 ---..
│ ┌───Ö ---.
└───O ---
│ ┌───9 ----.
└───CH ----
└───0 -----
Ref: http://en.wikipedia.org/wiki/Morse_code
(muse:
It is said that experienced telegraph operators, who used morse code to
transmit messages, could recognise each others tapping style; the equivalent
of being able to distinguish someone's handwriting (or coding style).
)
Adám Brudzewsky suggests this shorter coding for the upper case alphabet:
morse←{⎕io←0
In←(⊃∘⍸⍷⍨∘⊂⍨)¨∘⊂
a←'ET' 'IANM' 'SURWDKGO' 'HVF L PJBXCYZQ'
t←↑{↓'.-'[⍉2⊥⍣¯1⍳≢⍵]}¨a
1=≡⍵:t[⍵ In↑a]
(⍵ In t)⊃¨⊂a,¨' '
}
morse 'SAMUEL'
┌───┬──┬──┬───┬─┬────┐
│...│.-│--│..-│.│.-..│
└───┴──┴──┴───┴─┴────┘
Examples:
morse 'HELLO WORLD' ⍝ Morse code from plain text.
┌────┬─┬────┬────┬───┬─┬───┬───┬───┬────┬───┐
│....│.│.-..│.-..│---│/│.--│---│.-.│.-..│-..│
└────┴─┴────┴────┴───┴─┴───┴───┴───┴────┴───┘
morse morse'ROUND TRIP' ⍝ ... and back again.
ROUND TRIP
morse 'Samuel Finley Breese' ⍝ lower case letters ignored,
┌───┬─┬────┬─┬────┐
│...│/│..-.│/│-...│
└───┴─┴────┴─┴────┘
morse ucase 'Samuel Finley Breese' ⍝ ... so convert to upper case.
┌───┬──┬──┬───┬─┬────┬─┬────┬──┬──┬────┬─┬────┬─┬────┬───┬─┬─┬───┬─┐
│...│.-│--│..-│.│.-..│/│..-.│..│-.│.-..│.│-.--│/│-...│.-.│.│.│...│.│
└───┴──┴──┴───┴─┴────┴─┴────┴──┴──┴────┴─┴────┴─┴────┴───┴─┴─┴───┴─┘
See also: ucase packH
Back to: contents
Back to: Workspaces