在Maxima中对范围和集合进行惯用的迭代

huangapple go评论42阅读模式
英文:

idiomatic iteration over range and set in maxima

问题

maxima中实现等效的R表达式的习惯用法是:

for i: inlist(1,2,3,4,5,29,155) do display(i);
英文:

beginner's question. what is the idiomatic expression in maxima to accomplish the equivalent R expression

for (i in c(1:5,29,155)) message(i)
1,2,3,4,5,29,155

答案1

得分: 1

如果您想对列表中的每个值调用函数并不关心返回值(即,您对打印等副作用感兴趣),您可以这样说:

for x in mylist do print(x);

如果您想获取每个元素的返回值,例如 f

makelist(f(x), x, mylist);

Maxima 没有内置的范围操作符,因此类似 1:5 的操作需要略微冗长,可以使用 makelist(i, i, 1, 5)

此外,在 R 中,c(c(c(...), ...), ...) 会有效地折叠任何嵌套的向量,而 Maxima 允许嵌套列表保持不变(flatten 可以展平嵌套列表)。在给定的示例中,尝试 append(mypartiallist, [29, 155]),其中 mypartiallist 是先前 makelist 中的内容。

英文:

If you want to call a function for each value in a list and you don't care about the return value (i.e., you're interested a side effect such as printing), you can say:

for x in mylist do print(x);

If you want the return value of, say, f, for each element,

makelist (f(x), x, mylist);

Maxima doesn't have a built-in range operator, so something like 1:5 is the slightly more verbose makelist(i, i, 1, 5).

Also, while in R c(c(c(...), ...), ...) effectively collapses any nested vectors, Maxima lets nested lists stand (flatten flattens nested lists). In the example given, try append(mypartiallist, [29, 155]) where mypartiallist is the stuff from the previous makelist.

huangapple
  • 本文由 发表于 2023年4月11日 10:03:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定