我的递归函数没有返回任何东西,为什么?

My recursive function is not returning anything, why?

所以我对 scheme 很陌生,第一天就用它编程。我正在尝试创建一个递归函数,它基本上需要两个参数 numlist, num numlist 是一个数字列表,而 num 是一个数字。该函数获取列表,遍历每个元素并将其与 num 进行比较,如果数字大于 num,则将其替换为 1,否则将其替换为 0。

例如:arguments( (1,2,3,4,5,6,7,8,9,10) , 5) 将列表转换为 (0,0,0,0,0,1, 1,1,1,1)

但由于某种原因我的函数没有返回任何东西,它没有在我的屏幕上打印任何东西,为什么?

(define (greater-nums-in-list numlist num)
    (cond
    [(null? numlist) ()]
    [(> (car numlist) num) (append 1 (greater-nums-in-list((cdr numlist) num))]
    [else (append 0 (greater-nums-in-list((cdr numlist) num)))]
    )
)

任何帮助将不胜感激,这只是个人学习代码,所以我并不着急。

它打印到屏幕上的 "nothing" 是因为递归底部的基本条件:

[(null? numlist) ()]

这意味着当您完成列表迭代后,您 return () - 这不是空列表 - 应用 "nothing",空列表是 '().

有几个括号错误,您不能将 10 附加到列表中 - 您需要先将它们包装在列表中:

(define (greater-nums-in-list numlist num)
  (cond
    [(null? numlist) '()]
    [(> (car numlist) num) (append '(1) (greater-nums-in-list (cdr numlist) num))]
    [else (append '(0) (greater-nums-in-list (cdr numlist) num))]
    )
  )

用法

>  (greater-nums-in-list '(1 2 3 4) 2)
'(0 0 1 1)
> (greater-nums-in-list '(1 2 3 4 5 6 7 8 9 10) 5)
'(0 0 0 0 0 1 1 1 1 1)
>