March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -0,0 +1,12 @@
class Caeser(val key: Int) {
@annotation.tailrec
private def rotate(p: Int, s: IndexedSeq[Char]): IndexedSeq[Char] = if (p < 0) rotate(s.length + p, s) else s.drop(p) ++ s.take(p)
val uc = 'A' to 'Z'
val lc = 'a' to 'z'
val as = uc ++ lc
val bs = rotate(key, uc) ++ rotate(key, lc)
def encode(c: Char) = if (as.contains(c)) bs(as.indexOf(c)) else c
def decode(c: Char) = if (bs.contains(c)) as(bs.indexOf(c)) else c
}

View file

@ -0,0 +1,6 @@
val text = "The five boxing wizards jump quickly"
val myCaeser = new Caeser(3)
val encoded = text.map(c => myCaeser.encode(c))
println("Plaintext => " + text)
println("Ciphertext => " + encoded)
println("Decrypted => " + encoded.map(c => myCaeser.decode(c)))

View file

@ -0,0 +1,27 @@
;
; Works with R7RS-compatible Schemes (e.g. Chibi).
; Also current versions of Chicken, Gauche and Kawa.
;
(cond-expand
(chicken (use srfi-13))
(gauche (use srfi-13))
(kawa (import (srfi :13)))
(else (import (scheme base) (scheme write)))) ; R7RS
(define msg "The quick brown fox jumps over the lazy dog.")
(define key 13)
(define (caesar char)
(define A (char->integer #\A))
(define Z (char->integer #\Z))
(define a (char->integer #\a))
(define z (char->integer #\z))
(define c (char->integer char))
(integer->char
(cond ((<= A c Z) (+ A (modulo (+ key (- c A)) 26)))
((<= a c z) (+ a (modulo (+ key (- c a)) 26)))
(else c)))) ; Return other characters verbatim.
(display (string-map caesar msg))
(newline)

View file

@ -13,7 +13,7 @@
(e (tostringp (+ #\a (mod (+ i k) 26))))
(P (upcase-str p))
(E (upcase-str e)))
'(((,p ,e) (,P ,E))
^(((,p ,e) (,P ,E))
((,e ,p) (,E ,P))))))
@(deffilter enc . @(mappend (fun first) enc-dec))
@(deffilter dec . @(mappend (fun second) enc-dec))