[plt-scheme] Why multiple values?

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Sun Dec 24 14:11:46 EST 2006

> 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!


Posted on the users mailing list.