def Lisprint(x): print( ' (' + ', '.join(x) + ')' , '\n')
a= ' a b c ? def f x if zero? x 0 1 '
a += ' A B C ! just an example '
x= a.split()
print(x)
Lisprint(x)
['a', 'b', 'c', '?', 'def', 'f', 'x', 'if', 'zero?', 'x', '0', '1', 'A', 'B', 'C', '!', 'just', 'an', 'example']
(a, b, c, ?, def, f, x, if, zero?, x, 0, 1, A, B, C, !, just, an, example)
[1, 2, [3, 4, [5, 6, 7], 8], 9]nested = [1, 2, [3, 4, [5, 6, 7], 8], 9]
print(nested)
(1, 2, [3, 4, [5, 6, 7], 8], 9)print(tuple(nested))
(1, 2, (3, 4, (5, 6, 7), 8), 9)print(p2b(nested))
def Lisprint(x): print( ' (' + ', '.join(x) + ')' , '\n')
a= ' a b c ? def f x if zero? x 0 1 '
a += ' A B C ! just an example '
x= a.split()
print(x)
Lisprint(x)
['a', 'b', 'c', '?', 'def', 'f', 'x', 'if', 'zero?', 'x', '0', '1', 'A', 'B', 'C', '!', 'just', 'an', 'example']
(a, b, c, ?, def, f, x, if, zero?, x, 0, 1, A, B, C, !, just, an, example)
I so rarely need to save a list in python in a form acceptable to LISP but here is a go with no visible recursion needed.
nested = [1, 2, [3, 4, [5, 6, 7], 8], 9]
[1, 2, [3, 4, [5, 6, 7], 8], 9]print(nested)
# Just converting to a tuple does not change nested lists
(1, 2, [3, 4, [5, 6, 7], 8], 9)print(tuple(nested))
# But a function that typographically replaces [] with () needs no recursion >>> def p2b(nested_list): return repr(nested_list).replace('[','(').replace(']',')')
(1, 2, (3, 4, (5, 6, 7), 8), 9)print(p2b(nested))
People who speak python well do not necessarily lisp.
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmai...@python.org> On Behalf Of Hen Hanna
Sent: Sunday, February 26, 2023 4:54 AM
To: pytho...@python.org
Subject: Re: one Liner: Lisprint(x) --> (a, b, c) instead of ['a', 'b', 'c']
On Saturday, February 25, 2023 at 11:45:12 PM UTC-8, Hen Hanna wrote:
def Lisprint(x): print( ' (' + ', '.join(x) + ')' , '\n')
a= ' a b c ? def f x if zero? x 0 1 '
a += ' A B C ! just an example '
x= a.split()
print(x)
Lisprint(x)
['a', 'b', 'c', '?', 'def', 'f', 'x', 'if', 'zero?', 'x', '0', '1', 'A', 'B', 'C', '!', 'just', 'an', 'example']
(a, b, c, ?, def, f, x, if, zero?, x, 0, 1, A, B, C, !, just, an, example)
For nested lists.... impossible to improve upon P.Norvig's code
def Lisprint(x): print(lispstr(x))
def lispstr(exp):
"Convert a Python object back into a Lisp-readable string."
if isinstance(exp, list):
return '(' + ' '.join(map(lispstr, exp)) + ')'
else:
return str(exp)
a= ' a b c '
x= a.split()
x += [['a', 'b', 'c']]
x += x
print(x)
Lisprint(x)
['a', 'b', 'c', ['a', 'b', 'c'], 'a', 'b', 'c', ['a', 'b', 'c']]
(a b c (a b c) a b c (a b c))
---------- Without the commas, the visual difference (concision) is striking !
--
https://mail.python.org/mailman/listinfo/python-list
is it poss. to peek at the Python-list's messages without joining ?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 307 |
Nodes: | 16 (2 / 14) |
Uptime: | 63:05:12 |
Calls: | 6,915 |
Files: | 12,379 |
Messages: | 5,431,470 |