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



rjf <fateman@xxxxxxxxx> writes:

On Jun 15, 12:58 pm, Christopher Creutzig <christop...@xxxxxxxxxxx>
wrote:

c = Hash.new(0)
a.each {|ea,ca| b.each {|eb,cb| c[ea+eb] += ca*cb }}

Certainly not optimized for anything, maybe(!) except for code size. The
code would look virtually the same in almost any language that provides
hash tables, which certainly includes C++, Python, MuPAD, Maple, and I
would be very surprised if it did not include LISP, Axiom etc.

The equivalent program in lisp is
(defun pt (a b)
(let ((z (make-hash-table)))
(maphash (lambda(e c)
(maphash (lambda (e2 c2)
(incf (gethash (+ e e2) z 0) (* c c2))) a)) b) z))

While persons unused to Lisp may find there are "lots of parentheses",
persons used to Lisp may find it
distracting in Ruby or C that one must remember symbols such as {[]}
+=*. all with different precedence rules, syntactic parts, etc.

As Christopher pointed out, the code will be similar in all
languages with hash tables, but the readability does vary.
Python tends to avoid punctuation. 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
.



Relevant Pages

  • Re: whats it worth to write a short program for polynomial multiplication?
    ... than for pedagogical applications, ... code would look virtually the same in almost any language that provides ... hash tables, which certainly includes C++, Python, MuPAD, Maple, and I ... would be very surprised if it did not include LISP, ...
    (sci.math.symbolic)
  • Re: ILC2005: McCarthy denounces Common Lisp, "Lisp", XML, and Rahul
    ... >> the language should be available to users. ... In the design of Common Lisp, I asked Dave Moon (one of the architects ... Now, there are good kinds of low-level, like the way that floats are ...
    (comp.lang.lisp)
  • Re: CollabRx seeks brilliant engineers for an excellent e-science adventure
    ... belief that lisp programmers are smarter/better. ... Java or PHP programmers. ... a type of language that attracts a personality that meets my perceptions ...
    (comp.lang.lisp)
  • Re: Whats the best language to learn...
    ... on processors designed to run Lisp and Lisp OSes. ... byte-addressed memory, has native support for variable-sized value types, ... popular OO language, rather a language about like that of Delphi would have ... lisp, java, ruby, etc. ...
    (comp.programming)
  • Re: Program compression
    ... TM> supported by your favorite language (LISP) are good concepts. ... then call the Java compiler to compile that source file to a Class ... TM-STC> Since static type checking makes run-time type checks unnecessary, ...
    (comp.programming)