macros in Racket repository

31 views
Skip to first unread message

Tim Meehan

unread,
May 9, 2021, 9:26:09 AM5/9/21
to Racket Users
Where in the repository are macros like "and" and "or" defined?
I tried searching for "and" and "or" ... but you probably know how that worked out.

Thanks folks!

Ryan Culpepper

unread,
May 9, 2021, 10:13:11 AM5/9/21
to Tim Meehan, Racket Users
Here are the three most convenient ways I know of to find that information (which is "$RACKET/collects/racket/private/qq-and-or.rkt" in this specific case):

If you use DrRacket, then open a file that uses `and`, right-click on an occurrence of `and`, and choose "Open Defining File" (which changes to "Jump to Definition (in Other File)" once DrRacket opens the file.

If you use Emacs with racket-mode, go to an occurrence of `and` and hit "M-." (that is, hold down Meta/Alt and press the period key). You can also use "M-x racket-visit-definition". That opens the defining module and jumps to the definition.

If you have the `whereis` package installed, run the command `raco whereis -b racket/base and` and it will print the path of the defining file.

Ryan


--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CACgrOxK6S8EOAGk_rPbE%2B_wMLJiSbpwMhVd4AeRL8C9%2BDW3mgg%40mail.gmail.com.

Jens Axel Søgaard

unread,
May 9, 2021, 10:30:45 AM5/9/21
to ry...@racket-lang.org, Tim Meehan, Racket Users
Hi Tim,

In this case Ryan's method leads to:

https://github.com/racket/racket/blob/master/racket/collects/racket/private/qq-and-or.rkt#L440

But in case you are wondering about the style used in that file:
at the point where "qq-and-or.rkt" is used, none of the usual 
syntax tools (such as syntax-case and syntax-parse) are available,
so it is written using only primitive constructs.

That is, that particular file does not represent the usual style of macro writing.

/Jens Axel


Robby Findler

unread,
May 9, 2021, 10:38:03 AM5/9/21
to Jens Axel Søgaard, Ryan Culpepper, Tim Meehan, Racket Users
Here's one way to write it in modern Racket:

#lang racket
(require (for-syntax syntax/parse))

(module+ test (require rackunit))

(define-syntax (my-and stx)
  (syntax-parse stx
    [(_) #'#f]
    [(_ e:expr) #'e]
    [(_ e1:expr e2:expr ...)
     #'(if e1 (my-and e2 ...) #f)]))

(module+ test
  (check-equal? (my-and) #f)
  (check-equal? (my-and 1) 1)
  (check-equal? (my-and 1 2) 2)
  (check-equal? (my-and 1 #f 2) #f))



Tim Meehan

unread,
May 9, 2021, 1:40:46 PM5/9/21
to Robby Findler, Jens Axel Søgaard, Ryan Culpepper, Racket Users
Thanks folks!


On May 9, 2021, at 09:38, Robby Findler <ro...@cs.northwestern.edu> wrote:


Reply all
Reply to author
Forward
0 new messages