[plt-scheme] v4 questions

From: Eli Barzilay (eli at barzilay.org)
Date: Mon Mar 31 00:57:42 EDT 2008

On Mar 30, Mark Engelberg wrote:
> You are right that if you separate operations from data in your
> code, and only expose a given interface, you can go through and make
> whatever changes you want without affecting users of your code.
> What I'm talking about is the ease of actually going through and
> making those changes.

And that is exactly something that depends on the difference between
the original data structures.  Like the fact that `cdr' doesn't make
much sense for a vector.


> Ideally, it should be easy to replace internal uses of a list with
> internal uses of something like a list that sacrifices a bit of
> speed on cons, first, and rest, in order to get faster
> random-access, for example.

but it's far from just a simple matter of tradeoffs.  It's not a
coincidence that lists are so structurally simple that htdp uses them
extensively, and barely touches vectors (which call for a completely
different kind of iterations).


> On Sun, Mar 30, 2008 at 8:08 PM, Eli Barzilay <eli at barzilay.org> wrote:
> >  > The designers of Scala and F# probably have similar tastes to me in
> >  > this regard, because these forms of laziness are offered by those
> >  > languages.
> >
> >  (Any pointers with interaction examples?)
> 
> Scala:
> lazy keyword examples given in
> http://www.scala-lang.org/docu/files/ScalaByExample.pdf, chapter 14.

I looked at that, and what I see is a way to delay evaluation of
expressions in classes -- and that's an easy exercise using structs in
mzscheme.  Imitating the first example in the chapter:

  (define-struct Employee (id name managerId Db manager team))
  (define (*make-Employee id name managerId Db)
    (make-Employee id name managerId DB
                   (delay (Db-get Db managerId))
                   (delay (Db-team id))))
  (define (*Employee-managerId e) (force (Employee-managerId e)))
  (define (*Employee-team e)      (force (Employee-team e)))
  (provide (rename-out [*make-Employee make-Employee]
                       [*Employee-managerId Employee-managerId]
                       [*Employee-team Employee-team]
                       ...others are provided as usual...))

And it's simple enough to make this into a macro, and even extending
define-struct to have a #:lazy keyword for some fields.  Note that
it's also easy to replace `delay' and `force' by a thunk and an
application to avoid caching, except that it's silly to have a field
for that, since the accessor can just do the job.

The thing is that there is no implicit force -- it's implicit only in
the interface to the class (or structs in the above), which I control.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.