On 2011-12-16, arnuld <sun...@invalid.address> wrote:
>> On Wed, 14 Dec 2011 13:46:47 +0000, Tamas Papp wrote:
>> This [reduce-based summit function] has the extra advantage that it works for
>> all kinds of sequences:
>>
>> (summit #(1 nil 2 3)) ; 6
>
>> Graham's ACL follows the pedagogical tradition of emphasizing recursion,
>> especially tail recursion. It is certainly valuable to learn about
>> these things, and recursion can be a very nice idiom for some problems,
>> but I wonder if you would be better off learning from Seibel's Practical
>> Common Lisp book (you can find it on the net), which (IMO) teaches a
>> more idiomatic Common Lisp style.
>
> I thought recursion was CL's basic paradigm (ACL says its functional
> programming).
Note that REDUCE is still purely functional, and it appears in purely
functional languges, under different names like "fold", "left-fold",
"fold-left", "inject", etc, or denoted by some cryptic operator glyph.
It might not be implemented recursively, but you can't tell.
You don't have to write explicit recursion to do functional programming.
Recursion over basic building blocks like CAR and CDR is very low-level
functional programming.
In Common Lisp, you can throw away functional purity if it's inconvenient or
inefficient in the implementation, yet write a module which has a functional
API.
Uses of loop are not necessarily imperative. For instance:
(loop for x below 10 summing x)
This is purely functional, to me. The imperative-ness will reveal itself if
you create lexical closures in the loop body.
;; collect 10 functions that return x, then pass
;; through mapcar #'funcall to call them all
(mapcar #'funcall (loop for x below 10 collecting (lambda () x)))
-> (10 10 10 10 10 10 10 10 10 10)
Oops! This tells us something we already know: that there is actually only one
X. We made ten lexical closures in different iterations of the loop, but they
all captured the same X, which means that loop works by instantiating a single
variable X and incrementing it.
However, it's not obvious in many uses of loop.