News for hackers. Stuff that matters.
Re:Can your programming language do this? by trezor on Wednesday July 08, 2009 @05:52PM (#694782)
C#:

    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.

Re:Can your programming language do this? by profquail on Wednesday July 08, 2009 @08:00PM (#694931)
Or you could just do this:

double entropy = -1.0d * mydata.Select(x => x * Math.Log10(x)).Sum();

Where 'mydata' is of type IEnumerable<double>.

Re:Can your programming language do this? by trezor on Thursday July 09, 2009 @02:07AM (#695329)
I knew there was a simple and concise syntax I was missing. Writing code after midnight is definitely a nono :)
Re:Can your programming language do this? by snprbob86 on Wednesday July 08, 2009 @06:03PM (#694799)
Not sure why you are using lambda syntax instead of defining a method shrug, but anyway I don't think the type inference shortcoming is inherent to static typing.

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? :-)

Re:Can your programming language do this? by scott_s on Wednesday July 08, 2009 @06:08PM (#694811)
You're right, statically typed languages can use type inferencing; both Haskell and Ocaml do.
Re:Can your programming language do this? by trezor on Wednesday July 08, 2009 @06:12PM (#694828)
I'm sure there is an equally reasonable explanation of costs and implications for your example.

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 :)