在两个列表中查找最小成对产品
Find minimum pairwise product in two lists
我需要编写一个执行以下操作的函数:给定 [ 2 6 3 ]
和 [ 4 6 2 ]
,它们的乘积是 [ (2 * 4) (6 * 6) (3 * 2) ]
,或 [ 8 36 6 ]
,
所以最小的乘积是 6.
我有一个辅助函数来获取每个列表中的最小整数:
(define ( myListMax Lst )
( if ( null? Lst )
0
( if ( null? ( cdr Lst ) )
( car Lst )
( if ( > ( car Lst ) ( myListMax ( cdr Lst ) ) )
( car Lst )
( myListMax ( cdr Lst ) )
)
)
)
)
我有以下函数调用辅助函数来帮助我实现 objective。这是函数:
(define (smallProd LstOne LstTwo)
(*
(myListLeast(LstOne) (myListLeast(LstTwo)
))))
但是当我 运行 我的代码时,我收到以下消息:
(smallProd '(1 2 3) '(4 5 6))
; application: not a procedure;
; expected a procedure that can be applied to arguments
; given: '(1 2 3)
; [,bt for context]
非常感谢您的帮助。
您的代码(具有固定格式)假设两个最小的数字将位于同一位置 -
(define (smallProd LstOne LstTwo)
(* (myListLeast LstOne) ; fixed parens
(myListLeast LstTwo))) ; fixed parens
如果将其应用于 '(2 6 3)
和 '(4 6 2)
的示例输入,则两个最小的数字将是 2
和 2
,结果为 2 * 2 = 4
.这是不正确的,因为 '((* 2 4) (* 6 6) (* 3 2))
的最小 product 来自 3 * 2
,即 6
.
(define (smallest-product a b)
(if (or (null? a) (null? b)) ; if either list is empty
+inf.0 ; return base case
(min (* (car a) (car b)) ; otherwise min of 1st product
(smallest-product (cdr a) (cdr b))))) ; and the recursive result
(define (min a b)
(if (< a b) ; if a is less than b
a ; return a
b)) ; otherwise b
(smallest-product '(2 6 3) '(4 6 2))
6.0
如果给定一个空列表作为输入,则不能有最小乘积。在这种情况下 positive infinity、+inf.0
返回 -
(smallest-product '() '(5))
(smallest-product '(5) '())
(smallest-product '() '())
+inf.0
+inf.0
+inf.0
Scheme/Racket 与 C/Java 家族的语言明显不同。它对初学者非常友好,但如果您尝试引入从其他语言获得的实践,您会遇到困难。花时间学习这门强大的语言提高了我所有其他语言的编程技能。把一切都留在门口,你会得到最大的好处。
我需要编写一个执行以下操作的函数:给定 [ 2 6 3 ]
和 [ 4 6 2 ]
,它们的乘积是 [ (2 * 4) (6 * 6) (3 * 2) ]
,或 [ 8 36 6 ]
,
所以最小的乘积是 6.
我有一个辅助函数来获取每个列表中的最小整数:
(define ( myListMax Lst )
( if ( null? Lst )
0
( if ( null? ( cdr Lst ) )
( car Lst )
( if ( > ( car Lst ) ( myListMax ( cdr Lst ) ) )
( car Lst )
( myListMax ( cdr Lst ) )
)
)
)
)
我有以下函数调用辅助函数来帮助我实现 objective。这是函数:
(define (smallProd LstOne LstTwo)
(*
(myListLeast(LstOne) (myListLeast(LstTwo)
))))
但是当我 运行 我的代码时,我收到以下消息:
(smallProd '(1 2 3) '(4 5 6)) ; application: not a procedure; ; expected a procedure that can be applied to arguments ; given: '(1 2 3) ; [,bt for context]
非常感谢您的帮助。
您的代码(具有固定格式)假设两个最小的数字将位于同一位置 -
(define (smallProd LstOne LstTwo)
(* (myListLeast LstOne) ; fixed parens
(myListLeast LstTwo))) ; fixed parens
如果将其应用于 '(2 6 3)
和 '(4 6 2)
的示例输入,则两个最小的数字将是 2
和 2
,结果为 2 * 2 = 4
.这是不正确的,因为 '((* 2 4) (* 6 6) (* 3 2))
的最小 product 来自 3 * 2
,即 6
.
(define (smallest-product a b)
(if (or (null? a) (null? b)) ; if either list is empty
+inf.0 ; return base case
(min (* (car a) (car b)) ; otherwise min of 1st product
(smallest-product (cdr a) (cdr b))))) ; and the recursive result
(define (min a b)
(if (< a b) ; if a is less than b
a ; return a
b)) ; otherwise b
(smallest-product '(2 6 3) '(4 6 2))
6.0
如果给定一个空列表作为输入,则不能有最小乘积。在这种情况下 positive infinity、+inf.0
返回 -
(smallest-product '() '(5))
(smallest-product '(5) '())
(smallest-product '() '())
+inf.0
+inf.0
+inf.0
Scheme/Racket 与 C/Java 家族的语言明显不同。它对初学者非常友好,但如果您尝试引入从其他语言获得的实践,您会遇到困难。花时间学习这门强大的语言提高了我所有其他语言的编程技能。把一切都留在门口,你会得到最大的好处。