On Thu, Aug 21, 2008 at 11:28 PM, David Philp <[EMAIL PROTECTED]> wrote: > > > On 22/08/2008, at 3:34 PM, William Stein wrote: > >> >> On Thu, Aug 21, 2008 at 9:41 PM, Arnaud Bergeron >> <[EMAIL PROTECTED]> wrote: >>> >>>>>> data /. x_?(# < 0 &) -> 0 (this is perhaps not the killer example) >>>>> >>>>> What does that do? >>>> >>>> /. is the pattern replacement operator, _ is a placeholder pattern >>>> that matches anything, x_ gives this placeholder a name so you can >>>> use >>>> it later, ? filters the matches (in this case, everything matches) >>>> with the pure function # < 0&, which takes one argument and returns >>>> true if the argument is less than 0, and the -> 0 part says anything >>>> that made it through the filter gets set to 0. So >>>> >>>> In[40]:= data = {-1, 2, 3}; >>>> In[41]:= data /. x_?(# < 0 &) -> 0 >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> Just out of curiosity, what words do you say in your head >> when you read In[41]? > > Keep in mind that it is a Lisp-like expression. That means there has > to be some intracranial translation into lisp-like nested > expressions. If there is a flamewar to be had, it is probably about > the relative merits of Lisp-like languages. I am not at all
There's not going to be any flamewars in this thread. > interested in proving that Mathematica is better than language X, I am > only pointing out features that I think are worth studying. Many thanks for explaining this in detail below. It was a superb explanation. > > Start small: > > data /. x->y > means "data, replacing all occurrences of x with y". > > x_?( # < 0&) is a pattern somewhat like in ocaml. It reads like this > "any item x, such that when the test 'argument is less than zero' is > applied, returns True" > > So line 41 reads: > data, replacing all (/.) items (x_) that are less than zero (?(#<0&) ) > with (->) zero. > > More strictly, though, the -> indicates a rule, whose full form is: > Rule[ x_?(#<0&), 0] > > The "x" is strictly unnecessary, but it is simpler to understand. > > If you had more than one rule, you would read it differently: > data /. { a -> 2, b -> 3} > "data, transformed by replacing a with 2 and b with 3" > > Can one do the same with Arnaud's python version? > >> I would like to understand what >> people are thinking when they find an expression like that >> easy to read and appealing. > > It is neat because I can write it from left to right, and I think > about the object to be manipulated before the operations. > >> When I look at Arnaud's python version: >> >>> sage: data = [-1, 2, 3] >>> sage: [(0 if d < 0 else d) for d in data] >> >> I read "the list got from data where the negative >> entries are replaced by 0", or maybe more literally >> "the list made by taking each entry d of data and >> putting 0 if it is negative else putting d." > > The unappealing thing is the emphasis on "the list". No such > requirement in Mathematica: "data" could just as well be Cos[8 x]. > > snip. > >> I could certainly see a place in sage for something >> like this: >> >> sage: data = [-1, 2, 3] >> sage: data = ma_eval('data /. x_?(# < 0 &) -> 0') >> sage: data >> [0, 2, 3] >> >> where ma_eval is a function that evaluates a mathematica-style >> expression in the scope of the current Sage >> session, and returns the result as a Python object. >> It's something that could be totally ignored by most users, >> but something people who understand the power of >> Mathematica might find very useful. Anyway, that's why >> this thread is I think potentially interesting. > > I would be scared of getting sued into oblivion. I would have a > lawyer look at the Mathematica EULA before even thinking about it. > "WRI is the holder of ... including without limitation... structure, > sequence, organization, "look and feel", programming language and > compilation of command names." I hope one can't own such things but I > don't want a legal fight. If I were afraid, then Sage would be nowhere today. > Otherwise, I agree. A mathematica-like parser would be useful. > Though I'd be surprised if the concepts map across well enough for it > to be anything other than an ugly hack. Why? > > D > > > ================================== > David J Philp > Postdoctoral Fellow > National Centre for Epidemiology and Population Health > Building 62, cnr Mills Rd & Eggleston Rd > The Australian National University > Canberra ACT 0200 Australia > > T: +61 2 6125 8260 > F: +61 2 6125 0740 > M: 0423 535 397 > W: http://nceph.anu.edu.au/ > > CRICOS Provider #00120C > > > > > > > > -- William Stein Associate Professor of Mathematics University of Washington http://wstein.org --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---