Vigenère cipher: Difference between revisions

m (→‎{{header|Phix}}: syntax coloured)
Line 914:
 
=={{header|Common Lisp}}==
====Main solution====
This doesn't assume anything about character codes other than A-Z being a contiguous block (but still, we could be using EBCDIC. Who knows.)
<lang lisp>(defun strip (s)
Line 945 ⟶ 946:
enc: WMCEEIKLGRPIFVMEUGXXYILILZXYVBZLRGCEYAIOEKXIZGU
dec: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH</pre>
====Alternative====
No string to circular list conversion.
 
1. Program
 
<lang lisp>(defconstant +a+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
 
(defun strip (s)
(string-upcase (remove-if-not 'alpha-char-p s)))
 
(defun vigenere (text key &key decode &aux (d (if decode -1 1)))
(let ((text (strip text)) (key (strip key)) (i -1))
(map 'string
(lambda (x)
(setf i (mod (1+ i) (length key)))
(char +a+ (mod (+ (position x +a+) (* d (position (elt key i) +a+))) 26)))
text)))</lang>
 
2. Execution
 
{{out}}
<pre>(vigenere "« Through the Looking-Glass »" "Lewis Carroll")
"ELNWMIHKYSWZZOEVYILRJG"
(vigenere "ELNWMIHKYSWZZOEVYILRJG" "Lewis Carroll" :decode t)
"THROUGHTHELOOKINGGLASS"</pre>
 
=={{header|D}}==
422

edits