In article <5ebe5a7d-cbdf-4d66-a816-a7d2a0a27...@40g2000prx.googlegroups.com>, Xah Lee <xah...@gmail.com> wrote:
> On Dec 10, 2:47 pm, John W Kennedy <jwke...@attglobal.net> wrote: > > Xah Lee wrote: > > > In lisp, python, perl, etc, you'll have 10 or so lines. In C or Java, > > > you'll have 50 or hundreds lines. > > > > C: > > > > #include <stdlib.h> > > #include <math.h> > > > > void normal(int dim, float* x, float* a) { > > float sum = 0.0f; > > int i; > > float divisor; > > for (i = 0; i < dim; ++i) sum += x[i] * x[i]; > > divisor = sqrt(sum); > > for (i = 0; i < dim; ++i) a[i] = x[i]/divisor; > > > > } > > > > Java: > > > > static float[] normal(final float[] x) { > > float sum = 0.0f; > > for (int i = 0; i < x.length; ++i) sum += x[i] * x[i]; > > final float divisor = (float) Math.sqrt(sum); > > float[] a = new float[x.length]; > > for (int i = 0; i < x.length; ++i) a[i] = x[i]/divisor; > > return a; > > > > } > > Thanks to various replies. > > I've now gather code solutions in ruby, python, C, Java, here: > > A Example of Mathematica's Expressiveness > http://xahlee.org/UnixResource_dir/writ/Mathematica_expressiveness.html > > now lacking is perl, elisp, which i can do well in a condensed way. > It'd be interesting also to have javascript... and perhaps erlang, > OCaml/F#, Haskell too. Perl: sub normal { my $sum = 0; $sum += $_ ** 2 for @_; my $length = sqrt($sum); return map { $_/$length } @_; } -- Jim Gibson -- http://mail.python.org/mailman/listinfo/python-list