views:

485

answers:

4

I notice that almost all scheme functions can only return one list as output.

In the following, I would like to return multiple values of all the adjacent nodes of neighbors.

 (define (neighbors l w)
   (if (and (= 1 l) (= 1 w))
     (list (and (l (+ 1 w))) (and (+ 1 l) w)))) ; how to output 2 or more values?

In this case I'm first testing if the node is at corner, if so, return 2 values of the coordinates where (l and w+1), (l+1 and w) basically if I'm at (1,1) return me (1,2) and (2,1)

Same applies when the node has only 1 neighbor near the edge, in this case I will have 3 values.

When no edge is nearby I will have 4 return values.

I tried to use cons, append, list, display, write none of them seems working with additional values. I need this as a sub-function of this question. How should I implement it so I could pass on the return value and use it recursively to return me all the adjacent nodes?

Edit: I found the answer: use the keyword "values" to return multiple values. Example:

(define (store l w)
  (values (write l)
          (write w)
          (newline)
          (list (+ 1 w) l)
          (list w (+ 1 l))))
+2  A: 

You can return a pair of values in a cons cell:

(define (foo)
  (cons 'a 5))

(let* ((r (foo))
       (x (car r))
       (y (cdr r)))
  (display x) (display y) (newline))

You can generalise this to return multiple values in a list, too.

Greg Hewgill
thanks I just found a better way, use "values" I can return any number of return values I want :)(define (store l w) (values (write l) (write w) (newline) (list (+ 1 w) l) (list w (+ 1 l))))
Jonathan
+2  A: 

values, continuation passing style, and list are at least three ways of returning multiple values:

(import (rnrs))


; let-values + values
(define (foo1)
  (values 1 2 3))

(let-values (((a b c) (foo1)))
  (display (list a b c))
  (newline))

; cps
(define (foo2 k)
  (k 1 2 3))

(foo2 (lambda (a b c) 
        (display (list a b c))
        (newline)))

; list
(define (foo3)
  (list 1 2 3))
(let ((result (foo3)))
  (display result)
  (newline))
grettke
A: 

Returning a pair of values in a cons cell is the correct answer. The other is not.

Ryan Aghdam
+1  A: 

The Guile implementation of Scheme has a receive syntax, which it says is "much more convenient" than values. I haven't used it yet, however, but this may be useful:

http://www.gnu.org/software/guile/manual/html%5Fnode/Multiple-Values.html

KirarinSnow