view 2-Io/day2-loops.io @ 101:1fae0cca1ef8

Reduce large maze to single width corridors This reduces the permutations for a x x x b x To one (two steps north) from four (two steps north; one east, two north, one west; one east, one north, one west, one north; and one north, one east, one north, one west). Longer corridors were worse! We would filter this in the "been here before via another path" but that's still a lot of lookups in lists, which is inefficient.
author IBBoard <dev@ibboard.co.uk>
date Sun, 14 Jul 2019 13:42:24 +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!