On Sat, Dec 31, 2016 at 3:13 AM, Marc Nieper-Wißkirchen <xxxxxx@gmail.com> wrote:
When these procedures are implemented with case-lambda (and when case-lambda is more than a mere macro keyword), it is not too hard for an implementation to dispatch on the number of arguments at compile/expansion time, rather than at evaluation time.

I really think case-lambda is the biggest wart in R7RS,
and needs to be discouraged at every opportunity.

1. it's more verbose for simple cases than other idioms
    for optional argument handling (DSSSL, let-optionals, etc.)
2. it's quadratic in code size for optional argument handling
3. it encourages different behavior based on the number of
    arguments, which is extremely confusing for anything
    other than the most common functions (+, /, etc.)
4. it's insufficient for meaningful variadic matching, such as
    dispatch closures, which could easily be handled by
    more general utilities such as match-lambda
5. it encourages implementors to prematurely optimize,
    treating it as a core form, complicating their ASTs and
    associated utilities and all compiler transformations

It's possible to provide a more general approach to
optimizing variadic arguments without forcing a specific,
limited syntax.  For example, using Chibi's plugin
optimization infrastructure:

$ chibi -mchibi.disasm -e'(disasm (lambda a (let ((x (if (pair? a) (car a) 0))) (+ x 1))))'
      -------------- 0x107627320
      LOCAL-REF 0
      TYPEP 6
      JUMP-UNLESS 27 L1
      LOCAL-REF 0
      CAR 
      JUMP 17 L2
L1:   PUSH 0
L2:   PUSH #<procedure #f>
          -------------- 0x1076272c0
          PUSH 1
          LOCAL-REF 0
          ADD 
          RET 
      TAIL-CALL 1
      RET 
$ chibi -mchibi.{disasm,optimize.rest} -e'(disasm (lambda a (let ((x (if (pair? a) (car a) 0))) (+ x 1))))'
      -------------- 0x1016e6f60
      PUSH #<undef>
      FCALL0 0x10172c860 "num-parameters"
      PUSH 0
      LT 
      JUMP-UNLESS 26 L1
      LOCAL-REF 1
      JUMP 17 L2
L1:   PUSH 0
L2:   LOCAL-SET -5
      PUSH 1
      LOCAL-REF -5
      ADD 
      RET 

With the optimization enabled, no rest list will be consed.
[Beware, this is just a proof-of-concept and not well tested,
but the principle is sound.]  These sorts of optimizations
make heavy use of pattern matching and AST fiddling,
which is more difficult if you have lots of different AST types
like `case-lambda', or `let' as I sometimes see,

-- 
Alex