Mercurial > repos > other > SevenLanguagesInSevenWeeks
view 2-Io/day2-loops.io @ 103:98be775c533c default tip
An odd "non-determinism" example from StackOverflow
It is clever, but doesn't make much sense as to how it gets its results
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 14 Jul 2019 13:44:13 +0100 |
parents | 86668d32e162 |
children |
line wrap: on
line source
# loop() gives an infinite loop - let's not run that! # While loops aren't too unfamiliar: # while(condition, code) # where calls can be put in a "block" with semicolons i := 1 # Notice the lack of "++" while(i <= 11, i println; i = i + 1); "This one goes up to 11" println "Or we could have put the line here" println # For loops are similarly simple: # for(variable, min, max, code) for(i, 1, 11, i println); "For this one also goes up to 11" println # Oddly (but necessarily) the increment is in the middle of that as an optional parameter increment := 2 for(i, 1, 11, increment, i println); "For this one goes to 11 in 2s" println # This is bad in some cases, because Io accepts extra args without complaining # BUT it'll fill in extra args first # AND things have potentially unexpected return values (e.g. "11 println" returns 11) # Although it's actually just returning *something* so that you can chain more methods # and in many cases then "self" is the best return, e.g. "11 println println" prints twice for (i, 1, 10, 2, i println, "extra arg gets ignored") "Bad arg example" println for (i, 1, 10, /* no increment arg, so command runs *and* becomes increment*/ i println, "extra arg becomes message") # However, we apparently only see the last print! Seems odd and unexpected - unless we're taking it from the previous call? for (j, 1, 10, /* no increment arg, so command runs *and* becomes increment*/ j println, "extra arg becomes message") # Yep, this crashes out!