Carl Love

Carl Love

28150 Reputation

25 Badges

13 years, 351 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@janhardo S is both the sum and the return value. It must be initilized to 0.

This procedure does the same thing using a for-in-do loop:

SumList:= proc(L::list)
local S:= 0, x;
    for x in L do
        S:= S + x
    od;
    S
end proc:

An alternative formulation is

SumList:= (L::list(algebraic))-> 
    if L=[] then 0 else local S:= 0, x; for x in L do S:= S+x od fi
:

In this formulation, the return value is either 0 or the last S computed in the loop, so no S at the end is needed.

S:= S+x is equivalent to S+= x. This feature was added in Maple 2018 or Maple 2019, so you won't find it in your book.

If you're having trouble with the factorial, then do this exercise first: Write a procedure to find the product of a list of numbers.

@janhardo From closely reading your loop attempts, I see that you're really struggling with it. Here's a simple straightforward procedure with a loop to sum the elements of a list of arbitrary length:

SumList:= proc(L::list)
local S:= 0, i;
    for i to nops(L) do
        S:= S + L[i]
    od;
    S
end proc;

Please study this thoroughly and let me know if you have any questions.

@janhardo I think that I'll wait until you've learned the while clause of the do statement before I explain further. But note that it's not an until statement; the until part is a clause that terminates the preceding do statement.

In my opinion, Maple is not well suited to derive the displayed form from your typed form. If, however, you want to verify whether the two forms are equivalent, Maple is good at that.

I totally understand why you'd want to come up with contrived examples such as you have been as a means of experimentation. I do the same thing myself. So, don't take my Answers too harshly; I encourage you to experiment.

Pedagogic advice: You'll learn a lot if you try to remove from your examples everything that doesn't cause the anomaly. In all intellectual pursuits, there is an art to creating good examples.

@Carl Love In the future, I'll say implicitLY local rather than implicit local because the latter may be more likely to give the false impression that there are two types of locals with different semantics. (But note that English grammar requires that local be used as an adjective to use implicitly local; cases where local is used as a noun need to be reworded.)

@radaar In the example that you just posted, a is still local.

@radaar As I told you in another thread, there's no semantic difference between implicit and explicit locals, and there never has been. So I don't know how you acquired that "understanding". There's no good reason to ever ignore the warning about implicit locals. 

In the second procedure, a is local for the entire procedure. It does not suddenly become local at the point that the "compiler" realizes that it should be flagged as implicit local. If you look at the "compiled" procedure with showstat, you'll see local a at the top.

@mmcdara Yes, you could use define. Personally, I've never encountered a situation where I preferred using define.

@mmcdara put 

I mean that you can write e mod m; there's no need to put the operator first. If you do put the operator first, then it must be in quotes.

@mmcdara The first, or left, argument to mod can be almost anything, including a container. There's no need for you to use mod in prefix form, i.e., as `mod`.

@stefanv Thank you, Stefan. If something akin to what you wrote above were included in the help pages of seq, add, and mul, I'd consider the matter to be fully documented.

@radaar Thanks. I have read this book, and in my opinion it is by far the best book about Maple. Although most of this book is already incorporated into the Maple help pages (see ?ProgrammingGuide), it's much more convenient in PDF form.

@radaar What don't you understand about Kitonum's example? The f and g are considered "equal" at this point, although it's difficult to say precisely what "equal" means in a Maple context. From a mathematical viewpoint, they are unquestionably equal. From Maple's viewpoint, we have

evalb(eval(f) = eval(g));
             true

But

evalb(addressof(eval(f)) = addressof(eval(g)))
             false

That shows that they are "currently" equal, but they could become unequal if they acquire different remember tables. For example, this would make them unequal:

f(3):= 7;

All of this has nothing to do with the global assignments to x and t; indeed it would all be the same even if there had been no assignments to x and t.

@radaar Yes, all that you just said is correct.

First 196 197 198 199 200 201 202 Last Page 198 of 711