[plt-scheme] help writing idiomatic plt scheme

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sun Jun 15 19:39:47 EDT 2008

On Jun 15, 2008, at 4:44 PM, Martin DeMello wrote:

> On Sun, Jun 15, 2008 at 10:29 AM, Matthias Felleisen
> <matthias at ccs.neu.edu> wrote:
>> 3. Here is my take on 'idiomatic' coming from a design perspective.
>> Everything is based on the data def file = eof | line * file
> Thanks - took me a while to wrap my head around this, since I'm one of
> those people who come from a loopy background :) I was trying to do
> the whole thing in one pass, rather than run filter twice on the list
> at the end, but I agree your version looks cleaner.

Over the years, I learned to factor such functions into distinct
"loops" [*] until I encounter data sets for which this approach
is too expensive. Then I "deforest" or "fuse the loops." (I have
been around compiler people for long enough to know how much effort
it is to compute such loop fusions automatically in Fortran and
how little this gains in average cases.)

-- Matthias

[*] In imperative languages, loops tend to come it just two
or three flavors (for, while, repeat). The problem is that
loops result type is VOID so the only way to communicate
results from the loop body to the outside is via SET!.

If you think about loops, though, they are traversals of
data structures PLUS an action applied to each node. FPLs
take this to an extreme: map, fold, andmap, ormap, for/fold,
etc. They separate traversal and action; you pass in the latter.
Better still, you get all kinds of return types so you don't
have to use SET!. And best, you can define your own LOOPs
because you get higher-order functions and TCO.

Posted on the users mailing list.