[英]How to set two variables at once in Hy
I just started learning Hy (my first attempt at a Lisp dialect).我刚开始学习 Hy(我第一次尝试 Lisp 方言)。 I have a function that returns a tuple of 2 values, and I don't know how to receive it:
我有一个返回 2 个值的元组的 function,但我不知道如何接收它:
(defn function [] #("Hello" "World"))
; I don't know how to initialize two variables here
(setv tuple (function))
(get tuple 0) ; "Hello"
(get tuple 1) ; "World"
So, in Python, it'd look like this:所以,在 Python 中,它看起来像这样:
def function():
return "Hello", "World"
# This is what I have in Hy.
# tuple = function()
# tuple[0] # "Hello"
# tuple[1] # "World"
# This is what I want:
a, b = function()
Tuples in Hy are written #( … )
, so the literal translation of a, b = f()
is (setv #(ab) (f))
. Hy 中的元组写成
#( … )
,所以a, b = f()
的直译是(setv #(ab) (f))
。 As in Python, you can also use a list for this rather than a tuple, and the syntax for lists in Hy is a little shorter: (setv [ab] (f))
.就像在 Python 中一样,您也可以为此使用列表而不是元组,并且 Hy 中列表的语法要短一些:
(setv [ab] (f))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.