50 points · by arohner · 17y ago · 61 comments
(def entropy (x)
(- (sum [* (p _) (log:p _)] x)))
I like macros, but I wouldn't use them in this case. Summing is close enough to mapping that the form above is easy for any Lisp programmer to understand.I'd also argue that code size is a more meaningful measure of readability than familiarity. There's nothing intrinsically right about sigma notation. Some people are just more used to it. But since whatever notation you put in a language will eventually become familiar to its users, you should factor out familiarity when making design decisions, or at most use it to break ties. The notation above is honestly more readable to me, because it's what I'm used to.
> I'd also argue that code size is a more meaningful measure of readability than familiarity ... but since whatever notation you put in a language will eventually become familiar to its users, you should factor out familiarity when making design decisions
An exaggerated, uncharitable interpretation of what you said would be "users will figure out the notation, regardless of how hard it is, so make them jump through hoops so you can get a shorter notation". How do you distinguish between shorter notation that is a net win, and shorter notation that looks like perl?
My goal in using the macro was to reduce noise. Without the macro, the equivalent clojure would look like
(defn entropy [x]
(- (Σ #(* (p %) (log (p %)) x))
To me, the original version is easier to read because it lacks the #, % characters. Given that every call to sum would likely use an anonymous function (and therefore use #, %), it seemed worthwhile. Maybe that's an argument against clojure, but the arc version is almost as noisy, IMO.It's an interesting question whether there are any existing language constructs that force one to write unreadable code. I ended
http://www.paulgraham.com/power.html
by asking for examples, and I don't think I ever got any.
Depends. Suppose we added GoTo, ComeFrom, and assignment to the Haskell specification, to be implemented by all conforming implementations. The language has become strictly more powerful. But compiler writers will have a much harder time proving certain properties about your program e.g. constancy of variables. The implementation and optimisation techniques that depend on those properties no longer work. Thus the purely functional parts of your programmes will become slower, and you will be forced to switch to imperative style if you want any performance.
(To make the compiler writers job even more difficult, add some of Python's dynamic capabilities that make it so hard to compile. Plus an eval that works in the current environment on strings and can change arbitrary variables or cause other side-effects.)
Of course the language itself does not force you to write unreadable code. But it may make it harder for the implementors to allow you to write readable code.
How about TECO?
(Σ (fn [i] (* (p i) (log (p i)))) x)
The tradeoff with your macro version is that when you want to leverage Σ as a first-class function (say, with comp or partial), you will end up with #() or (fn..) in the calling code. You can't escape it -- it's a natural way to write functional Lisp code.It's no less readable than the rest of Clojure's API. You can reasonably expect a Clojure user to understand functions as arguments.
The hairier equations can juggle several similar variables and carry on for several lines of code. It isn’t always obvious how to break them apart into smaller functions like you would in a normal application. Using income literals instead of spelling variables out can save a lot of space. More importantly, it makes the code look more like the source material, which makes mistakes much easier to spot.
In idiomatic Python, with only basic language features needed:
-sum(p(x) * log(p(x)) for x in l)
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.
>>> 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')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.)Both look much cleaner imo.
I'd agree, except every mathematical definition I've seen used infix notation. Minus a function for summing a series, the C version at least keeps the symbols in the same order. Anyone with even a passing familiarity of C can read this and recognize it as the same thing.
double entropy(int x) {
double e = 0.0;
for (int i = 0; i < x; i++) e += p(i) * log(p(i));
return -e;
}Really don't do that - try to stick to ASCII in the source code please.
You simply define a keyboard shortcut.
For very common primitives, a special character can work nicely. We use λ in our Common Lisp code instead of typing lambda. Actually, our λ is a macro that does a couple of things besides expanding into lambda; if it were just the latter we probably wouldn't have gone there. Once we did, though, I was surprised at the improvement in readability.
I mean, sure, if it was my job to deal with your unicode code every day from here on out, I'd bother to learn how to map screwy symbols to keyboard shortcuts. But really, why would you do this to your code in the first place? It certainly doesn't make things more readable. Exactly the opposite.
Without bothering to understand why it was done that way or discussing it with the team? That wouldn't be a good way to last very long.
why would you do this to your code in the first place?
I was careful to include a phrase which explains this: for very common primitives. Perhaps your code doesn't have those; ours does.
It certainly doesn't make things more readable.
Considering that I just finished saying it did, that's quite the claim.
screwy symbols
Most programmers I've observed are closed-minded about deviations from their comfort zone. Faced with something that doesn't conform to the way they always do things (a.k.a. the Right Way), they reflexively condemn. Your comment is remarkable insofar as every single one of its sentences suggests this mentality. I hope that's just my mistaken impression.
: entropy ( n -- e ) [1,b] [ p dup log * ] sigma neg ;
Note that [1,b] is a word which makes a numeric range from an integer on the stack, and [ ... ] is a function object (quotation). #!/usr/bin/ruby -Ku
# encoding: utf-8
module Enumerable
def sum
inject { |acc, val| acc + val }
end
def Σ(&blk)
map(&blk).sum
end
def entropy(&prob)
- Σ { |i| prob[i] * Math.log(prob[i]) }
end
end
EDIT: And in 1.8 if you pass -Ku. entropy <- function(x) -sum(p(x)*log(p(x)))http://research.sun.com/projects/plrg/Publications/fortress....
(Image from the part of the above PDF that I was thinking of: http://skitch.com/joelf/bs99c/sun-research-fortress)
- +/ p * ^. p
where p is the array. you can even remove the duplication of the array with a so-called hook: - +/ (* ^.) pI don't grok this argument, when would one like to have that?
For me personally J is all about tacit functional programming. Any APL have this?
entropy x = - sum [p i * (log . p) i | i <- [1..x]]
consider bounds on a: 0 < a < 1; it's (and (< 0 a 1) (foo)) ... should be easy in any language
how about 0 < a < b < 1 ? it's (and (< 0 a b 1) (bar)) ... a little ugly in other languages
0 < a < b < ... < z < 1 ? i won't program that in other languages :p
Func<List<double>, double> entropy = (X) =>
{
double e = 0;
X.ForEach((p) => e += p * Math.Log(p));
return -e;
};
Compiler gets negative points for not being willing to infer the type-definition of the function-variable though. I guess that is stuff you have to live with in statically typed languages ;)Edit: Just checked. C# allows unicode characters like Σ for functions and variables, although I would never use it myself.
double entropy = -1.0d * mydata.Select(x => x * Math.Log10(x)).Sum();
Where 'mydata' is of type IEnumerable<double>.
See this similar post regarding type inference on fields: http://blogs.msdn.com/ericlippert/archive/2009/01/26/why-no-...
I'm sure there is an equally reasonable explanation of costs and implications for your example. Eric Lippert would probably be happy to share it with you, why not ask him? :-)
There sure is. Had I defined a method (like you suggested), and just gone ahead and made my function variable point to that, the compiler would have enough information to infer types. In that case I would get away with the following:
var someFunction = (X) => entropy(X);
Just felt like contributing somethings which isn't on the HN daily list of languages, that's all :) function entropy(x){ return sum(p(x)*log(p(x))); }However, some languages will achieve some things much easier. Your language apparently makes it pretty easy to create new functions such as sum or product.