How to create a list with interpolated variables? #102
-
What's the Hissp equivalent of this Scheme?
In Python it's just |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
This comment has been hidden.
This comment has been hidden.
-
Nearest equivalent (this needs the bundled macros): (define foo 'bar)
(en#tuple 1 foo 2) Hissp code is based on tuples, not linked lists. Python's builtin Without any bundled macros (Scheme calls this "quasiquoting"): (.update (globals) : foo 'bar)
`(,1 ,foo ,2) This uses Python builtins to accomplish the assignment and the template quote reader syntax to make a data tuple. Notice how this also expands to a lambda. (Try it in the REPL.) The ints technically don't have to be unquoted here: `(1 ,foo 2) But make sure you understand how fragment quoting works before omitting unquotes on strings. With the empty fragment hack ( (.update (globals) : foo 'bar)
(|| 1 foo 2 ||) No reader syntax either. An equivalent form also works in readerless mode: ('.update',('globals',),':','foo',('quote','bar'),)
('',1,'foo',2,'',) With the "list of" macro (define foo 'bar)
(@ 1 foo 2) This may be more idiomatic in Lissp, depending on what you are trying to do. It makes a Python list, not a tuple, however. With Python fragments (escape hatch): |(foo := 'bar')|
|(1, foo, 2)| Lissp allows you to inject Python expressions (or even statements, at the top level) directly into the compiled output. Anything you know how to do in Python can be done in Lissp this way. |
Beta Was this translation helpful? Give feedback.
Nearest equivalent (this needs the bundled macros):
Hissp code is based on tuples, not linked lists. Python's builtin
tuple
takes a single iterable argument. Theen#
reader macro uses a lambda to convert a function applicable to one tuple to a function of its elements.Without any bundled macros (Scheme calls this "quasiquoting"):
This uses Python builtins to accomplish the assignment and the template quote reader syntax to make a data tuple. Notice how this also expands to a lambda. (Try it in the REPL.) The ints technically don't have to be unquoted here:
But make sure you understand how fragm…