notjack
2020-10-27 20:49:33

Made a neat macro that I think this channel would be interested in: (guarded-block (define x (random 10)) (guard (even? x) (define message-template "X was even, X = ~a, returning early") (format message-template x)) (define y (random 10)) (guard (odd? y) (define message-template "Y was odd, Y = ~a, returning early") (format message-template y)) (+ x y))


notjack
2020-10-27 20:55:42

the above expands into this: (block (define x (random 10)) (cond [(even? x) (define message-template "X was even, X = ~a, returning early") (format message-template x)] [else (define y (random 10)) (cond [(odd? y) (define message-template "Y was odd, Y = ~a, returning early") (format message-template y)] [else (+ x y)])]))


soegaard2
2020-10-27 20:58:30

I have been using a binding form defr (short for define-random). A short example with just one clause:

(defr [(x y) (random 1 10)] #:where (> (+ x y) 5)]) This computes two random numbers and binds them to x and y. Then checks if the predicate (> (+ x y) 5) . If true we carry on, if not, a new set of numbers are generated until we find a pair that fullfills the predicate.


soegaard2
2020-10-27 20:59:23

The form is useful, when you need to generate a set of numbers that needs to be in some relation to each other.


soegaard2
2020-10-27 21:00:46

I like that you guarded-block has an expansion without let/ec.


notjack
2020-10-27 21:01:44

Yeah I really want to avoid paying the cost of continuations


notjack
2020-10-27 21:02:31

I think this version is something we could add directly to racket and make define support automatically