new thing - functions returning multiple values

(values 1 2 3) at the tnd of funtion fill make funciton return 1, 2, 3
and I can either treat function as returning single value,
or use functions like multiple-value-list or multiple-value-[bind|setq]
to get secodary return values

so not quite like a tuple
This commit is contained in:
efim
2022-07-28 15:35:14 +00:00
parent 6dcca61283
commit 89673df888
3 changed files with 131 additions and 104 deletions

View File

@@ -22,20 +22,28 @@
(y (multiple-value-list (floor 3/2))))
(assert-equal x 1)
(assert-equal y '(1 1/2)))
(assert-equal ____ (multiple-value-list (floor 99/4))))
(assert-equal '(24 3/4) (multiple-value-list (floor 99/4))))
(defun next-fib (a b)
;; The function VALUES allows returning multiple values.
(values b (+ a b)))
(values 5 4 3) ; now this, is very new and like what
;; not at all what I saw in scala types, if that's not a tuple, but just "multiple values"
;; funcitons are with prefix #'multiple-values-.*
(define-test binding-and-setting-multiple-values
;; The macro MULTIPLE-VALUE-BIND is like LET, except it binds the variables
;; listed in its first argument to the values returned by the form that is its
;; second argument.
(multiple-value-bind (x y) (next-fib 3 5)
(let ((result (* x y)))
(assert-equal ____ result)))
;; The macro MULTIPLE-VALUE-BIND is like LET, except it binds the variables
;; listed in its first argument to the values returned by the form that is its
;; second argument.
(multiple-value-bind (x y) (next-fib 3 5)
(let ((result (* x y)))
(assert-equal 40 result)))
;; SETF can also set multiple values if a VALUES form is provided as a place.
(let (x y)
(setf (values x y) (next-fib 5 8))
(assert-equal ____ (list x y))))
(assert-equal '(8 13) (list x y))))
;;; or maybe #'values is a tuple,
;;; it's used as accessor in setf, not quite destructuring, since that's not possible with lists?