Multiple Values and Destructuring

;; Functions can return multiple values without allocating a list
(floor 17 5)                              ; 3, 2
(defun min-max (list) (values (reduce #'min list) (reduce #'max list)))
(min-max '(3 1 4 1 5))                    ; 1, 5

(multiple-value-bind (q r) (floor 17 5)
  (format nil "~a remainder ~a" q r))     ; "3 remainder 2"
(multiple-value-list (floor 17 5))        ; (3 2)
(nth-value 1 (floor 17 5))                ; 2
(multiple-value-call #'+ (floor 17 5))    ; 5
(values-list '(1 2 3))                    ; 1, 2, 3
(let ((x (floor 17 5))) x)                ; 3: extra values are ignored unless requested
(multiple-value-setq (a b) (floor 7 2))

;; destructuring-bind: pattern matching on list structure (macro lambda lists)
(destructuring-bind (a (b c) &rest more) '(1 (2 3) 4 5)
  (list a b c more))                      ; (1 2 3 (4 5))
(destructuring-bind (&key name (age 0)) '(:name "Ana")
  (list name age))                        ; ("Ana" 0)
(destructuring-bind (first &optional second) '(1) (list first second))   ; (1 NIL)

;; Pattern matching library: trivia
;; (trivia:match '(1 (2 3))
;;   ((list a (list b c)) (+ a b c)))     ; 6