2

我需要找到一个列表的最大值和最小值,然后添加一个新列表。到目前为止,这是我的代码:

(define alist '(18 39 57 -4 0)

(define (nMax alist)
  (if (null? (cdr alist))
      (car alist)
      (if (> (car alist) (nMax (cdr alist)))
          (car alist)
          (nMax (cdr alist)))))

(define (nMin alist)
  (if (null? (cdr alist))
      (car alist)
      (if (< (car alist) (nMin (cdr alist)))
          (car alist)
          (nMin (cdr alist)))))

现在我被困住了。如何在新列表中添加nMin+ ?nMax我的输出应该是这样的:

'(57 -4)
4

2 回答 2

6

试试这个:

(define alist '(18 39 57 -4 0))

(define (max-min alist)
  (list (nMax alist)
        (nMin alist)))

(max-min alist)
> '(57 -4)
于 2012-04-19T19:23:33.170 回答
0

就像是(cons (nMax alist) (cons (nMin alist) '()))

于 2012-04-19T19:06:22.350 回答