3 ms·
The reduce code is just not very declarative. I have to inspect the code and i need to have a mental model of REDUCE (which is not trivial). What is b and what
by lispm 12y ago
The reduce code is just not very declarative. I have to inspect the code and i need to have a mental model of REDUCE (which is not trivial). What is b and what is a? I need a mental model of ITERATE too, but the declarative wording gives hints what it is supposed to do.
Code like above is simply less readable. There are lots of uses of REDUCE which are simple to understand. Code like above, less so - where I need to write a special purpose function.
Another problem is that you need to think about the corner cases:
(reduce (lambda (a b) (if (> (length b) (length a)) b a))
'())
-> Error
Also you can see that the REDUCE version is slower. Usually there will be a function call overhead - unless the compiler has some optimization for that in some optimize level. Also 'LENGTH' is called multiple times on the longest list so far. You could work around that in REDUCE ... In ITERATE this is not needed: it calls LENGTH only once per list item.
- ScottBurson 12y agoYou always need to think about the corner cases. What is the longest element in an empty list of lists? It might be better to get an error in such a case than to have an empty list returned, since that's not an element of the input list, in violation of one possible interpretation of the function's specification. > Code like above is simply less readable. I write code like this all the time, and have for 35 years. I think it's perfectly readable.