annotate 2-Io/day2-loops.io @ 77:7bab4843aec6

Add roulette with auto-restarting process (And black-magic auto-registering of atoms)
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Feb 2018 20:42:51 +0000
parents 86668d32e162
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 # loop() gives an infinite loop - let's not run that!
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
3 # While loops aren't too unfamiliar:
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 # while(condition, code)
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 # where calls can be put in a "block" with semicolons
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 i := 1
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 # Notice the lack of "++"
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 while(i <= 11, i println; i = i + 1); "This one goes up to 11" println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 "Or we could have put the line here" println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 # For loops are similarly simple:
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 # for(variable, min, max, code)
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 for(i, 1, 11, i println); "For this one also goes up to 11" println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 # Oddly (but necessarily) the increment is in the middle of that as an optional parameter
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 increment := 2
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 for(i, 1, 11, increment, i println); "For this one goes to 11 in 2s" println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 # This is bad in some cases, because Io accepts extra args without complaining
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 # BUT it'll fill in extra args first
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 # AND things have potentially unexpected return values (e.g. "11 println" returns 11)
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 # Although it's actually just returning *something* so that you can chain more methods
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 # and in many cases then "self" is the best return, e.g. "11 println println" prints twice
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 for (i, 1, 10, 2, i println, "extra arg gets ignored")
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 "Bad arg example" println
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 for (i, 1, 10, /* no increment arg, so command runs *and* becomes increment*/ i println, "extra arg becomes message")
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 # However, we apparently only see the last print! Seems odd and unexpected - unless we're taking it from the previous call?
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 for (j, 1, 10, /* no increment arg, so command runs *and* becomes increment*/ j println, "extra arg becomes message")
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 # Yep, this crashes out!
86668d32e162 Add Day 2 code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30