Home > OS >  How to set two variables at once in Hy
How to set two variables at once in Hy

Time:08-14

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()

CodePudding user response:

Tuples in Hy are written #( … ), so the literal translation of a, b = f() is (setv #(a b) (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 [a b] (f)).

  • Related