根据后面的代码段,example-func-A
和example-func-B
之间是否存在任何有意义的差异?
#lang racket/base
(require (only-in racket/function curry))
(define (((example-func-A x) y) z)
(+ x y z))
(define example-func-B
(curry
(lambda (x y z)
(+ x y z))))
答案 0 :(得分:7)
是的,example-func-A
(使用MIT风格的curried-procedure语法)不如example-func-B
灵活,因为它希望一次只能用一个参数调用 :
> (((example-func-A 4) 5) 6)
15
> (example-func-A 4 5 6)
example-func-A: arity mismatch;
the expected number of arguments does not match the given number
expected: 1
given: 3
arguments...:
4
5
6
context...:
/opt/homebrew-cask/Caskroom/racket/6.4/Racket v6.4/collects/racket/private/misc.rkt:87:7
相反,example-func-B
适用于接收多个(甚至零!)参数:
> (((example-func-B 4) 5) 6)
15
> (example-func-B 4 5 6)
15
> ((((example-func-B) 4)) 5 6)
15
(推测curry
的灵活性会在运行时受到一点性能影响。)