Scheme How To Return Multiple Values?
Solution 1
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))
Solution 2
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
Solution 3
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.
Related videos on Youtube
Comments
-
Ramesh almost 2 years
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))))
-
Nathan Shively-Sanders over 14 yearsDid you know you can post an answer to your own question? (If you like getting points on Stack Overflow. :)
-
-
Ramesh over 14 yearsthanks 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))))
-
ulidtko about 13 yearsAnd this doesn't
print
pretty well, especially if you store pairs or lists in you car/cdr. -
Will Ness over 2 yearsthe problem is, the number of return values varies, so list is really the only viable option.