Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring multiple return values in Racket

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?

like image 946
abeln Avatar asked Jun 19 '13 20:06

abeln


1 Answers

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
like image 129
Chris Jester-Young Avatar answered Nov 15 '22 07:11

Chris Jester-Young