Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.35 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 (round (modulo (+ (car ascii) i) 26))
  30.                 (encode-iter (cdr ascii)))))
  31.     (char-list->string
  32.      (ascii-list->char-list (encode-iter ascii-list)))))
  33.  
  34. (define (decode text i)
  35.   (let ((ascii-list (char-list->ascii-list
  36.                      (string->char-list
  37.                       text))))
  38.     (define (decode-iter ascii)
  39.       (if (eq? ascii '())
  40.           '()
  41.           (cons (- (car ascii) i)
  42.                 (decode-iter (cdr ascii)))))
  43.     (char-list->string
  44.      (ascii-list->char-list
  45.       (decode-iter ascii-list)))))
  46.  
  47. (define hello "helloz")
  48.  
  49. (define encoded (encode hello 2))
  50.  
  51. (decode encoded 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement