In Racket, it's possible to return multiple values from a function by doing e.g.
(define (foo)
(values 1 2 3))
Then we can bind them by doing
(define-values (one two three) (foo))
Now one
is bound to 1
, two
to 2
, and three
to 3
.
I have a function that returns multiple values, but I'm interested only in some of them. Is there a way of extracting the "interesting" return values, while "ignoring" (i.e. not binding) the rest, a la _
pattern in Haskell?
You can use match-let-values
or match-define-values
for this (depending on whether you want lexical or top-level variables):
> (match-let-values (((_ _ a _) (values 1 2 3 4)))
a)
; => 3
> (match-define-values (_ a _ _) (values 1 2 3 4))
> a
; => 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With