Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

loop over list?

21 views
Skip to first unread message

Friedemann Groh

unread,
Oct 31, 2024, 2:59:02 AM10/31/24
to Macaulay2
Hi,

I'd appreciate if a loop counter could run in a list:

L = {1,4,8,10};
for i in L do( or list ) ( ... );

Is it very difficult to extend the M2-language in this way?

best
Friedemann

Mahrud Sayrafi

unread,
Oct 31, 2024, 5:49:50 AM10/31/24
to maca...@googlegroups.com
Hi Friedemann,

If I understand your question correctly, this is already possible:

i1 : for i in {1,2,3} do print i
1
2
3

i2 : for i in {1,2,3} list i

o2 = {1, 2, 3}

o2 : List


Mahrud

--
You received this message because you are subscribed to the Google Groups "Macaulay2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to macaulay2+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/macaulay2/b3fd7d9b-ad39-4f8e-bc81-6c8fdb64cef6n%40googlegroups.com.

Friedemann Groh

unread,
Oct 31, 2024, 7:57:38 AM10/31/24
to maca...@googlegroups.com

Thank you Mahrud, I'd better read the documentation.
best
Friedemann


Mahrud Sayrafi

unread,
Oct 31, 2024, 9:00:36 AM10/31/24
to maca...@googlegroups.com
Other fun things to try:

i1 : for pair in {0,0}..{1,1} do print pair
{0, 0}
{0, 1}
{1, 0}
{1, 1}

i3 : for i in 0 .. 5 list i

o3 = {0, 1, 2, 3, 4, 5}

i4 : for i in 0 ..< 5 list i

o4 = {0, 1, 2, 3, 4}


Friedemann Groh

unread,
Oct 31, 2024, 10:04:03 AM10/31/24
to maca...@googlegroups.com

I like the i1-example. At the moment I'm using Cartesian products of 1d-list instead. But your code is much better.
Thanks


Reply all
Reply to author
Forward
0 new messages