M
Marko Rauhamaa
Chris Angelico said:I don't find it more readable to cast something as recursive; compare
these two tight loops:
(let find-divisor ((c 2))
(cond
((= c i)
(format #t "~S\n" i)
(display-primes (1+ count) (1+ i)))
((= (remainder i c) 0)
(display-primes count (1+ i)))
(else
(find-divisor (1+ c)))))))))
for ( factor = 2 ; factor <= i - 1 ; factor++ )
if ( i%factor == 0 ) break;
if ( factor == i )
{
printf("%d\n",i);
count--;
}
In the first one, you start doing something, and if you don't have a
termination point, you recurse - which means you have to name this
loop as a function. In the second, you simply iterate,
I implemented the loops in the scheme way. Recursion is how iteration is
done by the Believers. Traditional looping structures are available to
scheme, but if you felt the need for them, you might as well program in
Python.
On the other hand, I didn't look for the most elegant implementation
idiom but tried to translate the original rather mechanically--in good
and bad.
My view is definitely that the C version is WAY more readable than the
Scheme one.
Yes, scheme is an acquired taste. As is Python. My experienced bash/C
colleague was baffled by some Python idioms (not in my code, I might
add) that looked pretty clear to me.
Marko