1

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.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
Vinn
  • 1,030
  • 5
  • 13

2 Answers2

4

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.

Martin Půda
  • 7,353
  • 2
  • 6
  • 13
2

You have to repack the returned values into a new values call:

(defun run-function-1 ()
  (values 1 2))
(defun run-function-2 ()
  (values 3 4 5))
(defun main () 
  (multiple-value-bind (a b) (run-function-1) 
    (multiple-value-bind (c d e) (run-function-2)
    (values a b c d e))))
(main)
1 ;
2 ;
3 ;
4 ;
5
Leo
  • 1,869
  • 1
  • 13
  • 19