Josh Tilles
Josh Tilles

Reputation: 1271

Is there any difference between using “MIT-style curried-procedure forms” and lambda-expressions passed to the `curry` function?

Given the code snippet that follows, is there any meaningful difference between example-func-A and example-func-B?

#lang racket/base

(require (only-in racket/function curry))

(define (((example-func-A x) y) z)
  (+ x y z))

(define example-func-B
  (curry
   (lambda (x y z)
     (+ x y z))))

Upvotes: 3

Views: 49

Answers (1)

Josh Tilles
Josh Tilles

Reputation: 1271

Yes, example-func-A (which uses the MIT-style curried-procedure syntax) is less flexible than example-func-B, in that it expects to only be called with a single argument at a time:

> (((example-func-A 4) 5) 6)
15
> (example-func-A 4 5 6)
example-func-A: arity mismatch;
 the expected number of arguments does not match the given number
  expected: 1
  given: 3
  arguments...:
   4
   5
   6
  context...:
   /opt/homebrew-cask/Caskroom/racket/6.4/Racket v6.4/collects/racket/private/misc.rkt:87:7

In contrast, example-func-B accommodates receiving multiple (or even zero!) arguments:

> (((example-func-B 4) 5) 6)
15
> (example-func-B 4 5 6)
15
> ((((example-func-B) 4)) 5 6)
15

(Presumably the flexibility of curry comes with a bit of a performance hit at runtime.)

Upvotes: 7

Related Questions