0

私はsicpに取り組んでおり、accumulateを使用して「最後の」関数を書き込もうとしています。

(define (accumulate f x xs)
  (if (null? xs)
      x
      (f (car xs)
         (accumulate f x (cdr xs)))))

 (last '(1 2 3 4 5)) ;;=> (5)

これを試しましたが、機能しません

 (define (last seq)
   (accumulate (lambda (x y) x)
               '()
               seq))
4

1 に答える 1

2

これを試して:

(define (last lst)
  (accumulate (lambda (x y)
                (if (null? y)
                    (cons x y)
                    y))
              '() lst))
于 2013-01-29T19:28:47.717 に答える