Re: Re: declaring integers
- To: mathgroup at smc.vnet.net
- Subject: [mg13133] Re: [mg13063] Re: declaring integers
- From: David Withoff <withoff>
- Date: Tue, 7 Jul 1998 03:44:35 -0400
- Sender: owner-wri-mathgroup at wolfram.com
My concerns regarding your comments about Mathematica, variable types, and other programming languages, can probably best be focussed by recalling the following paragraphs from your original message: > I think the desire to "declare variable types" arises from an > underlying desire to program Mathematica in a procedural or functional > way. However, Mathematica is, at its heart, a pattern-matching system. > Whether that is better or worse than a functional or procedural style > depends on both the particular problem you are trying to solve, and on > your particular aesthetic approach to problem solving. > > Pattern matching is obviously better for some things, but, I think, > also obviously not better for *all* things. One has to watch out when > trying to use Mathematica in a way contrary to its design. For the > things I do, pattern matching is rarely what I want. I want to use > Mathematica for the numerical methods and special functions, which I > hope are written better in Mathematica than I could write myself. > However, numerical computation is only the last stage of what > Mathematica does when you type something in, so I often find its > behavior puzzling or frustrating. My first question is about the connection you mentioned between declaring variable types and whether or not Mathematica is considered a functional language, a procedural language, or something else. I don't understand what you mean by that. These seem like orthogonal issues to me. My second question regards your comments about frustrating or puzzling behaviors, or behaviors that are otherwise not what you want. I am not questioning that you may have had difficulties. What I am questioning is what I read as the implication that these difficulties are the result of some intrinsic characteristic of the Mathematica programming language. If that isn't what you meant, then you can ignore this question, although I would be curious to know what you did mean. Otherwise, I claim that any difficulties that you have had are due to other causes, such as aesthetic preferences, learning a new programming language, or switching from one language to another. For example, the following comparison: > Well, no. If a and b don't have defined values, nothing gets added. The > pattern is simply returned: > > In[1] := a + b > Out[1] = a + b > > I.e. nothing is really "added". You might say that this is what addition > "means" for things which are just symbols, but this misses my point, which > is that no "machine instruction for addition" is invoked. > > In Scheme, without any definitions > > (+ a b) --> error a not defined > > I.e. what happens is that Scheme *evaluates* all three subforms +, a, and > b. Well, + evaluates to a built-in procedure <do addition>, but the > evaluation of a fails because a doesn't have a value, so Scheme has to > stop. while it illustrates a difference between Scheme and Mathematica, does not show a difference that would make any program more difficult in Mathematica than it is in Scheme, unless you happen to be relying on this particular Scheme error message for debugging (which may be important) or for some other purpose. Any Scheme program that doesn't generate this sort of error will show equivalent behavior if it is translated into Mathematica, which makes Mathematica a superset of Scheme in this regard. Regarding > I believe the difference is something fundamental, and is > essential to what "apply" is used for in Scheme. > > > (apply + '(2 2)) > > > > in Scheme is equivalent to > > > > Apply[Plus, {2, 2}] > > > > in Mathematica. > > Yes, but one would never use quote in the argument to apply! (I assume > that you mean a forward quote ', and not a backquote, which as far as I > know isn't used for Scheme, but in Common Lisp, refers to a more elaborate > quoting that, in your example, gives the same thing.) I'm not sure why you brought this up, but since you did, no, I meant the quote that I entered, yes, it gives the same thing in this example, both quotes are used in Scheme (see for example "The Scheme Programming Language" by R. Kent Dybvig), and it is quite common to use quote in the argument of apply. In fact, the very first example of "apply" in the aforementioned reference uses a quote (and proper quoting would also make your "(apply + (2 2)) --> error" example work). Jumping ahead to > Here is a demonstration of what I found confusing > > ; example in Scheme > > (define g (lambda (first second) (list first second))) > > (define a 5) > (define b 3) > > (apply - (g a b)) --> 2 > > ; because (g a b) returns a list (5 3) > > (define g-reversed (lambda (first second) (list second first))) > > (apply - (g-reversed a b)) --> -2 > > ; because g-reversed returns a list (3 5) > > (* In Mathematica *) > > a = 5; b = 3; > > Apply[Minus, g[a,b]] (* oops, haven't defined g yet *) > > --> 2 (* well, I don't notice *) > > Apply[Minus, g-reversed[a,b]] --> 2 > > (* Hey, what's going on??? g and g-reversed give the same answer. > > OHHHH!!! I don't have a definition for g-reversed yet. *) > > g-reversed[first_,second_] := List[second,first]; > > Apply[Minus, g-reversed[a,b]] --> -2. this comparison just shows that the translation into Mathematica was done badly. First, the "-" procedure in Scheme translates into Subtract in Mathematica, not Minus. Second, while the Scheme example includes a definition for g, that input got dropped from the translation into Mathematica. Third, "g-reversed" isn't a valid identifier in Mathematica (or in most other programming languages). You have identified one legitimate complaint about Mathematica -- that it would be convenient for some purposes if Mathematica would warn you when you forget to define a procedure. This difference between Scheme and Mathematica doesn't affect programs that work, but the warning message can be a useful diagnostic tool for programs that don't work. Regarding patterns, not only are patterns not a fundamental part of Mathematica, you don't even need to use patterns at all in Mathematica if you don't want to. The translation of (define g (lambda (first second) (list first second))) into Mathematica is Se[g, Function[{first, second}, List[first, second]]] which doesn't use patterns any more (or less) than Scheme uses patterns. In Mathematica you could also use g[p_, q_] = {p, q} to define this procedure, which does use non-trivial patterns, but that just shows that Mathematica has functionality that Scheme doesn't have. If you don't want to use that functionality, you can program Mathematica in much the same way that you program Scheme. It is perfectly legitimate to prefer some language for aesthetic reasons, or because it somehow appeals to your way of thinking. If you like Scheme because of some subjective feature, I could readily accept that. If you want to argue that Scheme is objectively superior to Mathematica in some way, however, you have to do a lot better than that. In particular, are there any essential features of Scheme (other than generating errors for undefined procedures) that you can't find in Mathematica? Dave