8.16.0.1
2.5 柯里化
2.5.1 curry定义
syntax
(define/curry (id args ...+) body ...+)
结合了define及curry,直接定义柯里化函数。
Examples:
> (define/curry (my/add a b) (+ a b)) > (my/add) #<procedure:curried:my/add>
> (my/add 1) #<procedure:curried:my/add>
> (my/add 1 2) 3
2.5.2 定义柯里化、约束型函数
syntax
(define/curry/contract (id args ...+) body ...+)
与define/curry类似,同样定义出柯里化函数,唯一不同在于它内部用了define/contract,能对函数做出约束。
Examples:
> (define/curry/contract (my/add a b) (-> number? number? number?) (+ a b)) > (my/add) #<procedure:curried:my/add>
> (my/add 1 2) 3
> ((my/add 1) "2") my/add: contract violation
expected: number?
given: "2"
in: the 2nd argument of
(-> number? number? number?)
contract from: (function my/add)
blaming: top-level
(assuming the contract is correct)
at: eval:30:0
2.5.3 柯里化不定参函数
syntax
(curry/n n f)
n = 参数个数 f = 已定义的函数名
将不定参函数f,转化成全新、参数个数确定(n个)的柯里化函数。
Examples: