Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme How To Return Multiple Values?

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))))
like image 836
Jonathan Avatar asked Oct 16 '09 10:10

Jonathan


People also ask

How can I return multiple values?

You can return multiple values by bundling those values into a dictionary, tuple, or a list. These data types let you store multiple similar values. You can extract individual values from them in your main program. Or, you can pass multiple values and separate them with commas.

How do you return multiple things from a function?

If we want the function to return multiple values of same data types, we could return the pointer to array of that data types. We can also make the function return multiple values by using the arguments of the function.

Can a class return multiple values?

We can use Pair in Java to return two values. We can encapsulate all returned types into a class and then return an object of that class.

Can you return multiple values in CPP?

While C++ does not have an official way to return multiple values from a function, one can make use of the std::pair , std::tuple , or a local struct to return multiple values.


3 Answers

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))
like image 141
grettke Avatar answered Oct 10 '22 19:10

grettke


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_node/Multiple-Values.html

like image 42
KirarinSnow Avatar answered Oct 10 '22 21:10

KirarinSnow


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.

like image 21
Greg Hewgill Avatar answered Oct 10 '22 20:10

Greg Hewgill