[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: pros and cons of abstraction

William James

5/7/2015 7:24:00 AM

> In an earlier example (p. 26), Gabriel considers the problem of
> MAPCARing a function to a list and calculating the list's length at
> the same time. The most abstract solution is also extremely
> inefficient:
>
> (let ((result (mapcar f list))
> (len (length list)))
> ...)
>
> Gabriel suggests (and then rejects) that one could replace this with
> optimized code, and then the programming environment could display the
> unoptimized version. This version "correctly respects the abstraction
> of both operations...but it incorrectly overemphasizes abstraction
> because the un-abstracted but common-pattern code is just fine:"
>
> (let ((length 0)
> (let ((result
> (mapcar
> #'(lambda (x)
> (incf length)
> (f x))
> list)))
> ...))
>
> Again, I'm not convinced. My guess is that Paul Graham would have
> written a MAP-WITH-LENGTH utility to abstract out this pattern. Even
> though the above pattern is instantly recognizable (even to a novice
> like me), I'd rather see a one-line call to MAP-WITH-LENGTH, if for no
> other reason than that it makes the code shorter. But maybe that's
> just because I've been brainwashed by Paul Graham's books.

Gauche Scheme:

(use gauche.collection :only (map-accum))
(map-accum (lambda (c len) (values (char-upcase c) (+ 1 len)))
0
'(#\a #\b #\c))

===>
(#\A #\B #\C)
3

--
How does the Swedish political elite respond to their largest cities breaking
down? By pretending there is no problem. The media elite shares the same
contempt for and fear of the common people, and has largely played along.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html
Arrogant and isolated, the elite treat the ordinary citizens of the empire as
slaves and potential enemies.
www.kolumbus.fi/aquilon/america-middle-class-and-the-end-of-growth.htm