understand the implementation of "for/fold"

89 views
Skip to first unread message

Hemanth Kapila

unread,
Mar 26, 2021, 2:14:21 PMMar 26
to Racket Developers
Hi,

Apologies if this is wrong mailing list for the question.
I am trying to understand how  "for/fold" is implemented and I am finding it tough to grok for.rkt[1]
Can some one kindly provide me with some guidance on how to go about understanding the code in the collects folder?
I imagine this is too vague a request , if not off topic. I am trying to figure out what racket does when it encounters a `for/fold` in a program.


Many thanks,
Hemanth


Sam Tobin-Hochstadt

unread,
Mar 26, 2021, 2:34:58 PMMar 26
to Hemanth Kapila, Racket Developers
I think the best way to start understanding `for/fold` is to look at
the steps it takes in the macro stepper. That will give you a sense
for what intermediate macros are used, and what the shape of the
resulting code is. Once you understand that for some examples, it will
probably be easier to read the code in for.rkt.

Sam
> --
> 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 view this discussion on the web visit https://groups.google.com/d/msgid/racket-dev/5a6a734b-29ed-4f62-b7de-911eeac96f3dn%40googlegroups.com.

Stephen Chang

unread,
Mar 26, 2021, 2:49:47 PMMar 26
to Hemanth Kapila, Racket Developers
Hi Hemanth,

You can roughly think of it as a series of nested let loops and conditionals.

I had the same question once and you can see my attempt to
re-implement all the for/X forms (to integrate pattern matching) here:
https://github.com/stchang/generic-bind/blob/master/generic-bind/generic-bind.rkt#L581-L591
(Not sure if this will help or hurt your understanding :) )

Steve

Hemanth Kapila

unread,
Mar 26, 2021, 3:00:02 PMMar 26
to Stephen Chang, Racket Developers
Hi Steve,

Thanks! this looks great. I believe studying your code will help me
understand it a bit more. thanks a lot.

Hemanth Kapila

unread,
Mar 26, 2021, 3:04:38 PMMar 26
to Sam Tobin-Hochstadt, Racket Developers
Thanks Sam,

fwiw, I did try it via the expander. Perhaps I did not try it out with
the right examples. Or I gave up too soon.
I guess I need to do it a little bit more methodically.

Thanks for the advice
Hemanth
--
I drink I am thunk.
Reply all
Reply to author
Forward
0 new messages