1

继上一个问题之后,我询问了有关编写 curry 函数的问题,How to create a make-curry function like racket has,我已经开始编写 0、1、2 的固定大小写——它们与 Church Numerals 非常相似很整洁。这是我到目前为止所拥有的:

(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)))))

怎么可能做到这一点?

4

3 回答 3

2

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

(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,它每次都需要更少的参数,然后 curry。它的行为非常类似于您对 fixed 尝试的概括n。这与 rawrex 的(完全合理的)答案相同的基本方法,但没有花哨的工具来处理更大块的 args。

于 2021-06-17T05:40:06.643 回答
1

使用循环

你需要某种东西来loop收集每个-arglambda

(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总是返回一个,即使是这样。每个都'd 到,创建一个反向的参数列表。这就是为什么我们在使用用户提供的程序之前, .lambdanum = 0argconsargsreverseargsapplyf

它是这样工作的 -

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

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

使用定界延续

本着分享学习练习的精神,花一些时间来回顾这个使用定界延续的例子-

(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
于 2021-06-17T05:15:02.290 回答
0

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

;; 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 a procedure that accepts y, 
;   and returns 9 - x - y
于 2021-06-17T04:46:37.880 回答