简体   繁体   English

在Scheme中找到列表的最大值和最小值

[英]Find the max and min of a list in Scheme

I need to find the max and min of a list and then add in a new list. 我需要找到列表的最大值和最小值,然后添加一个新列表。 This is my code so far: 到目前为止,这是我的代码:

(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)))))

and now I'm stuck. 现在我被卡住了。 How can i add nMin + nMax in a new list? 如何在新列表中添加nMin + nMax My output should be something like this: 我的输出应该是这样的:

'(57 -4)

Try this: 尝试这个:

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

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

(max-min alist)
> '(57 -4)

类似于(cons (nMax alist) (cons (nMin alist) '()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM