从 Curry-0, 1, 2 到 ...n

问题描述

继上一个我问到的关于编写咖喱函数的问题之后,How to create a make-curry function like racket has,我已经开始编写 0、1、2 的固定大小写——它们与教堂数字非常相似,非常简洁.这是我目前所拥有的:

(define (curry-0 func)
  func)

(define hello (begin (display "Hello!") (newline)))
(curry-0 hello)
; Hello!
(define (curry-1 func)
  (lambda (x)
      (func x )))

((curry-1 -) 2)
; -2
(define (curry-2 func)
  (lambda (x)
    (lambda (y)
      (func x y))))

(((curry-2 +) 2) 3)
5

模式似乎是:

(define curry-n func
     (lambda (x1)
        (lambda (x2)
           ...
            (lambda (xn)
                (func x1 x2 ... xn)

但是,我在“构建”n 嵌套 lambda 表达式时遇到了一些麻烦。我想我可以用类似的东西构建一个嵌套的 lambda:

(define (curry-n num func)
     (if (num > 0)
         (lambda (?) (curry-n (- num 1) func))))

但我不确定如何将变量“绑定”到每个 lambda 函数,以及如何最终将这些相同的变量(按顺序)传递给 (func x1 x2 ... xn) 函数

这是不正确的,但我已经开始...

(define (curry-n num func)
 (if (> num 0)
      ; but lambda won't accept a number,possible to string-format?
      (curry-n (- num 1) (lambda (num)))))

这怎么可能?

解决方法

其他答案可能会更有效一些,因为它们积累了具体的参数列表,或者更有趣,因为它们允许您一次采用多个参数,但在我看来,这是一个更好的学习练习,而且更简单,只是编写一个基本函数,一次接受一个参数,不需要累加器。

(define (curry n f)
  (cond ((= n 1) f)
        (else (lambda (x)
                (curry (- n 1)
                       (lambda args
                          (apply f (cons x args))))))))

 > (((curry 2 +) 5) 4)
=> 9

我们不累积参数,而是创建一个新的 lambda,它每次都需要更少的参数,并对其进行柯里化。它的行为非常类似于您对固定 n 的尝试的概括。它与 rawrex 的(完全合理的)答案是相同的基本方法,但没有处理更大块的 args 的花哨设施。

,

使用循环

您需要某种 loop 来从 arg 中收集每个 lambda -

(define (curry-n num f)
  (let loop
    ((n num)
     (args null))
    (lambda ((arg null))
      (cond ((= n 0)
             (apply f (reverse args)))
            ((= n 1)
             (apply f (reverse (cons arg args))))
            (else
             (loop (- n 1) (cons arg args)))))))

curry 应该总是返回一个过程,所以你可以看到 loop总是返回一个 lambda,即使对于num = 0 情况。每个 arg 都被 cons 放到 args 上,创建了一个反向的参数列表。这就是我们在reverse执行用户提供的过程 args 之前apply f 的原因。

它是这样工作的 -

(define (hello)
  (println "hello world"))

((curry-n 0 hello))
"hello world"
((((curry-n 3 +) 10) 20) 30)
60

使用分隔的延续

本着分享学习练习的精神,请花一些时间使用 delimited continuations -

查看此示例
(require racket/control)

(define (curry-3 f)
  (reset (f (shift k k) (shift k k) (shift k k))))

(define (hello a b c)
  (printf "hello world ~a ~a ~a\n" a b c))

((((curry-3 hello) 10) 20) 30)
hello world 10 20 30

要获得 curry-n,我们需要做的就是构建一个 n 延续列表!

(require racket/control)

(define (curry-n n f)
  (reset (apply f (build-list n (lambda (_) (shift k k))))))

它就像我们的第一个一样 -

(define (hello a b c)
  (printf "hello world ~a ~a ~a\n" a b c))

((((curry-n 3 hello) 10) 20) 30)
"hello world 10 20 30"
((((((curry-n 5 +) 10) 20) 30) 40) 50)
150

您可以将这个过程想象成这样的工作,其中每个 __ 都是一个要填充的“洞” -

(f __ __ __)
    \
     \_ (lambda (x)
          (f x __ __))
                \
                 \_ (lambda (y)
                      (f x y __))
                              \
                               \_ (lambda (z)
                                    (f x y z))

唯一的区别是要填充 n 个孔,因此我们构建了一个 n 孔和 apply -

(build-list 3 (lambda (_) (shift k k)))
(list __
       \
        \_ (lambda (x)
              (list x __
                       \
                        \_ (lambda (y)
                             (list x y __
                                        \
                                         \_ (lambda (z)
                                              (list x y z))
(apply f (list x y z)

方案

我没有注意到你在 Scheme 中做这项工作。我们可以定义 build-list -

(define (build-list n f)
  (let loop
    ((m 0))
    (if (>= m n)
        '()
        (cons (f m) (loop (+ m 1))))))

我们可以使用 Olivier Danvy 对 shift/reset 的原始实现,

(define-syntax reset
  (syntax-rules ()
    ((_ ?e) (reset-thunk (lambda () ?e)))))

(define-syntax shift
  (syntax-rules ()
    ((_ ?k ?e) (call/ct (lambda (?k) ?e)))))

(define *meta-continuation*
  (lambda (v)
    (error "You forgot the top-level reset...")))

(define abort
  (lambda (v)
    (*meta-continuation* v)))

(define reset-thunk
  (lambda (t)
    (let ((mc *meta-continuation*))
      (call-with-current-continuation
        (lambda (k)
      (begin
        (set! *meta-continuation* (lambda (v)
                    (begin
                      (set! *meta-continuation* mc)
                      (k v))))
        (abort (t))))))))

(define call/ct
  (lambda (f)
    (call-with-current-continuation
      (lambda (k)
    (abort (f (lambda (v)
            (reset (k v)))))))))

我们的curry-n可以保持不变 -

(define (curry-n n f)
  (reset (apply f (build-list n (lambda (_) (shift k k))))))

((((((curry-n 5 +) 10) 20) 30) 40) 50)
150
,

我建议您改为编写以下函数:

;; create `num` nested lambdas,where the body applies `func` to
;; `args-so-far` along with the arguments of those nested lambdas
(define (curry-n* func num args-so-far)
  ...)

这是它的用法:

(define (sub-4 a b c d)
  (- a b c d))

(curry-n* sub-4 0 (list 10 1 2 3)) 
;=> should evaluate to 10 - 1 - 2 - 3 = 4

(curry-n* sub-4 1 (list 10 1 2)) 
;=> should evaluate to a procedure that accepts x,;   and returns 10 - 1 - 2 - x = 7 - x

(curry-n* sub-4 2 (list 10 1)) 
;=> should evaluate to a procedure that accepts x,;   and returns a procedure that accepts y,;   and returns 10 - 1 - x - y = 9 - x - y

对于基本情况 (num = 0),您需要使用函数 apply

拥有 curry-n* 后,您可以创建 curry-n,它调用 curry-n* 作为辅助函数。


一旦你有了解决方案,你会发现它不是很有效。你可以修改curry-n*来翻转args-fo-far,这样它的用法是这样的:

(define (sub-4 a b c d)
  (- a b c d))

(curry-n* sub-4 0 (list 3 2 1 10)) 
;=> should evaluate to 4

(curry-n* sub-4 1 (list 2 1 10)) 
;=> should evaluate to a procedure that accepts x,;   and returns 7 - x

(curry-n* sub-4 2 (list 1 10)) 
;=> should evaluate to a procedure that accepts x,;   and returns 9 - x - y