我注意到,几乎所有的计划函数只能返回一个列表作为输出。
在下面,我想回邻居的所有相邻节点的多个值。
(定义(邻长宽)
(如果(和(= 1升)(= 1瓦特))
(列表(以及(l(+ 1瓦特)))(和(+ 1升)瓦特))));如何输出2个或多个值?
在这种情况下,我第一次测试,如果节点位于拐角处,如果是的话,返回所在的坐标(L和W + 1)的2值,(L + 1和w)基本上,如果我在( 1,1)返回我的(1,2)和(2,1)
同样适用于当节点只有1的边缘附近的邻居,在这种情况下,我将有3个值。
在没有边缘附近,我将有4个返回值。
我试图用利弊,追加,列表显示,写
没有人似乎有附加值的工作。我需要这为this问题。我应该如何实现它,所以我可以传递的返回值,并用它递归地返回我所有的相邻节点?
修改:我找到了答案:使用关键字值
返回多个值。例如:
(定义(店长宽)
(值(写L)
(写W)
(新行)
(表(+ 1W)升)
(表瓦特(+ 1升))))
解决方案
值,延续传送风格,并列出至少有三种方式返回多个值:
(进口(rnrs))
;让值+值
(定义(foo1)
(值1 2 3))
(允值(((A B C)(foo1)))
(显示器(列举A B C))
(新行))
; CPS
(定义(foo2的K)
(K 1 2 3))
(foo2的(拉姆达(A B C)
(显示器(列举A B C))
(新行)))
;名单
(定义(foo3)
(表1 2 3))
(让((结果(foo3)))
(显示结果)
(新行))
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))))
解决方案
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))
相关推荐
最新文章