Nicodemus (nicodemusrat) wrote,
Nicodemus
nicodemusrat

  • Mood:

Programmer Language Discussions

When language wonks* compare their various languages, unlike other guys, it's all about being small. I found this exchange in a wiki discussion of higher-order functions:


To compute the inner product of two vectors:

a = [ ... ] # your vector here
b = [ ... ] # your other vector here
d = map( (lambda p, q: p * q), a, b ) # work out the products
e = reduce( (lambda x, y: x + y), d, 0 ) # sum them up

I love Python.




sum = a.zip(b).inject(0) { |s, (x,y)| s + x * y }

Yeah, and that's why I prefer Ruby.




innerProd = (sum .) . zipWith (*)

Or Haskell ;)




+/(a*b)

Or J. >:)




Now, I fully expect someone to come up with a three character solution for Matlab or something. But, if you compute lots of inner products (and hey, who doesn't!) you now know that J is a great language for you! ;)


*Language wonk is not a derisive term. No, really.
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 14 comments