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)) 94here 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) => 94Your 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更多推荐
发布评论