On this page:
2.5.1 curry定义
define/  curry
2.5.2 定义柯里化、约束型函数
define/  curry/  contract
2.5.3 柯里化不定参函数
curry/  n
8.12

2.5 柯里化🔗

2.5.1 curry定义🔗

syntax

(define/curry (id args ...+) body ...+)

结合了definecurry,直接定义柯里化函数。

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:
> (define my/add (curry/n 2 +))
> (my/add)

#<procedure:curried:+>

> (my/add 3 4)

7

> (my/add 3 4 5)

curried:+: arity mismatch;

 the expected number of arguments does not match the given

number

  given: 3