Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set two variables at once in Hy

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading