使用“MIT风格的curried-procedure表单”和传递给`curry`函数的lambda表达式有什么区别吗?

时间:2016-03-12 07:25:14

标签: racket

根据后面的代码段,example-func-Aexample-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))))

1 个答案:

答案 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的灵活性会在运行时受到一点性能影响。)