I just started learning Hy (my first attempt at a Lisp dialect). I have a function that returns a tuple of 2 values, and I don't know how to receive it:
(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:
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))
. 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))
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.