35 lines
1020 B
Racket
35 lines
1020 B
Racket
#lang racket
|
|
|
|
(define (get-input)
|
|
(display "Enter your string ")
|
|
(read-line (current-input-port) 'any))
|
|
|
|
(define (decode input)
|
|
(define bits '())
|
|
(for/list ((c (string->list input)))
|
|
(cond ((equal? c #\u200C) (set! bits (append bits '(0))))
|
|
((equal? c #\u200B) (set! bits (append bits '(1))))))
|
|
(bits->text bits))
|
|
|
|
(define (bits->text bits)
|
|
(define bytes (collect-bytes bits))
|
|
(apply string (map (lambda (byte)
|
|
(integer->char (bits->number byte)))
|
|
bytes)))
|
|
|
|
(define (collect-bytes bits)
|
|
(define (collect-bytes-iter bits acc)
|
|
(cond ((null? bits) (reverse acc))
|
|
((>= (length bits) 8) (collect-bytes-iter (drop bits 8) (cons (take bits 8) acc)))
|
|
(else (error "Invalid bits"))))
|
|
(collect-bytes-iter bits '()))
|
|
|
|
(define (bits->number bits)
|
|
(let ((binary-string (apply string (map (lambda (bit) (if (= bit 1) #\1 #\0)) bits))))
|
|
(string->number binary-string 2)))
|
|
|
|
(define (main)
|
|
(let ((input (get-input)))
|
|
(displayln (decode input))))
|
|
|
|
(main) |