Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 1.42 KB | None | 0 0
  1. #lang racket
  2.  
  3. (define (string->char-list text)
  4.   (string->list text))
  5.  
  6. (define (char-list->string clist)
  7.   (list->string clist))
  8.  
  9. (define (char-list->ascii-list char-list)
  10.   (if (eq? char-list '())
  11.       '()
  12.       (cons (char->integer (car char-list))
  13.             (char-list->ascii-list (cdr char-list)))))
  14.  
  15.  
  16. (define (ascii-list->char-list ascii-list)
  17.   (if (eq? ascii-list '())
  18.       '()
  19.       (cons (integer->char (car ascii-list))
  20.             (ascii-list->char-list (cdr ascii-list)))))
  21.  
  22. (define (encode text i)
  23.   (let ((ascii-list (char-list->ascii-list
  24.                      (string->char-list
  25.                       text))))
  26.     (define (encode-iter ascii)
  27.       (if (eq? ascii '())
  28.           '()
  29.           ;(cons (+ (car ascii) i)
  30.           (cons (+ (modulo (+ (- (car ascii) 65) i) 26) 65)
  31.                 (encode-iter (cdr ascii)))))
  32.     (char-list->string
  33.      (ascii-list->char-list (encode-iter ascii-list)))))
  34.  
  35. (define (decode text i)
  36.   (let ((ascii-list (char-list->ascii-list
  37.                      (string->char-list
  38.                       text))))
  39.     (define (decode-iter ascii)
  40.       (if (eq? ascii '())
  41.           '()
  42.           (cons (+ (modulo (- (- (car ascii) 65) i) 26) 65)
  43.                 (decode-iter (cdr ascii)))))
  44.     (char-list->string
  45.      (ascii-list->char-list
  46.       (decode-iter ascii-list)))))
  47.  
  48. (define hello "HELLO")
  49.  
  50. (define encoded (encode hello 1))
  51.  
  52. (decode encoded 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement