Carl Love

Carl Love

28055 Reputation

25 Badges

12 years, 357 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are replies submitted by Carl Love

@dharr If the issue that you raised was of concern, then I'd address it by defining a type for this:

TypeTools:-AddType(
    speclist,
    proc(e, T::type, n::nonnegint:= (), {distinct::truefalse:= false})
        e::list(T) and 
        (n<>() implies nops(e)=n) and
        (distinct implies (nops = nops@{op})(e))
    end proc
);

Then Rouben could define his procedure as 

P:= proc(Z::speclist(identical(a,b,c,d), 2, distinct)) end proc

and the error messages would be more reasonable.

 

There are sometimes programmatic reasons why you'd prefer an "invalid input" error over an error produced from within the procedure. One of those is for use with the overload command.

In addition, all that Acer said about tables also applies to Records because, like tables, they are mutable and have property last_name_eval.

@Rouben Rostamian  

Maple contains a whole sublanguage or algebra for type expressions. The parts of it that I used above are (is any single expression):

  1. If is a type, then e::T iff type(e,T).
  2. e::(T &under f) iff f(e)::T.
  3. If is a numeric constant (such as the 2 in my code), then e::C iff e=C.
  4. e::And(T1, T2) iff e::T1 and e::T2, and likewise for more types.
  5. e::[T1,T2] iff e::list and nops(e)=2 and e[1]::T1 and e[2]::T2, and likewise for any number of types.
  6. e::identical(a,b) iff e in {a,b}, and likewise for any number of expressions.
  7. e::list(T) iff e::list and andmap(type, e, T).

Andand, and andmap all follow the McCarthy evaluation rule: Operands are evaluated left to right (or in order), and the evaluation stops if a false occurs.

There is also a perhaps-more-familiar algebra of functional operators. The parts of it that I used above are (the () represents any argument sequence)

  1. {f,g}() {f(), g()}, and likewise for any number of operators.
  2. (f@g)() = f(g()), and likewise for any number of operators.
  3. [f,g]() = [f(), g()], and likewise for any number of operators.
  4. a &f b `&f`(a,b) (regardless of whether the latter has been formally defined).
  5. &f has higher precedence than @, so the parentheses are required in 2 &under (nops@{op}).

The most important help pages on this material are ?type,structure, ?evalapply, ?operators,precedence, ?&, and ?use (but only the part towards the end of Description where it talks about operators). Let me know if you have any questions.

@janhardo 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 computed in the loop, so no 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 seqadd, and mul, I'd consider the matter to be fully documented.

First 194 195 196 197 198 199 200 Last Page 196 of 709