使用球拍中的键排序功能排序列表(Sort list of function using sort with key in racket)

系统教程 行业动态 更新时间:2024-06-14 17:04:02
使用球拍中的键排序功能排序列表(Sort list of function using sort with key in racket) (functionsort functionlist value)

functionsort采用2个参数,函数列表和单个值。 当给定“value”时,返回以递增顺序排序的相同函数列表。

例如:这是我的功能列表

(define myfunctions (list (lambda (x) (+ x 3)) (lambda (x) (- 100 x)) (lambda (x) (* x 2)))) (define fs (function-sort myfunctions 5))

应该返回以下内容

((first fs) 6) ; (first fs) is (lambda (x) (+ x 3)) 9 ((second fs) 6) ; (second fs) is (lambda (x) (* x 2)) 12 ((third fs) 6) ; (third fs) is (lambda (x) (- 100 x)) 94

这是我到目前为止所得到的:

define (function-sort functions value ) (map (lambda (y) (y value)) functions))

计算值,9 94 12 un sorted。 我试着用

(sort myfunctions #:key (function-sort myfunctions 10) <)

这给了我错误

. . sort: contract violation expected: (any/c . -> . any/c) given: '(13 90 20).

有什么建议吗? 提前致谢

(functionsort functionlist value)

functionsort takes 2 argument, list of functions and a single value. return same list of function sorted in increasing order when given "value".

for example : here is my list of function

(define myfunctions (list (lambda (x) (+ x 3)) (lambda (x) (- 100 x)) (lambda (x) (* x 2)))) (define fs (function-sort myfunctions 5))

should return the following

((first fs) 6) ; (first fs) is (lambda (x) (+ x 3)) 9 ((second fs) 6) ; (second fs) is (lambda (x) (* x 2)) 12 ((third fs) 6) ; (third fs) is (lambda (x) (- 100 x)) 94

here is what i got so far:

define (function-sort functions value ) (map (lambda (y) (y value)) functions))

which compute the value , 9 94 12 un sorted. and I try to use

(sort myfunctions #:key (function-sort myfunctions 10) <)

which gives me error

. . sort: contract violation expected: (any/c . -> . any/c) given: '(13 90 20).

Any suggestion ? thanks in advance

最满意答案

你的尝试是在正确的轨道上,但你必须为#:key传递足够的参数 - 我们想用给定的值来评估每个函数,并且sort过程将根据每个函数返回的结果对函数的输入列表进行排序。函数,当应用于值时。 尝试这个:

(define (function-sort functions value) (sort functions < #:key (lambda (f) (f value))))

为了提高具有昂贵功能的大型列表的性能,请注意Will Ness的建议:使用#:cache-keys? #t #:cache-keys? #t将阻止对同一个参数进行多次评估,事实上它将类似于您打算首先使用map进行的操作(即: 排序之前预先计算值)。 考虑:

(define (function-sort functions value) (sort functions < #:cache-keys? #t #:key (lambda (f) (f value))))

无论哪种方式,它都按预期工作:

(define myfunctions (list (lambda (x) (+ x 3)) (lambda (x) (- 100 x)) (lambda (x) (* x 2)))) (define fs (function-sort myfunctions 5)) ((first fs) 6) => 9 ((second fs) 6) => 12 ((third fs) 6) => 94

Your attempt was on the right track, but you have to pass an adequate argument for #:key - we want to evaluate each function with the given value, and thesort procedure will sort the input list of functions according to the result returned by each function, when applied to the value. Try this:

(define (function-sort functions value) (sort functions < #:key (lambda (f) (f value))))

For improving performance on large lists with expensive functions, pay attention to Will Ness' suggestion: using #:cache-keys? #t will prevent multiple evaluations for the same argument, and in fact will be similar to what you intended to do with map in the first place (that is: precomputing the values before sorting). Consider:

(define (function-sort functions value) (sort functions < #:cache-keys? #t #:key (lambda (f) (f value))))

Either way, it works as expected:

(define myfunctions (list (lambda (x) (+ x 3)) (lambda (x) (- 100 x)) (lambda (x) (* x 2)))) (define fs (function-sort myfunctions 5)) ((first fs) 6) => 9 ((second fs) 6) => 12 ((third fs) 6) => 94

更多推荐

本文发布于:2023-04-24 20:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d660e573218ca1626e258ea20de48766.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:球拍   功能   列表   Sort   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!