-1

过程 find-last 接受一个包含 1 个参数的谓词和一个列表,并返回列表中满足该谓词的最右边的元素。如果不存在这样的元素,则返回#f

(define (find-last-helper ans)
  (if (predicate? ans)
      (add1 ans)))
(define find-last
  (lambda (predicate? ls)
    (cond 
      [(null? ls) #f]
       [(equal? predicate? (car ls)) car ls]
        [else(find-last-helper(find-last ls(cdr ls)))])))

我是否在帮助程序或查找最后程序中遗漏了某些内容?它返回一个语法错误。

4

1 回答 1

1

predicate? is not visible to find-last-helper. The way to fix that would be to move the definition of find-last-helper inside find-last's lambda block or to add an extra parameter to find-last-helper so that predicate? can be passed to it.

I would recommend the former; assuming (from the name) that find-last-helper is only useful to find-last, it keeps the outer environment cleaner.

Note: assuming you fix the errors but keep the basic structure, your function is going to return the leftmost match - the first match it finds - rather than the rightmost. To see this clearly, assume that the leftmost item in the list satisfies the predicate and step through your code; it should be clear that the third, recursive cond line is not executed.

于 2012-10-09T11:21:14.597 回答