Hacker News new | past | comments | ask | show | jobs | submit login

the spec of the underlying language would be very simple. it's basically just repeated pattern matching. beyond that, knowing where to draw the line would be a bit weird. for example, does the function TextRecognize [1] need to be specified?

and by the way, Mathematica's documentation is enormous so i don't think a single spec would be necessary. maybe a few functions here or there would need some clarification in certain edge cases, but those would be higher-level functions like Plot

[1] http://reference.wolfram.com/mathematica/ref/TextRecognize.h...




Wrong. Just check the core language. It large and there is much more than repeated 'pattern matching'.


right, but in those cases you can just go to the documentation, which is pretty clear in the various ways you can use functions. for example: https://reference.wolfram.com/language/ref/Map.html


does it provide closures?


yes, using Module primarily. here's one way you might make a basic object-like thing (though i would generally advise against OO stuff in Mathematica. part of the power of the language is due to how it keeps everything straightforwardly manipulable):

    makeObj[] := Module[{obj, i = 0},
     obj["inc"] := ++i;
     obj["dec"] := --i;
     obj[] := i;
     obj];
precisely speaking, however, Module simply renames symbols to make them unique. the effect is the same as a closure, but it isn't a "pure" closure like you would find in other functional languages, because you can do for example Names["i*"] and find that the supposedly-private 'i' has the name i$2656 and you can alter its value that way

another point is that you could, if you wanted to, create your own version of Module and other scoping stuff, since it's a matter of making a macro by using the HoldAll atttribute [1] and using 'ReplaceAll' to replace specific variables or arbitrary structures

[1] http://reference.wolfram.com/mathematica/ref/HoldAll.html


are functions being passed/returned as closures?


if you're asking whether functions in Mathematica are first-class values, then yes. everything in Mathematica is first-class. functions are just symbols that have associated pattern-matching rules in the form f[___], otherwise they are the same as any other symbol.

you can do, for example:

    RandomChoice[{Plus, Times}][2, 3]
where the result will be either 5 or 6. in more explicit form:

    If[RandomReal[] < .5, Plus, Times][2, 3]
example of an "anonymous" function:

    (#1^2 + #2^2 &)[2, 3]
square of all numbers from 1 to 100:

    #^2 & /@ Range[100]
simpler form:

    Range[100]^2
all pairwise products of the first seven prime numbers (among themselves):

    Times @@@ Tuples[Prime /@ Range[7], 2]
etc.


I'm asking if first class functions also can be closures. Does it use lexical binding?


i think i answered that question already, which is yes, but technically no. here is a rough Javascript version of the original example:

    makeObj[] := Module[{obj, i = 0},
     obj["inc"] := ++i;
     obj["dec"] := --i;
     obj[] := i;
     obj];


    makeObj = function() {
        var i = 0;
        
        var obj = function(in) {
            switch(in) {
                case "inc" : return ++i;
                case "dec" : return --i;
                case undefined: return i;
            };
        };

        return obj;
    };




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: