Re: efficiently accumulating values



liyer.vijay@xxxxxxxxx writes:

liyer.vijay@xxxxxxxxx wrote:
[snip my own stuff]

Hi,

I found the reference in PCL (Peter, if you're reading this, thanks)
to VECTOR-PUSH-EXTEND which dramatically reduces time to 0.346
seconds. Here is the program:

(defun find-words (graph)
"Given GRAPH find all possible words."
(destructuring-bind (m n) (array-dimensions graph)
(loop with result = (make-array 0 :adjustable t :fill-pointer 0)
as i from 0 to (1- m)

I'd write:

:for i :from 0 :below m

Some would write:

for i below m

do (loop as j from 0 to (1- n)
do (loop as word in (get-words graph i j)
if (and (zerop i) (zerop j))
do (vector-push-extend word result)))
finally (return (coerce result '(or cons list))))))

list == (or nil cons) therefore (or cons list) == list

However, I'd still like to know if there's a better way, and since I
didn't mention it in my last post, any suggestions, comments,
criticisms on coding style, programming are greatly appreciated.

Now, what you seem to be doing in find-words, is jusd that:

(defun find-words (graph)
"Given GRAPH find all possible words."
(let ((words (get-words graph 0 0)))
(if words
(copy-list words)
'())))

Unless get-words is not a pure function. Do you have any side effect
in get-words?

--
__Pascal Bourguignon__ http://www.informatimago.com/

HEALTH WARNING: Care should be taken when lifting this product,
since its mass, and thus its weight, is dependent on its velocity
relative to the user.
.



Relevant Pages

  • Re: efficiently accumulating values
    ... (defun find-words (graph) ... "Given GRAPH find all possible words." ... If get-words has no side effect, then the loops are useless, since i ... (loop as word in (get-words graph i j) ...
    (comp.lang.lisp)
  • Re: efficiently accumulating values
    ... (defun find-words (graph) ... "Given GRAPH find all possible words." ... do (loop as word in (get-words graph i j) ...
    (comp.lang.lisp)
  • Re: efficiently accumulating values
    ... Here is the lisp I wrote when I fisrt came with my questions. ... (loop as line = (read-line stream nil nil) ... (defun find-words (graph) ... "Given GRAPH find all possible words." ...
    (comp.lang.lisp)
  • Re: efficiently accumulating values
    ... "Given GRAPH find all possible words." ... do (loop as word in (get-words graph i j) ... do (let ((old (aref graph i j))) ... acc (rec ni nj word acc) ...
    (comp.lang.lisp)
  • Re: efficiently accumulating values
    ... to VECTOR-PUSH-EXTEND which dramatically reduces time to 0.346 ... "Given GRAPH find all possible words." ... do (loop as word in (get-words graph i j) ... if (and (zerop i) ) ...
    (comp.lang.lisp)