[plt-scheme] What are the utilities of box ?

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Thu Sep 11 14:12:14 EDT 2008

On Thu, Sep 11, 2008 at 2:06 PM, Andre Mayers
<andre.mayers at usherbrooke.ca> wrote:
> Why sould I write
> ---
> (define x (box 3))
> (unbox x)
> ---
> instead of
> ---
> (define y 3)
> y

A box is useful as a first-class mutable reference.  If you pass y,
above, to a procedure, the procedure gets 3, not the actual name y --
the procedure can mutate its own internal variables, but not your y.
If you pass x, however, the procedure gets the box in x, and can use
set-box! to mutate its contents.  So if you want a procedure to be
able to mutate what you pass in to it, you might want to use a box.

-- 
Carl Eastlund


Posted on the users mailing list.