dvanhorn
2021-2-17 15:06:44

scribble/example with escape: I stumble on this issue every few months it seems. The behavior of #:escape with examples seems very strange to me. I start by wanting to compute something to be included in example code. First, I try the default escape identifier, unsyntax and write something like: (examples (add1 #,(sub1 3))) which results in a perplexing “unsyntax: illegal outside of quasisyntax”.

After struggling around for a while trying out different identifiers, I realize that there’s a kind of pun going on which is what’s causing the problem. The example is escaping for what’s shown, but not what’s evaluated, hence the unsyntax error. But I don’t see how to write something that escapes both what is rendered and what is evaluated so that the above shows Examples: > (add1 2) 3 Am I missing something?


mflatt
2021-2-17 15:14:25

I don’t think there’s a way to escape in the evaluation construction, but you can use eval:alts to do something different in the two cases, such as @(examples (eval:alts (add1 #,(sub1 3)) (add1 (sub1 3)))) In that example, (sub1 3) is not escaped for evaluation.


dvanhorn
2021-2-17 15:16:57

Thanks, yes, this is the solution I usually settle on, but it’s painful when the add1 is replaced by something substantial which is duplicated.


mflatt
2021-2-17 15:26:09

Adding some option to examples to make unsyntax escape might make sense. Right now, the evaluation treatment boils down to quote via base-quote-expr in scribble/eval.


dvanhorn
2021-2-17 15:30:03

a thought that occurred to me just now and works in my particular use case is to use #:escape identity lol.


mflatt
2021-2-17 15:43:15

I’ve added #:callback-exns? to _fun. The intent for your example is that you’d add #:callback-exns? #t to the _fun for entry.


jestarray
2021-2-17 18:13:30

so im trying to write to a file and (writeln (vector 1 2 3) out) writes #(1 2 3) but what I really want is something like: #( 1 2 3 ) currently im doing something like: (map (lambda (item) (writeln item out)) (vector 1 2 3)) (define final (string-append "#(" (get-output-string out) ")"))) (displayln final out) ;feels weird mixing wirteln and displayln to hack around it. Is there an easier/better way or is this it?


dvanhorn
2021-2-17 18:54:30

I don’t think you need to bother going through a string; you can just print directly, e.g. > (define (write-vector v) (displayln "#(") (for ((x (in-vector v))) (writeln x)) (displayln ")")) > (write-vector #(1 2 3)) #( 1 2 3 )


jestarray
2021-2-17 18:58:27

sweet thanks!


dvanhorn
2021-2-17 19:14:08

awesome thanks! In the meantime, using the code from draw/unsafe worked well.


jestarray
2021-2-18 00:20:35

syntax-case: unbound identifier; also, no #%app syntax transformer is bound in the transformer phase in: syntax-case https://docs.racket-lang.org/reference/stx-patterns.html#%28form._%28%28lib._racket%2Fprivate%2Fstxcase-scheme..rkt%29._syntax-case%29%29

how do i tell what the require statement is in order to import this?


badkins
2021-2-18 00:24:45

I tried replacing my hacky, mutating, chess move iterator with a generator, but it made the program 3.3x slower. Given that the move iterator is a tiny part of the whole process, that indicates generators (or at least the way I used it) are way too inefficient for my purposes. Is there something similar that’s much faster? Both versions are <https://gist.github.com/lojic/381b8b94098fc57498b38b51737f28f8|in this gist> .


shu--hung
2021-2-18 00:32:49

Assuming you are using #lang racket/base, then you may want (require (for-syntax racket/base)) to require racket/base at compile-time (technically, phase 1)


samth
2021-2-18 03:28:23

Is this CS or BC?


badkins
2021-2-18 04:52:13

Racket v7.9 [cs]


badkins
2021-2-18 04:53:49

badkins
2021-2-18 04:57:53

lol - while double checking that gist, I noticed line 42 should’ve been an else - I suppose the set! functioned as else/#t since it’s not #f :)


shu--hung
2021-2-18 07:40:05

Does this tiny fragment capture what you need in the iterators?

If it is, it seems difficult to have a more abstracted construct that win over the let-over-lambda implementation. generator is doing way more work and excels in other aspects. https://gist.github.com/shhyou/05b33363502014c25b77bab3004ebf22