Re: what's it worth to write a short program for polynomial multiplication?



On Jun 16, 11:00 am, Dan Christensen <j...@xxxxxx> wrote:
[Dan] Here's the equivalent code:

c = collections.defaultdict(int)
for ea in a:
for eb in b:
c[ea+eb] += a[ea]*b[eb]

The only cryptic line is the first, which defines a hash table
whose default value is 0.

Dan

Dan's note reminded me that there are other (in my opinion, very non-
lispy) ways of writing loops in ANSI standard Common Lisp. I rarely
use them, but they make possible the following code..


(setf ans (make-hash-table))
(loop for ea being the hash-index of a using (hash-value c1) do
(loop for eb being the hash-index of b using (hash-value c2) do
(incf (gethash (+ ea eb) ans 0) (* c1 c2))))

.



Relevant Pages

  • Re: Noob! Help required and would be really appreciated!
    ... You've read 60% of "Learning Perl"? ... On finding the keyword "New Order", it has to start building a hash. ... want to build a loop around this hash creation. ... transferred to %allOrders. ...
    (comp.lang.perl.misc)
  • Re: multiline matching
    ... > City: Austin ... You never had more than one entry in the hash at a time. ... The for loop below is in the wrong spot. ... That loop fetches all the keys of the hash and then prints the Name, ...
    (perl.beginners)
  • Re: My hash table is in need of peer review
    ... struct hash { ... of the two possible loop exit conditions caused the loop to ... You might want to consider keeping the lists sorted ... same value pointer the caller hands you. ...
    (comp.lang.c)
  • Re: Loop over keys and values of a hashtable
    ... > that doesn't mean that accesses to a hash table are nondeterministic ... (loop for i being the hash-keys of foo using (hash-value j)) ... CL-USER> (time (loop for i being the hash-keys of foo ... ; Compiling LAMBDA NIL: ...
    (comp.lang.lisp)
  • Re: Should loops return a value?
    ... >return a different value depending on the context the loop is assigned in. ... >value being the same value put in the array. ... return a pairs of scalars and put them to hash instead of array. ... >provide a special syntax for that value. ...
    (perl.beginners)