Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do anything with multiple return values in racket?

Tags:

racket

It seems like in order to use multiple return values in Racket, I have to either use define-values or collect them into a list with (call-with-values (thunk (values-expr)) list). In the latter case, why would someone to choose to return multiple values instead of a list, if just have to collect them into a list anyway? Additionally, both of these are very wordy and awkward to work into most code. I feel like I must be misunderstanding something very basic about multiple-return-values. For that matter, how do I write a procedure accepting multiple return values?

like image 873
Matt G Avatar asked Dec 13 '13 00:12

Matt G


People also ask

Can you have multiple return values?

To return multiple values from a function, you can pack the return values as elements of an array or as properties of an object.

How would you return more than one value from a method?

If we want the function to return multiple values of same data types, we could return the pointer to array of that data types. We can also make the function return multiple values by using the arguments of the function.


1 Answers

Although I may be missing some of the Scheme history and other nuances, I'll give you my practical answer.

First, one rule of thumb is if you need to return more than 2 or 3 values, don't use multiple values and don't use a list. Use a struct. That will usually be easier to read and maintain.

Racket's match forms make it much easier to destructure a list return value -- as easy as define-values:

(define (f)
  (list 1 2))

(match-define (list a b) (f))
(do-something-with a b)

;; or

(match (f)
  [(list a b) (do-something-with a b)])

If you have some other function, g, that takes a (list/c a b), and you want to compose it with f, it's simpler if f returns a list. It's also simpler if both use a two-element struct. Whereas call-with-values is kind of an awkward hot mess, I think.

Allowing multiple return value is an elegant idea, because it makes return values symmetric with arguments. Using multiple values is also faster than lists or structs (in the current implementation of Racket, although it could work otherwise).

However when readability is a higher priority than performance, then in modern Racket it can be more practical to use a list or a struct, IMHO. Having said that I do use multiple values for one-off private helper functions.

Finally, there's a long, interesting discussion on the Racket mailing list.

like image 139
Greg Hendershott Avatar answered Sep 22 '22 10:09

Greg Hendershott