在用球拍初学者语言编写递归函数时卡住了?

Stuck while writing a recursion function in racket beginner language?

所以最近我学习了递归函数,我正在尝试一些练习然后我就卡住了。

问题是 list-nth-item,它使用一个列表 (lst) 和一个自然数 (n),以及 如果存在,则生成 lst 中的第 n 个元素,否则该函数生成 false。笔记 第一项在索引 0 中。例如:(list-nth-item (list 1 2 3) 0) 产生 1

这是我的代码

     ;;list-nth-item consumes list and a natural number
     ;;and produces the n-th element in the list or false
     ;;list-nth-item: List Nat -> (Anyof Any false)
    (define (list-nth-item lst n)
          (cond
            [(empty? lst)false]
            [(= n 0)(first lst)]
            [else ????]))


         (list-nth-item (list 1 2 3)2)--> should produce 3

我知道这不是一个正确的递归,当 n = 0 时它应该在列表示例中产生第一个数字 (list-nth-item (list 1 2 3)0) 应该给出 1。 我是新手,只是不知道如何形成递归。

将列表视为传送带:您检查是否到达了您的物品(使用第一种情况 (= n 0)),如果没有(else 情况),您只需将传送带移动取出列表的尾部(使用 cdr 函数)并再次重复该过程。

这可以像下面这样完成:

(define (list-nth-item lst n)
  (cond
    [(empty? lst) #f]
    [(zero? n) (car lst)]
    [else (list-nth-item     ; <-- repeats the process
           (cdr lst)         ; <-- shifts the "belt"
           (sub1 n))]))      ; <-- updates the number of steps to go

PS:list-ref函数已经完成了。