From: Ken Bloom Date: 2007-04-23T05:10:08+09:00 Subject: Re: [QUIZ] Morse Code (#121) Ruby Quiz wrote: > The three rules of Ruby Quiz: > > 1. Please do not post any solutions or spoiler discussion for this quiz until > 48 hours have passed from the time on this message. > > 2. Support Ruby Quiz by submitting ideas as often as you can: > > http://www.rubyquiz.com/ > > 3. Enjoy! Technically, I'm not breaking any rules in suggesting that the most natural language for this problem is Prolog: m('.-', a). m('-...', b). m('-.-.', c). m('-..', d). m('.', e). m('..-.', f). m('--.', g). m('....', h). m('..', i). m('.---', j). m('-.-', k). m('.-..', l). m('--', m). m('-.', n). m('---', o). m('.--.', p). m('--.-', q). m('.-.', r). m('...', s). m('-', t). m('..-', u). m('...-', v). m('.--', w). m('-..-', x). m('-.--', y). m('--..', z). morse_decode(Text,Translation):-atom_chars(Text,List), morse_i(List,TranslationList), atom_chars(Translation,TranslationList). morse_encode(Text,Translation):- atom_chars(Translation,TranslationList), morse_i(List,TranslationList), atom_chars(Text,List). morse_i([],[]). morse_i(List,Translation):-m(First,Letter),atom_chars(First,FirstList), append(FirstList,RestList,List),Translation=[Letter|RestTrans], morse_i(RestList,RestTrans). Examples of use: morse_decode('...---...',X). morse_decode('...---...',sos). morse_encode(X,'sos'). morse_encode('...---...',sos). --Ken Bloom