Reflecting on context arity

25 views
Skip to first unread message

William J. Bowman

unread,
Mar 27, 2020, 8:28:16 PM3/27/20
to Racket Users
I'm trying to write a generalization of map that supports multiple return
values:

(define (map-n n f ls . lss)
(if (empty? ls)
(apply values (build-list n (lambda _ '())))
(call-with-values
(thunk (apply f (car ls) (map car lss)))
(lambda vs
(call-with-values
(thunk (apply map-n n f (cdr ls) (map cdr lss)))
(lambda lss
(apply
values
(map cons vs lss))))))))

This version works fine, but, tediously, requires I specify how many return
values.
This is annoying, since surely the context knows how many values it expects:

> (let/ec escape
(let-values ([(a b c)
(let/cc k
(escape (procedure-arity k)))])
'meow))
> (arity-at-least 0)

Oh, I guess not.
Well that's fine, because surely I can reflect on `f`'s result arity:

> (procedure-result-arity (lambda (x) (values 'a 'b 'c)))
> #f

Welp.

So, uh, can I haz a current-context-arity please?

--
William J. Bowman

Ben Greenman

unread,
Mar 27, 2020, 8:40:22 PM3/27/20
to Racket Users
procedure-result-arity is very limited

if you can find a way to call `f` once, though, you could assume that
its result arity never changes

Laurent

unread,
Mar 28, 2020, 5:48:40 AM3/28/20
to Ben Greenman, Racket Users
Follow up on Ben's idea:
Take n as an optional argument, init value #f.
On first application of f on the first values, continue n-map with n=received number of values. Once arrived at the empty lists you know how many values you should have.
At each iteration, you can even check that the number of values doesn't change (or take the max?).

The only corner case is when provided with empty lists from the start, in which case either the user can specify the init value for n if it is known, or you can throw an exception.


--
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/CAFUu9R6YDx9%2BF27dHW3N1P%3DMK_UuzfrWqo01kigM4fBKxxFXUA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages