Arc Forumnew | comments | leaders | submitlogin
2 questions about for
2 points by skenney26 5814 days ago | 5 comments
I'm looking for help with understanding the definition of for:

1) Why is it necessary to initialize v to nil? (afterward, it immediately gets set to the value of init)

2) Why is it necessary to create a gensym for init? (it only gets called once)



2 points by almkglor 5814 days ago | link

Probably some fuzzy thinking. 'for uses 'loop, and presumably pg was thinking that 'loop might change its characteristics (i.e. it might execute the init part more than once) in the future.

Really, there's a reason why a formal language spec (!= code) is useful...

-----

1 point by absz 5814 days ago | link

You need to initialize v to nil so that it exists. The with binding creates a v that can be set. But you're right, there doesn't seem to be a reason you need the gensym.

-----

2 points by skenney26 5814 days ago | link

Thanks for the help absz. I'm creating a variety of iterators for a graphics app and I just want to make sure they're being written correctly.

I still don't quite understand why v needs to be initialized. For example, the following definition seems to work fine:

  (mac for (v init max . body)
    (w/uniq gm
     `(let ,gm (+ ,max 1)
        (loop (set ,v ,init) (< ,v ,gm) (set ,v (+ ,v 1))
          ,@body))))

-----

2 points by absz 5814 days ago | link

The problem is that if you define it this way, then your v is no longer lexical; instead, it's in the global namespace. Thus, writing (for ref 0 n (frob ref)) would overwrite the global ref procedure, which is not what you want.

-----

1 point by skenney26 5814 days ago | link

Ah, it finally makes sense. Thanks again.

-----