Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Racket 球拍错误:需要非空列表,但给定为空_Racket - Fatal编程技术网

Racket 球拍错误:需要非空列表,但给定为空

Racket 球拍错误:需要非空列表,但给定为空,racket,Racket,我正在编写一个程序,它要求我将str中的所有大写字母转换为小写,并将小写字母转换为大写字母,所有其他字符保持不变。 下面是我的代码: (define (switch-case str) (list->string (switch-char (string->list str)))) (define (switch-char loc) (cons (cond [(empty? (first loc)) empty] [(char-lower-

我正在编写一个程序,它要求我将str中的所有大写字母转换为小写,并将小写字母转换为大写字母,所有其他字符保持不变。 下面是我的代码:

(define (switch-case str)
  (list->string (switch-char (string->list str))))

(define (switch-char loc)
   (cons
    (cond
      [(empty? (first loc)) empty]
      [(char-lower-case? (first loc)) (char-upcase (first loc))]
      [(char-upper-case? (first loc)) (char-downcase (first loc))]
      [else (first loc)]) (switch-char (rest loc))))
(开关箱“ABC”)的错误信息为:

第一:需要一个非空列表;给定:空


有人能帮我吗?我不知道代码的哪一部分是错误的:(

您的代码中有几个语法错误。我建议您花更多时间研究Scheme的基本语法,以及如何构造递归过程。请注意:

  • 开头的
    cons
    不应该在那里
  • 基本情况是错误的,您应该要求
    (空?loc)
  • 最后一个案例不正确,这不是
    else
    的用法
  • 最严重的错误是:在所有情况下都忘记调用递归。这就是
    cons
    发挥作用的地方
此版本解决了上述所有问题:

(define (switch-char loc)
  (cond
    [(empty? loc) empty]
    [(char-lower-case? (first loc))
     (cons (char-upcase (first loc)) (switch-char (rest loc)))]
    [(char-upper-case? (first loc))
     (cons (char-downcase (first loc)) (switch-char (rest loc)))]
    [else (cons (first loc) (switch-char (rest loc)))]))