Skip to content

Instantly share code, notes, and snippets.

@Glorp
Created October 31, 2018 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Glorp/154c4a65767150460892c644ad5d7ee1 to your computer and use it in GitHub Desktop.
Save Glorp/154c4a65767150460892c644ad5d7ee1 to your computer and use it in GitHub Desktop.
#lang racket
(define alphabet (list->vector (string->list "abcdefghijklmnopqrstuvwxyzæøå ")))
(define (caeschar shift char)
(match char
[#\newline #\newline]
[#\return #\return]
[c (match (vector-member c alphabet)
[#f #f]
[i (vector-ref alphabet (modulo (+ i shift) (vector-length alphabet)))])]))
(define (caesport shift in out)
(let loop ()
(define c (read-char in))
(unless (eof-object? c)
(define new-char (caeschar shift c))
(when new-char (write-char new-char out))
(loop))))
(define (caesfile shift filename)
(call-with-input-file filename #:mode 'text
(λ (p)
(caesport shift p (current-output-port)))))
(define (caestring shift s)
(define out (open-output-string))
(caesport shift (open-input-string s) out)
(get-output-string out))
(module+ test
(require rackunit)
(check-equal? (caestring 7 "experience is the teacher of all things")
"lawlyplujlgpzgæolgælhjolygvmghssgæopunz")
(check-equal? "no one is so brave that he is not disturbed by something unexpected"
(caestring -7 "uvgvulgpzgzvgiyhålgæohægolgpzguvægkpzæøyilkgibgzvtlæopungøulawljælk"))
(check-equal? (caestring 7 "i had rather be first in a village than second at rome")
"pgohkgyhæolygilgmpyzægpughgåpsshnlgæohugzljvukghægyvtl")
(check-equal? (caestring 7 "men freely believe that which they desire")
"tlugmyllsbgilsplålgæohæg opjogæolbgklzpyl")
(check-equal? "i came i saw i conquered"
(caestring -7 "pgjhtlgpgzh gpgjvuxølylk")))
(module+ main
; racket caesar.rkt 7 caestest.txt -d
(match (current-command-line-arguments)
[(vector num filename) (caesfile (string->number num) filename)]
[(vector num filename "-d") (caesfile (- (string->number num)) filename)]
[x (printf "oh no: ~a~n" x)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment