[英]Syntax error in Dr Racket
The window keeps saying there is a error on the last part where it says (...(count-even-strings (rest alist)))
窗口一直在说最后一部分出现错误
(...(count-even-strings (rest alist)))
saying that it found on extra part but I just don't know how to fix it. 说它是多余的,但我只是不知道如何解决。
;; consumes a list of strings, list, and produces the number of strings in list that have an even length
(define (count-even-strings alist)
(cond
[(empty? alist) 0]
[else (+ (cond [(even? (string-length (first alist))) true]) 1)]
[else 0]) (count-even-strings (rest alist)))
Using an editor that aligns properly the code, like DrRacket, you could see that your function has the following structure: 使用可以使代码正确对齐的编辑器(例如DrRacket),您可以看到函数具有以下结构:
(define (count-even-string1s alist)
(cond
[(empty? alist) 0]
[else (+ (cond [(even? (string-length (first alist))) true])
1)]
[else 0])
(count-even-strings1 (rest alist)))
and has two else
inside the same cond
, which is not correct sintactically, while the last part is outside of the cond
. 并有两个
else
同里cond
,这是不正确sintactically,而最后一部分是外cond
。
A correct solution with the use of if
is the following: 下面是使用
if
正确解决方案:
(define (count-even-strings alist)
(if (empty? alist)
0
(+ (if (even? (string-length (first alist)))
1
0)
(count-even-strings (rest alist)))))
(count-even-strings '("a" "ab" "c"")) ;; => 2
or, using a single conditional: 或者,使用单个条件:
(define (count-even-strings alist)
(cond ((empty? alist) 0)
((even? (string-length (first alist))) (+ 1 (count-even-strings (rest alist))))
(else (count-even-strings (rest alist)))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.