该方案等效于元组拆包? [英] What is scheme's equivalent of tuple unpacking?
本文介绍了该方案等效于元组拆包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中,我可以执行以下操作:
In Python, I can do something like this:
t = (1, 2)
a, b = t
...并且a将为1,b将为2.假设我在Scheme中有一个列表'(1 2)
.有什么办法可以与let
做类似的事情吗?如果有什么不同,我正在使用球拍.
...and a will be 1 and b will be 2. Suppose I have a list '(1 2)
in Scheme. Is there any way to do something similar with let
? If it makes a difference, I'm using Racket.
推荐答案
In racket you can use match
,
(define t (list 1 2))
(match [(list a b) (+ a b)])
That works for lists, vectors, structs, etc etc. For multiple values, you'd use define-values
:
(define (t) (values 1 2))
(define-values (a b) (t))
或let-values
.但是请注意,我不能将t
定义为元组",因为(大多数)方案实现中多个值不是一等值.
or let-values
. But note that I can't define t
as a "tuple" since multiple values are not first class values in (most) scheme implementations.
这篇关于该方案等效于元组拆包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文