5 ms·
Well, I certainly prefer it spelled "sum" -- trivial to understand and viewable with any font. In idiomatic Python, with only basic language features needed:
by Sharlin 17y ago
Well, I certainly prefer it spelled "sum" -- trivial to understand and viewable with any font.
In idiomatic Python, with only basic language features needed:
-sum(p(x) * log(p(x)) for x in l)
- deleted 17y ago[deleted]
- brent 17y agoIn matlab: -sum(p(x).*log(p(x))) Both look much cleaner imo.
- DanielStraight 17y agoYes. Why should something which is NOT written in prefix notation in math be written in prefix notation in code? What is so impressive about that? All of my programming languages (who has just one?) can define nice looking functions. None of them use obscure prefix notation, and I think that's a good thing.
- eru 17y agoWhat's so obscure about prefix notation? Having to deal with state and order of computation is a much bigger headache when converting from math to code than fixity.
- silentbicycle 17y agoFor all the talk of how Lisp is so well suited to making minilanguages for expressing things own their own terms, the lack of infix arithmetic seems particularly jarring. I think that infix (Lisps), postfix (Forth), or all-left OR all-right associative (as in Smalltalk and J, respectively) are better choices than infix with order-of-operations (which is just historical cruft, IMHO), but making basic arithmetic awkward is an easy way to give people a bad first impression of a language. In OCaml, which has order-of-operations arithmetic syntax, people instead get mad because it uses different ops (*. +. etc) for floating point math. You can't win.
- eru 17y agoHaskell has solved these problems quite nicely for me. You can use infix or prefix. And you can make any operator prefix by enclosing it in parens. The other way round, you have to backtick your function to make it infix. And it uses typeclasses to (mostly) allow the same operators for all numeric types. > (+) 1 2 > 1 `add` 2
- silentbicycle 17y agoThere's a way to set the infix associativity and precedence, isn't there? (I'm rusty at Haskell, it's not my thing.) You can do typeclass-like overloading in OCaml via its module system, but it isn't really integrated into basic arithmetic, which probably would have been the single most useful place for it. (I just got used to it, but it's definitely a wart.)
- eru 17y ago> There's a way to set the infix associativity and precedence, isn't there? Yes, at least for your new operators like , ++, ==> etc.
- dreish 17y agoI think the point was that author was able to create a syntax for summation that had approximately the same shape as the math notation. I don't think I would argue that this is a very good example of macros (and yes, I wrote that before reading pg's comment ;), but solutions people have posted with other languages all have foreign symbols in them -- syntax for passing around blocks, or language-defined looping or list comprehension syntax, not ones they have chosen to suit their own needs. In this case, that's a virtue; you learn the language's syntax, and then you know how to recognize summation within any program in that language regardless of who wrote it, but in cases where the problem is more complicated and the native syntax for what you need to do more awkward, you really do want to create your own syntax. Arguably that's a danger inherent in Lisp: it gives more opportunities to create needless new syntax, not just the kind of syntax that makes a program clearer and simpler. If you wanted to extend destructuring in Python (that is, (a, b) = (1, 2)) to cover dictionaries, how would you do it? In a Lisp, it's done with macros. The core language usually doesn't start out with any destructuring at all. In Python, you can beg Guido and wait, or fork the project -- neither option is practical.
- tome 17y agoThat facility's already there. Any object is unpacked using its __iter__: >>> d = { 'key1': 'val1', 'key2': 'val2' } >>> (x,y) = d >>> (x,y) ('key2', 'key1') >>> (x,y) = d.items() >>> (x,y) (('key2', 'val2'), ('key1', 'val1')) >>> (x,y) = d.values() >>> (x,y) ('val2', 'val1')
- dreish 17y agoSorry, I should have given an example. I don't see how the above is ever likely to be useful, since the keys can be in any arbitrary order, but the fact that you can get something sequential out of a dictionary and revert back to tuple assignment is trivial. What I'd like to see you do in Python is this: >>> d = { 'key1': 'val1', 'key2': 'val2', 'key3': 'val3' } >>> { 'key1': x, 'key3': y } = d >>> (x,y) ('val1', 'val3') Nothing like this is built into the handful of special forms that Clojure starts out with, but roughly halfway through core.clj, it gets added to the language using the same macro facilities available to any Clojure programmer. If Python manages to be as powerful a language as Clojure without macros, you should have no trouble at all. user=> (def d {:key1 "val1", :key2 "val2", :key3 "val3"}) #'user/d user=> (let [{x :key1, y :key3} d] [x y]) ["val1" "val3"] (The order of key-value pairs is reversed to support :as, :or, and :keys, which let you do additional tricks while destructuring.)