方案 - 两个清单之间的最大差异

时间:2022-09-10 21:27:38

Given two lists of numbers (of the same length), return the largest difference between corresponding pairs of numbers (without creating another list). For example...

给定两个数字列表(长度相同),返回相应数字对之间的最大差异(不创建另一个列表)。例如...

Given [ 2 6 3 ] and [ 4 6 2 ],
their differences are [ (2-4) (6-6) (3-2) ], or [ 2 0 1 ],
so the largest difference is 2.

Notice that difference is always positive (the absolute value).

请注意,差异始终为正(绝对值)。

1 个解决方案

#1


I only provide code when OP provide code so here is the solution just described. Happy coding!

我只在OP提供代码时提供代码,所以这里是刚刚描述的解决方案。快乐的编码!

You can use fold-left and supply a procedure that takes the maximum of accumulator and the absolute of the difference between the two elements being processed.

您可以使用fold-left并提供一个过程,该过程采用最大累加器和正在处理的两个元素之间的差异的绝对值。

fold-left is the R6RS name and resides in the library (rnrs lists (6)). For a compatible approach you'll need to use SRFI-1 List Library. Here it's called fold and the accumulator is the last argument instead of the first.

fold-left是R6RS名称,位于库中(rnrs lists(6))。对于兼容的方法,您需要使用SRFI-1列表库。这里称为fold,累加器是最后一个参数而不是第一个参数。

Many R5RS Scheme implementations and Languages that are derived from a Scheme language (like Racket) has a left fold implemented by the name foldl. You need to check it's documentation to get the argument order as they vary. It's not a part of the standard so it's not portable between implementations. I urge you to either use R6RS or SRFI-1.

许多R5RS Scheme实现和从Scheme语言(如Racket)派生的语言都有一个由foldl名称实现的左侧折叠。您需要检查它的文档以获取参数顺序,因为它们会有所不同。它不是标准的一部分,因此在实现之间不可移植。我劝你要么使用R6RS,要么使用SRFI-1。

#1


I only provide code when OP provide code so here is the solution just described. Happy coding!

我只在OP提供代码时提供代码,所以这里是刚刚描述的解决方案。快乐的编码!

You can use fold-left and supply a procedure that takes the maximum of accumulator and the absolute of the difference between the two elements being processed.

您可以使用fold-left并提供一个过程,该过程采用最大累加器和正在处理的两个元素之间的差异的绝对值。

fold-left is the R6RS name and resides in the library (rnrs lists (6)). For a compatible approach you'll need to use SRFI-1 List Library. Here it's called fold and the accumulator is the last argument instead of the first.

fold-left是R6RS名称,位于库中(rnrs lists(6))。对于兼容的方法,您需要使用SRFI-1列表库。这里称为fold,累加器是最后一个参数而不是第一个参数。

Many R5RS Scheme implementations and Languages that are derived from a Scheme language (like Racket) has a left fold implemented by the name foldl. You need to check it's documentation to get the argument order as they vary. It's not a part of the standard so it's not portable between implementations. I urge you to either use R6RS or SRFI-1.

许多R5RS Scheme实现和从Scheme语言(如Racket)派生的语言都有一个由foldl名称实现的左侧折叠。您需要检查它的文档以获取参数顺序,因为它们会有所不同。它不是标准的一部分,因此在实现之间不可移植。我劝你要么使用R6RS,要么使用SRFI-1。