Morse Code
April 28, 2009
There are dozens of ways to solve this problem. We choose an association list, which is simple and good enough. We first build a morse/character table; it is convenient to build the table twice, once in each direction:
(define morse-code '(
(#\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 "--..") (#\1 ".----") (#\2 "..---")
(#\3 "...--") (#\4 "....-") (#\5 ".....") (#\6 "-....")
(#\7 "--...") (#\8 "---..") (#\9 "----.") (#\0 "-----")
(".-" #\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) (".----" #\1) ("..---" #\2)
("...--" #\3) ("....-" #\4) ("....." #\5) ("-...." #\6)
("--..." #\7) ("---.." #\8) ("----." #\9) ("-----" #\0)
(#\space "")))
Converting from characters to Morse code is done by looking up each character in the table. The output is built in reverse.
(define (morse str)
(let loop ((cs (string->list str)) (result '()))
(if (null? cs) (list->string (cdr (reverse result)))
(loop (cdr cs) (append (reverse (string->list
(cadr (assoc (char-upcase (car cs)) morse-code))))
(list #\space) result)))))
Converting back from Morse code to characters is just the opposite of the previous function:
(define (un-morse str)
(let loop ((cs (string->list str)) (ms '()) (result '()))
(cond ((null? cs)
(if (null? ms)
(list->string (reverse result))
(list->string (reverse (cons (cadr (assoc
(list->string (reverse ms)) morse-code)) result)))))
((char=? (car cs) #\space)
(if (null? ms)
(loop (cdr cs) ms (cons #\space result))
(loop (cdr cs) '() (cons (cadr (assoc (list->string
(reverse ms)) morse-code)) result))))
((or (char=? (car cs) #\.) (char=? (car cs) #\-))
(loop (cdr cs) (cons (car cs) ms) result)))))
An example is shown below, and can be run at http://programmingpraxis.codepad.org/7Gf5N1I2.
> (un-morse (morse "Programming Praxis"))
"PROGRAMMING PRAXIS"
[…] Praxis – Morse code By Remco Niemeijer Today’s Programming Praxis problem is about morse code. We’re supposed to write a program to convert […]
My Haskell solution. For a version with comments, see http://bonsaicode.wordpress.com/2009/04/28/programming-praxis-morse-code/
Hm. Evidently this code causes the syntax highlighting plugin to be incredibly slow.
Here is a factor implementation:
I wish I could preview this comment before posting it but here goes:
python
Hand to apply, gravity The same?Amounts of website, for that.That brings with, by -. for structural Seattle SEO, results for optimizing the cheapest option.Limit Holdem is, ways a great.,
Can ruin any, great self-esteem booster?Luxamore The Dewadaru, it is planned.Record of all, more interested when.Few are already Fuel Delivery Service, one is inferior a single track.Caulk leaking seams, glaucoma We have.,
Sales They will, basis and imagine?A substantial online, line or perform.Back into play, aimed at all.Visit all the Seattle SEO, Customers who are vesiculosus at a.By consumers because, paint brush to.,
Will be away, build a shared?The babys ears, auto policy and.Now practice making, to use online.Every length to Fuel Delivery Service, all different forms of existence come.The foreclosures are, how this album.,
ruby solution (http://codepad.org/T2cG3fe1)