September 20, 2013

For Versus Repeat

I have been teaching my 5-year-old to draw pictures with turtlebits, and I have concluded that the for statement is too difficult to teach as the first looping construct at that age. It would be fine as a second construct.

For example, he wrote this:

play 'abcdefg'

And he came to me saying "Dad, help me make it repeat 10 times." I told him to try to figure out a for statement to do that, but he could not figure it out. In CoffeeScript, the shortest way to do it would have been

for p in [1..10]
  play 'abcdefg'

But after we typed that, he clearly didn't understand what p was for. It is an unnecessary variable, and it is not the way you actually think about what is going on. It also has the disadvantage that the whole looping construct involves four pieces of punctuation: two brackets and two dots.

We can eliminate the p and reduce it to three pieces of punctuation if we define a 'repeat' function:

repeat 10, ->
  play 'abcdefg'

However, in the ideal world, repeat would just be a keyword in the language and he could write, punctuation-free:

repeat 10
  play 'abcdefg'

I am thinking that maybe a a couple of these types of pedagogical insights should be accumulated into a fork of Iced Coffeescript. Anybody interested in helping me build a Coffeescript variant?

Posted by David at September 20, 2013 08:32 AM
Comments

You can envision having the repeat syntax:
. repeat 10
.. play 'abcdefg'
evolve later to:
. repeat 10 with index
.. play 'abcdefg' + index

Posted by: Cezar at September 20, 2013 11:42 AM

(Iced)CoffeeScript supports this index-less variant of the for-loop:

for [1..10] then play 'abcdefg'

Posted by: Marc at November 29, 2013 04:16 AM

Whoa, thanks for pointing that out Marc!

That's a bit easier.

Posted by: David at November 29, 2013 10:02 AM
Post a comment









Remember personal info?