Correct i combinator.

This commit is contained in:
Simon Forman 2021-04-09 17:13:09 -07:00
parent fbdd79a8db
commit 49bcab2e91
2 changed files with 15 additions and 9 deletions

View File

@ -842,7 +842,10 @@ def i(stack, expression, dictionary):
Q
'''
quote, stack = stack
try:
quote, stack = stack
except ValueError:
raise StackUnderflowError
return stack, concat(quote, expression), dictionary

View File

@ -70,6 +70,7 @@ printed left-to-right. These functions are written to support :doc:`../pretty`.
.. _cons list: https://en.wikipedia.org/wiki/Cons#Lists
'''
from .errors import NotAListError
def list_to_stack(el, stack=()):
@ -164,7 +165,7 @@ def concat(quote, expression):
# RuntimeError: maximum recursion depth exceeded
# on quotes longer than sys.getrecursionlimit().
return (quote[0], concat(quote[1], expression)) if quote else expression
## return (quote[0], concat(quote[1], expression)) if quote else expression
# Original implementation.
@ -173,13 +174,15 @@ def concat(quote, expression):
# In-lining is slightly faster (and won't break the
# recursion limit on long quotes.)
## temp = []
## while quote:
## item, quote = quote
## temp.append(item)
## for item in reversed(temp):
## expression = item, expression
## return expression
temp = []
while quote:
if not isinstance(quote, tuple):
raise NotAListError
item, quote = quote
temp.append(item)
for item in reversed(temp):
expression = item, expression
return expression