将参数传递给 Maxima CAS 中的函数
Passing a parameter to the function in Maxima CAS
我有 a program 我是 Maxima CAS:
kill(all);
remvalue(all);
GivePart(n):=(
[Part, iMax],
if (n>20) then iMax:10
else iMax : 250,
Part : makelist(i, i, 0, iMax) )$
GiveList(iMax):=(
[Part, PartList ],
PartList:[],
for i:1 thru iMax step 1 do (
Part: GivePart(i),
PartList : cons(Part, PartList)
),
PartList
)$
pp:GiveList(60)$
length(pp);
它创建了一个列表页。
pp的长度应该是60但是是21.
程序有 2 个函数和 iMax
- 第二个函数的参数
- 第一个函数中的局部变量
程序运行没有任何错误消息。
我查看了Maxima CAS的源代码
grep -wnR "iMax"
Maxima CAS 代码中未使用 iMax
我知道如何解决问题:更改第一个函数中局部变量的名称:
kill(all);
remvalue(all);
GivePart(n):=(
[Part, i_Max],
if (n>20) then i_Max:10
else i_Max : 250,
Part : makelist(i, i, 0, i_Max) )$
GiveList(iMax):=(
[Part, PartList ],
PartList:[],
for i:1 thru iMax step 1 do (
Part: GivePart(i),
PartList : cons(Part, PartList)
),
PartList
)$
pp:GiveList(60)$
length(pp);
现在pp的长度是60(好)。
问题的原因是什么?
问题似乎是
GivePart(n):=(
[Part, iMax],
哪个不对,应该是
GivePart(n):=block(
[Part, iMax],
在 block
之外,[Part, iMax]
不被识别为局部变量列表,并且 iMax
具有调用 GiveList
时绑定的值(这是 Maxima "dynamic scope" 政策的结果)。
我看到 GiveList
也有缺失的 block
需要更正。
我有 a program 我是 Maxima CAS:
kill(all);
remvalue(all);
GivePart(n):=(
[Part, iMax],
if (n>20) then iMax:10
else iMax : 250,
Part : makelist(i, i, 0, iMax) )$
GiveList(iMax):=(
[Part, PartList ],
PartList:[],
for i:1 thru iMax step 1 do (
Part: GivePart(i),
PartList : cons(Part, PartList)
),
PartList
)$
pp:GiveList(60)$
length(pp);
它创建了一个列表页。
pp的长度应该是60但是是21.
程序有 2 个函数和 iMax
- 第二个函数的参数
- 第一个函数中的局部变量
程序运行没有任何错误消息。
我查看了Maxima CAS的源代码
grep -wnR "iMax"
Maxima CAS 代码中未使用 iMax
我知道如何解决问题:更改第一个函数中局部变量的名称:
kill(all);
remvalue(all);
GivePart(n):=(
[Part, i_Max],
if (n>20) then i_Max:10
else i_Max : 250,
Part : makelist(i, i, 0, i_Max) )$
GiveList(iMax):=(
[Part, PartList ],
PartList:[],
for i:1 thru iMax step 1 do (
Part: GivePart(i),
PartList : cons(Part, PartList)
),
PartList
)$
pp:GiveList(60)$
length(pp);
现在pp的长度是60(好)。
问题的原因是什么?
问题似乎是
GivePart(n):=(
[Part, iMax],
哪个不对,应该是
GivePart(n):=block(
[Part, iMax],
在 block
之外,[Part, iMax]
不被识别为局部变量列表,并且 iMax
具有调用 GiveList
时绑定的值(这是 Maxima "dynamic scope" 政策的结果)。
我看到 GiveList
也有缺失的 block
需要更正。