Skip to content

Do ​

Evaluates an expression repeatedly, for its side effects, and returns Nothing.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageDo✓
Do(body, n)Evaluates bodybody nn times, with no loop variable.
Do(body, {i, imax})Evaluates bodybody with ii running 1,2,…,imax1, 2, \dots, imax.
Do(body, {i, imin, imax})Evaluates bodybody with ii running imin,imin+1,…,imaximin, imin+1, \dots, imax.
Do(body, {i, imin, imax, di})Evaluates bodybody with ii running imin,imin+di,…imin, imin+di, \dots up to (or down to, for di<0di<0) imaximax.
Typed signatures
TypePackageApplies
(any, any) -> anycombinatoricsalways

Domain: Collections

Details
  • Always returns NothingNothing — this library's stand-in for Wolfram's NullNull (see the Nothing:NullNothing: Null entry in @enumeratio/wolfram's SYMBOLS map).
  • ii, when named, is a fresh local for the whole call, the same way Module's locals are — it does not leak, and does not clobber an outer binding of the same name.
  • Bounds and step are integers only; Wolfram additionally allows real-valued iterators.
  • The examples below place the DoDo call inside a ListList alongside its surrounding statements — the NothingNothing it returns simply vanishes from that ListList (compute-engine's own behavior for NothingNothing, matching Wolfram), which is why each result has one fewer entry than statements written.

Examples

Basic #

sum 1 through 5 with a named loop variable

live ↗
In[total:−0,Do(total:−total+k,[k,1,5]),total]\bigl\lbrack\mathrm{total}\coloneq0, \mathrm{Do}(\mathrm{total}\coloneq\mathrm{total}+k, \bigl\lbrack k, 1, 5\bigr\rbrack), \mathrm{total}\bigr\rbrackOut[0,15]\bigl\lbrack0, 15\bigr\rbrack

Do(body, n) repeats with no loop variable

live ↗
In[count:−0,Do(count:−count+1,4),count]\bigl\lbrack\mathrm{count}\coloneq0, \mathrm{Do}(\mathrm{count}\coloneq\mathrm{count}+1, 4), \mathrm{count}\bigr\rbrackOut[0,4]\bigl\lbrack0, 4\bigr\rbrack
Applications #
live ↗
InReap(Do(If(IsPrime(2n−1),Sow(n)),[n,100]))[2,1]\mathrm{Reap}(\mathrm{Do}(\mathrm{If}(\mathrm{IsPrime}(2^{n}-1), \mathrm{Sow}(n)), \bigl\lbrack n, 100\bigr\rbrack))[2, 1]Out[2,3,5,7,13,17,19,31,61,89]\bigl\lbrack2, 3, 5, 7, 13, 17, 19, 31, 61, 89\bigr\rbrack
About
Examples3
Checked againstWolfram Language 1 agree
CrosswalkWolfram Language

See also: While, Module