[plt-scheme] Why multiple values?
> This is undoubtedly a basic question, but I don't think I quite grasp
> the concept behind (values ...).
It supports returning multiple values from a function without structuring
them in some data structure. Concretely, let's say that we've written a
functional stack:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module stack mzscheme
(require (lib "list.ss"))
(define empty-stack '())
;; stack-empty: (stackof X) -> boolean
(define (stack-empty? stack)
(empty? stack))
;; stack-push: X (stackof X) -> X
(define (stack-push elt stack)
(cons elt stack))
;; stack-pop: (stackof X) -> ???
(define (stack-pop stack)
...))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Now we'd like to be able to pop an element off the stack to get the top
element. At the same time, we'd like to get the rest of the stack.
That's where VALUES comes in. We can say:
(values (first stack)
(rest stack))
to return two values from STACK-POP.
Best of wishes!