例如,假设我想编程一个proc加1,所有结果都是我程序中的实数。有没有办法做到这一点?
答案 0 :(得分:1)
您可以使用anames('user')
获取顶级的用户定义变量,然后选择那些具有实数值的变量。在下面的代码中,调用incrementReals()
将增加所有这些变量:
restart;
a, b, c := 3, 1 - I, 0.5;
# Procedure to increment by 1 all user-defined variables that are of type 'realcons'.
incrementReals := proc()
local A := select( u -> type( eval(u), 'realcons' ), [ anames( 'user' ) ] ):
local B := map( u -> eval(u) + 1, A ):
assign( A =~ B );
return NULL:
end proc:
# Increment.
incrementReals();
# Confirm that a and b were incremented, but b was not.
'a' = a, 'b' = b, 'c' = c; # a = 4, b = 1 - I, c = 1.5