Make range cooperate with for loops?

62 views
Skip to first unread message

Alexis King

unread,
Jan 3, 2017, 1:44:58 PM1/3/17
to dev
A question[1] was asked on Stack Overflow today that used `range` from racket/list in a for loop, then was baffled as to why it was so slow compared to a manually written loop using named let. To some extent, confusion of this sort is unavoidable, since it stems from a confusion about the difference between lists and sequences, but this seems like a reasonably common mistake to make. Is there any reason `range` cannot be adjusted to cooperate with for loops so that it gets compiled like `in-range`?

It seems like there would be two ways to do this: either make for loops recognize `range` like `in-range`, or make `range` a macro using define-sequence-syntax that just expands to the existing `range` procedure when used in an expression context. Both of these could easily be made backwards compatible, and it could only make things faster. Is there any technical or philosophical reason to not do that before I attempt it?

[1]: http://stackoverflow.com/q/41444129/465378

Robby Findler

unread,
Jan 3, 2017, 1:46:01 PM1/3/17
to Alexis King, dev
Go for it!!

Robby
> --
> You received this message because you are subscribed to the Google Groups "Racket Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to racket-dev+...@googlegroups.com.
> To post to this group, send email to racke...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-dev/4ED13FA1-45FC-4EAC-94EB-AFF15D91EAB0%40gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Andrew Kent

unread,
Jan 3, 2017, 8:51:44 PM1/3/17
to Racket Developers, d...@racket-lang.org
Is the beginner confusion then simply going to shift to "why is 'for' w/ 'range' fast but 'for' w/ 'other-list-function' is not?"

(Not trying to be negative -- I just wonder if it might just further delay people's realizing there are both functions and special syntactic forms for things like 'for', 'match', etc)

Robby Findler

unread,
Jan 3, 2017, 9:07:51 PM1/3/17
to Andrew Kent, Racket Developers, d...@racket-lang.org
I think that people coming from python will expect that `in-range`s
name is `range`. So why not meet their expectations?

Robby
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-dev+...@googlegroups.com.
> To post to this group, send email to racke...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-dev/76981dbd-f8a2-4a1b-9e9c-ba9ee4867662%40googlegroups.com.

Matthew Butterick

unread,
Jan 4, 2017, 5:17:52 PM1/4/17
to Robby Findler, d...@racket-lang.org

On Jan 3, 2017, at 6:07 PM, Robby Findler <ro...@eecs.northwestern.edu> wrote:

I think that people coming from python will expect that `in-range`s
name is `range`. So why not meet their expectations?

If you give a mouse a cookie ...


Though it does seem that exact nonnegative integers — which behave as sequences when used in the iterator position — should also expand to `in-range`.

(for/list ([i (in-range 5)])
          i)

(for/list ([i (range 5)])
          i)

(for/list ([i 5])
          i)

Gustavo Massaccesi

unread,
Jan 4, 2017, 5:48:14 PM1/4/17
to Matthew Butterick, Robby Findler, d...@racket-lang.org
I'm not sure that it's so easy. The transformation must ensure that
this program works correctly:

#lang racket
(define-syntax-rule (#%datum . x)
'(2 2 2))
(for ([i 5])
(displayln i))

Gustavo
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-dev+...@googlegroups.com.
> To post to this group, send email to racke...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-dev/BEC9DAE3-FAEB-4412-82AD-67EDAD6245E1%40mbtype.com.

Matthew Butterick

unread,
Jan 4, 2017, 9:59:47 PM1/4/17
to Gustavo Massaccesi, d...@racket-lang.org
> On Jan 4, 2017, at 2:47 PM, Gustavo Massaccesi <gus...@oma.org.ar> wrote:
>
> I'm not sure that it's so easy. The transformation must ensure that
> this program works correctly:


Oh, I didn't say it was easy. Macro mischief: always with us.

#lang racket
(define-syntax-rule (range x ...)
(reverse (for/list ([i (in-range x ...)])
i)))
(for ([i (range 5)])
(displayln i))

Reply all
Reply to author
Forward
0 new messages