6 ms·
In matlab: -sum(p(x).*log(p(x))) Both look much cleaner imo.
by brent 17y ago
In 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.