[racket-users] How to define a function that can be used both in syntax transformers and ordinary code?

2021-05-09 Thread Yushuo Xiao
I am using syntax transformers to define macros in Racket. I want to create some helper functions to help me manipulate the syntax. However, the functions I defined outside the syntax transformer are not available inside the syntax transformer. For example, in the following code (define

RE: [racket-users] How to define a function that can be used both in syntax transformers and ordinary code?

2021-05-09 Thread Jacob Jozef
You can put your function in a module and require it both normally and for syntax.#lang racket (module my-function racket (provide my-function) (define (my-function x) (+ x 1))) (require 'my-function (for-syntax 'my-function)) (define-syntax my-macro  (lambda (stx)    (datum->syntax stx

RE: [racket-users] How to define a function that can be used both in syntax transformers and ordinary code?

2021-05-09 Thread Jacob Jozef
Another solution: #lang racket (define-syntax (def-both-phases stx) (syntax-case stx ()  ((_ rest ...) #'(begin (define rest ...)     (define-for-syntax rest ...) (def-both-phases (my-function x) (+ x 1)) (define-syntax my-macro  (lambda (stx)    (datum->syntax stx (my-function (cadr

[racket-users] macros in Racket repository

2021-05-09 Thread Tim Meehan
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! -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group

[racket-users] Racket - How to define a function that can be used both in syntax transformers and ordinary code?

2021-05-09 Thread Yushuo Xiao
I am using syntax transformers to define macros in Racket. I want to create some helper functions to help me manipulate the syntax. However, the functions I defined outside the syntax transformer are not available inside the syntax transformer. For example, in the following code (define

[racket-users] Re: Help in understanding 'letrec' example

2021-05-09 Thread kalime...@gmail.com
We are finding a file (or directory) with name "tarzan" inside all directories inside given path upto given depth. Recursion is needed here, because tarzan-near-top-of-tree? calls tarzan-in-directory? and tarzan-in-directory? calls tarzan-near-top-of-tree? for each file in given directory.

[racket-users] Re: Help in understanding 'letrec' example

2021-05-09 Thread Utkarsh Singh
Hi, > We are finding a file (or directory) with name "tarzan" inside all > directories inside given path upto given depth. > > Recursion is needed here, because tarzan-near-top-of-tree? calls > tarzan-in-directory? and tarzan-in-directory? calls > tarzan-near-top-of-tree? for each file in

Re: [racket-users] Injecting local contracts for prefab constructors

2021-05-09 Thread Ryan Culpepper
I'm not clear on what constraints you're working under with respect to modules, but hopefully you can adapt this to your needs. One option is to use a combination of `define-module-boundary-contract` (or `define/contract`) and `define-match-expander` to bind a name that can be used as a

Re: [racket-users] macros in Racket repository

2021-05-09 Thread Ryan Culpepper
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

Re: [racket-users] Racket - How to define a function that can be used both in syntax transformers and ordinary code?

2021-05-09 Thread Sage Gerard
Wow, I really butchered a sentence. Let me try it again. Old: "That is, there's a compile-time "pass" where Racket is focuses on the code you write and what they can see" New: "That is, there's a compile-time "pass" where Racket focuses on replacing used macros with new code, and that pass has

Re: [racket-users] Injecting local contracts for prefab constructors

2021-05-09 Thread Sage Gerard
Of course, if you're okay with a longer email. Before that, thank you both for volunteering your time to code something out. I enjoyed running into a `define-module-boundary-contract` in the wild for the first time. I sometimes print output in a (read)able form because I like analyzing my logs.

Re: [racket-users] Injecting local contracts for prefab constructors

2021-05-09 Thread Sage Gerard
Almost forgot, just in case someone asks: I want to avoid checking for invariant violations when I print. That would entail checking a bunch of values in accumulated program output, where it would be awkward to do something non-printing related, let alone raise an error. When I am printing

Re: [racket-users] Injecting local contracts for prefab constructors

2021-05-09 Thread Philip McGrath
Here's another minimally-tested sample implementation. A more robust solution might try to chaperone the struct type, as well, to protect reflective access to the constructor—but I wonder if that really makes sense when you are working with prefab structs. If you can explain more about your

[racket-users] Injecting local contracts for prefab constructors

2021-05-09 Thread Sage Gerard
I have a project with 57 prefab structure types. I need to construct instances using a local contract (module level contracts do not fit my needs here). Since I cannot define guards, the solution is easy enough. (struct foo (num) #:prefab) (define/contract make-foo (-> real? foo?) foo)

Re: [racket-users] Racket v8.1

2021-05-09 Thread 'John Clements' via Racket Users
Ah! My apologies. I’ve added your name to our “preferred names” file, and I hope not to make this mistake again. Thank you! John > On May 8, 2021, at 13:38, Dexter Lagan wrote: > > Hello sir, > > Thank you ! My name is actually Dexter Santucci. Apologies for the > confusion. My email

Re: [racket-users] macros in Racket repository

2021-05-09 Thread Jens Axel Søgaard
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

Re: [racket-users] macros in Racket repository

2021-05-09 Thread Tim Meehan
Thanks folks! > On May 9, 2021, at 09:38, Robby Findler wrote: > >  > 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] > [(_

Re: [racket-users] Racket - How to define a function that can be used both in syntax transformers and ordinary code?

2021-05-09 Thread Sage Gerard
I'm stretching details a bit, but maybe it would help to think of phases as "passes." That is, there's a compile-time "pass" where Racket is focuses on the code you write and what they can see. These passes continue until the Racket program is fully expanded. Where things get tricky is

Re: [racket-users] macros in Racket repository

2021-05-09 Thread Robby Findler
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