[racket] a question of style, and one of performance

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Sun Jan 8 15:42:51 EST 2012

On Sun, Jan 8, 2012 at 3:23 PM, Jordan Schatz <jordan at noionlabs.com> wrote:
> This code runs, but I'm guessing that its not the "right way" to do it.
> (define (js-date [i (current-date)])
>  (let ([original-format (date-display-format)]
>        [return ((λ ()
>                     (date-display-format 'rfc2822)
>                     (date->string i #t)))])
>    (date-display-format original-format)
>    return))
> 1) In "some other language" using a function as the default value for an
> argument is inefficient and frowned upon. Is that the case in racket?

Hi Jordan,

Can you give an example of such a language?  I'm curious.

I'm not sure where the inefficiency would come from, unless computing
the default value expression's value is costly.

According to the documentation in:


with regards to "default-expr": "... if no such argument is provided,
the default-expr is evaluated to produce a value associated with id."

>From the reference docs, it sounds like that, unlike a language like
Python, the default value is evaluated for every use of the function,
rather than just once when the function's defined.  We can experiment
with this:

#lang racket
(define (test [x (begin
                   (printf "I'm evaluating at: ~a\n"
  (add1 x))

If you run test, you'll see that the default-expr is being evaluated
for each call to test that depends on the default argument.

If it is expensive to compute the default expression, we can bind a
value to a variable, and then refer to that variable instead for the
default expression.  That way, the expression is just name lookup,
which is constant-cost.  Functions are named values, so I don't think
they'd be particularly expensive to use as default expressions.

Posted on the users mailing list.