Functional programming 方案将列表定义为局部变量

Functional programming 方案将列表定义为局部变量,functional-programming,scheme,max,racket,fold,Functional Programming,Scheme,Max,Racket,Fold,我的目标是将list定义为局部变量,以便从中获取max元素。我的代码: #lang racket (define (f a b c) (list (+ (* a a) (* b b) (* c c)) (+ a c)) (define (max-of-list-2 lst) (foldr max (first lst) (rest lst))) (max max_from_list 12)) (f 5 6 7) 在第二行中,我定义了带有计算错误的列表。我的目标是

我的目标是将list定义为局部变量,以便从中获取max元素。我的代码:

#lang racket
(define (f a b c)
    (list (+ (* a a) (* b b) (* c c)) (+ a c))
    (define (max-of-list-2 lst)
      (foldr max (first lst) (rest lst)))
  (max max_from_list 12))
(f 5 6 7)

在第二行中,我定义了带有计算错误的列表。我的目标是将它传递到下一行,以便从中获取最大数量,最后从列表的最大值和12中获取最大数量。我做错了什么。如何处理它?

您需要使用内部定义,如下所示:

(define (f a b c)
    (define max_from_list (list (+ (* a a) (* b b) (* c c)) (+ a c)))
    (define (max-of-list-2 lst)
      (foldr max (first lst) (rest lst)))
  (max max_from_list 12))

要弄清你的意图有点困难,但这是我能想到的最好的理解-

(define (max a b)
  (if (< a b)
      b
      a))

(define (f a . more)
  (if (empty? more)
      (max a 12)
      (max a (apply f more))))

(f 5 6 7)
; => 12

(f 5 20 30)
; => 30
(定义(最大a和b)
(如果( 12
(f 5 20 30)
; => 30
您可以在函数顶部使用多个
定义

你是说

(define (f a b c)
  (define lst (list (+ (* a a) (* b b) (* c c)) 
                    (+ a c)))
  (define (max-of-list-2 lst)
      (foldr max (first lst) (rest lst)))
  (max (max-of-list-2 lst) 12))
但这只相当于

(define (f a b c)
  (foldr max 12 (list (+ (* a a) (* b b) (* c c)) 
                      (+ a c))))

用另一个define命名:
(define lst(list…)
。然后,您可以在任何lisp表达式中按名称引用它,例如函数调用。但我无法将(max-max-of-list-2(lst)12)调用到deep
(max(max-of-list-2 lst)12)
@IntoTheDeep@IntoTheDeep我们很难接受您的非工作代码并了解您的实际意图。你能更好地描述一下这个问题吗?你能想出比
f
更好的名字吗?返回值应该是什么?
(*a)
(*b)
(*c)
(+ac)
值的意义是什么?这只是一项任务吗?