4

nリストの最初の要素を返す方法は? ここに私が持っているものがあります:

(define returns(lambda (list n)
 (cond ((null? list) '())
 (((!= (0) n) (- n 1)) (car list) (cons (car list) (returns (cdr list) n)))
        (else '()))))

例:

(returns '(5 4 5 2 1) 2)
(5 4)

(returns '(5 4 5 2 1) 3)
(5 4 5)
4

1 に答える 1

12

あなたはtake手続きを求めています:

(define returns take)

(returns '(5 4 5 2 1) 2)
=> (5 4)

(returns '(5 4 5 2 1) 3)
=> (5 4 5)

これは宿題のように見えるので、最初から実装する必要があると思います。いくつかのヒント、空欄を埋めてください:

(define returns
  (lambda (lst n)
    (if <???>                     ; if n is zero
        <???>                     ; return the empty list
        (cons <???>               ; otherwise cons the first element of the list
              (returns <???>      ; advance the recursion over the list
                       <???>))))) ; subtract 1 from n

それをテストすることを忘れないでください!

于 2013-01-23T21:21:50.397 に答える