Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is an increment operator, like "++", possible?

490 views
Skip to first unread message

none Rouben Rostamian

unread,
Jan 19, 2013, 6:08:20 PM1/19/13
to
In many occasions I have wished that Maple had an increment operator
like C's "++" operator, or a function, say incr(), so that after doing:
x := 12;
incr(x);
the value of x would be 13 now.

Is there a way to implement such a thing within the scope of Maple?
It would be very handy when incrementing objects with long names,
e.g.,instead of:
Plant_Energy[i] := Plant_Energy[i] + 1;
we would be able to do:
incr(Plant_Energy[i]);

--
Rouben Rostamian

Joe Riel

unread,
Jan 19, 2013, 8:17:14 PM1/19/13
to
You could use

incr := proc(x :: evaln)
assign(x,eval(x)+1);
end proc:

--
Joe Riel

none Rouben Rostamian

unread,
Jan 20, 2013, 4:43:51 AM1/20/13
to
That's awesome. Thanks.

Motivated by this, I went a step further by defining
the following generalization:

oper := proc(x :: evaln, operator, d)
assign(x, `operator`(eval(x),d));

Now:
oper(x, `+`, 1); # increments x by 1,
oper(x, `-`, 4); # decrements x by 4,
oper(x, `*`, 3); # triples x
oper(x, `/`, 2); # halves x

Thanks again.

--
Rouben Rostamian
0 new messages