0

We need to write a currying method that does the same like the procedure below.

; Signature: c-bc(n)
; Type: [Number -> [Number -> Number]]
; Purpose: A naive Currying for binomial coefficient (n, k).
; Pre-conditions: n is a natural number
; Tests: ((c-bc 5) 3) => 10, ((c-bc 6) 2) => 15,
; ((c-bc 0) 0) => 1

(define c-bc
  (lambda (n)
    (lambda (k)
      (/ (fact n)
         (* (fact k)
            (fact (- n k)))))))

My solution:

(define c-bc
  (lambda (n)
    (let ((fact-n (fact n)))
      (lambda (k)
        (/ fact-n (* (fact k) (fact (- n k))))))))

Is it right? And how can I explain that this is currying?

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
roh
  • 123
  • 1
  • 1
  • 10
  • The original function is already currying. I'm not sure what your version is supposed to do differently. – C. K. Young Apr 11 '14 at 10:51
  • the first version is currying, but a naive one that compute (fact n) more times than the second solution. we expected to achieve partial evaluation goals. (as part from uni' course.) – roh Apr 11 '14 at 13:12

1 Answers1

1

Yes, it is right. Explain it as: "The binomial coefficient function takes two arguments, n and k. I've produced a curried function of n returning a function of r that computs the coefficient. The function of r is 'optimized' because a computation involving only n is lexically bound."

GoZoner
  • 67,920
  • 20
  • 95
  • 145