guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: extending FEM enginering package with Guile


From: Keith Wright
Subject: Re: extending FEM enginering package with Guile
Date: Wed, 7 Jan 2004 23:58:25 -0500

> From: Mario Storti <address@hidden>
> 
> .... But there is one point which I find too
> hard, and is the looping constructs. I think that explaining to the
> users concepts like tail-call recursion, 

Don't loop, recursion is easier.  Don't believe it?  Look in
your math books.  You may find recursion, often called induction,
but mathemeticians never loop.  They don't have to, so they do
it the easy way.

> or call/cc's is an overkill.

Definitely stay away from call/cc.  It _is_ hard, and the Guile
implementation may be buggy.  (Was in version 4, and nobody
knew how to fix it.  I haven't tested version 6 for this.)

> I'm not speculating here, I have talked with many of them
> (those with better programming skills).

You asked if they could understand recursion, and they
said "No"?

> Perhaps such things may be used by the developers, but definitely
> not by the average user.

Everybody uses recursion in Scheme.

> There is the `while' special form, but even with this some simple
> looping constructs, like the following in C/C++
> 
> while (cond0) {
>       // body1 0;
>       // ...
>       if (cond1) break;
>       // body 1
>       // ...
>       if (cond2) continue;
>       // body 2
>       // ...      
> }
> 
> are hard to write, due to the lack of `break' and `continue' in
> Scheme.

Don't use while in the first place.  Use "named LET".  Then BREAK
is just computing the final answer, and "continue" is calling 
the named LET procedure.

> I think that one can do that with catch/throw but it also seems
> an overkill.

And ugly and stupid.  Besides, Scheme doesn't have catch/throw
(it's added in Guile and maybe some other implementations).

> I found some flame wars between some other Lisp dialects and Scheme
> and I anticipate that I'm not interested in starting such a
> discussion. So:
> 
> Question 1: I ask simply if there is some simple way of writing code
> like this previous one in Scheme. 

(let loop ([var1 init1]
           [var2 init2])
  (if (cond1) (compute answer))
  ; ...
  (if (cond2) (loop (new-var1) (new-var2)))
  ; ...
  (loop (new-var1) (new-var2))  ; end-of-loop = continue
)

> Question 2: I have learned that in Scheme it is considered bad
> practice to `set!' variables, or in general to use side-effects. In
> our applications we manipulate huge structures (mainly vectors, sparse
> matrices, graphs, with sizes in the order of several hundredths MBs,
> and running for weeks), and it seems much more efficient to code this way
> (using side-effects). Am I right?

It depends on what the side effects are.  If, in the middle of
inverting a matrix, you start dorking around with the values of
unrelated global variables you are probably messing up.

Just compute your inverse (or whatever) and return it as the answer.
Do not find or make some global variable to SET! to the answer.

On the other hand, to compute your final answer you may want
to allocate a big vector (matrix, graph) full of zeroes (say)
as a local variable and compute the result by repeatedly
updating elements.  When your procedure returns the answer
the local variables disappear, only the result is returned,
and nobody need know about the VECTOR-SET! you did inside
your procedure.

     -- Keith




reply via email to

[Prev in Thread] Current Thread [Next in Thread]