DrRacket - 字母的数字等价物

DrRacket - numerical equivalents of letters

我正在编写一个函数,将每个字母替换为在字母圆圈后恰好 13 个空格处找到的字母,并保持非字母字符不变。对于字母A-N和a-n,我只需要将对应的整数加上13,然后将其转换为字符即可。这就是让我困惑的地方——对于字母N-Z和n-z,解码后对应的整数应该减去13,我不知道如何写问题表达式。我试过使用 char<=?和字符> =?但是有错误。

(define (rot-13 msg)
  (list->string (list-rot-13 (string->list msg))))

(define (list-rot-13 loc)
  (cond
    [(empty? loc) empty]
    [else
     (cons (convert-char (first loc)) (list-rot-13 (rest loc)))]))

(define (convert-char c)
  (cond
    [... (integer->char (- (char->integer c) 13))]
    [... (integer->char (+ (char->integer c) 13))]
    [else c])

您可以使用 char-ci>=?。例如。

(char-ci>=? #\l #\n) ; ==> #f
(char-ci>=? #\n #\n) ; ==> #t
(char-ci>=? #\p #\n) ; ==> #t

现在 char>=? 与数字 >=(char>=? #\A #\b) ; ==> #t 相同。 ci 代表不区分大小写,所以 (char>=? #\A #\b) ; ==> #f

PS:我认为你可以只使用一个谓词,它只适用于 if。它要么等于或大于 #\n,要么不等于。您还可以通过始终添加 (integer->char (+ (char->integer c) (if (char-ci>=? ...) -13 13))

在数学运算中执行 if