Search code examples
racket

How do I do anything with multiple return values in 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?


Solution

  • 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.