Search code examples
common-lispsbclmultiple-value

returning multiple values from multiple functions?


Often, I have main functions that run multiple functions in my common lisp apps.

(main 
  (run-function-1...)
  (run-function-2...)
  (run-function-3...)
  (run-function-4...)
  (run-function-5...))

At the moment, I have this situation where each function in main returns a varying number of values. However, the number of return values are also fixed.

I want main to return all values from all function.

(main 
  (run-function-1...) ;<--- returns 2 values
  (run-function-2...) ;<--- returns 2 values
  (run-function-3...) ;<--- returns 1 values
  (run-function-4...) ;<--- returns 3 values
  (run-function-5...)) ;<--- returns 2 values

;; main should return 10 values

When I do multiple-value-bind inside of main it doesnt capture all function returns. Because it only accepts one value-form.


Solution

  • You could use multiple-value-list + append + values-list, but I think the most straightforward is multiple-value-call:

    (multiple-value-call #'values
      (run-function-1 ...) 
      (run-function-2 ...)
      (run-function-3 ...) 
      (run-function-4 ...)
      (run-function-5 ...))
    

    If you want to return a list, replace #'values with #'list.