我写了以下字符串加密代码。但我希望能够使用(读取)而不是使用硬编码值从键盘获取加密密钥。但是我不能这样做,因为map将列表作为参数。
#lang racket
(define character-encryption
(lambda (ch)
(if (char-alphabetic? ch)
(character-rotation ch 3)
ch))) ; non-letter will not be encrypted.
(define character-rotation
(lambda (ch num)
(integer->char (+ (char->integer ch) num)))) ; key=3
(define string-encryption
(lambda (str)
(list->string (map character-encryption (string->list str)))))
更新
> (string-encryption (read))
"foo bar" ; <- entered
"irr edu" ; output
更新
#lang racket
(define character-encryption
(lambda (key)
(lambda (ch)
(if (char-alphabetic? ch)
(character-rotation ch key)
ch)))) ; non-letter will not be encrypted.
(define character-rotation
(lambda (ch num)
(integer->char (+ (char->integer ch) num)))) ; key=3 (I want to read this number)
(define string-encryption
(lambda (key str)
(list->string (map (character-encryption key) (string->list str)))))