[plt-scheme] How do I do this in true functional style?

From: Devlin Bentley (com2kid at gmail.com)
Date: Wed Dec 1 01:07:27 EST 2004

I was mentally going over how I was going to write a mini wireframe[1]
3d-renderer in Scheme, and right afterwards I went and skimmed through
parts of HTDP.  After finishing some reading in HTDP (and noticing
that set! is not introduced until Chapter 34), I began to feel sick to
my stomach with my current plan for doing my renderer.

The way I was going to do it up until about 10 minutes ago involved
taking a text file with comma separated integers and using each three
sequential integers as indices in a 3-dimensional array of booleans
when setting the read in index to true.

The actual drawing algorithm would then traverse the array (also an
icky hack that should not be done, nexted for loops in Scheme just
because they are easy for my brain to think of....) and use a "next in
line" function to jump between valid points.

Now it'd all work, and take me an hour or two tops to code, but the thing is:

- Lots of set-vector! statements being executed, tons of them.

- Hacked on nested iterative loops (I've already programmed this in
before, it looks horrible and feels "obviously wrong", not a recursive
algorithm at all!)

- I seriously get the feeling that my solution is best suited for a
more procedural language.  (Not surprising considering I originally
wrote it out in procedural psuedocode)

How am I approaching this wrong?  My instructor doesn't really care
how it gets done (The final "fun" assignment of the quarter is to draw
something, anything, in 3D) and no matter how I do end up doing it, it
will be fun having a little mini 3d room rendered on my screen, but I
will feel a lot better about it if I can have a true recursive (and if
possible functional!) solution.

Is this even possible given that I need to read in data from a text file?

Aside from converting the text file into one gigantic list and parsing
through it, is there any way for me to get around using a million and
one sets, or are situations like this considered times when it is
"acceptable" to use mutators?

No comments on my stupid choice of a data structure!  I know it is
stupid, but the sheer ease of coding a wireframe algorithm for points
in an array has lured me in!

[1] A lot easier than I at first suspected.
Devlin Bentley
Com2Kid at gmail.com

Posted on the users mailing list.