Skip to content

Instantly share code, notes, and snippets.

@andydude
Created October 5, 2024 12:33
Show Gist options
  • Save andydude/c13e17fb245a3119eea8b1f4af9f5f96 to your computer and use it in GitHub Desktop.
Save andydude/c13e17fb245a3119eea8b1f4af9f5f96 to your computer and use it in GitHub Desktop.
Impl. of (scheme base) in Python.
"""
scheme_base.py -- by andydude
This is an implementation of (scheme base) in Python.
"""
class char_t(str):
def __init__(self, o):
if isinstance(o, int):
self.__init__(chr(o))
elif isinstance(o, str):
self.__init__(o)
else:
raise TypeError
class bytevector_t(bytearray):
def __init__(self, o):
if isinstance(o, bytes):
self.__init__(o)
elif isinstance(o, bytearray):
self.__init__(o)
else:
raise TypeError
class symbol_t(str):
def __init__(self, o):
if isinstance(o, str):
self.__init__(o)
else:
raise TypeError
def append(*lists):
return sum(lists, [])
def begin(*args):
return args[-1]
def null_q(o):
return isinstance(o, list) and len(o) == 0
def pair_q(o):
return isinstance(o, list) and len(o) > 0
def cons(a, b):
assert isinstance(b, list)
return [a] + b
def car(o):
assert isinstance(o, list)
return o[0]
def cdr(o):
assert isinstance(o, list)
return o[1:]
def list_ref(o, k):
assert isinstance(o, list)
return o[k]
def list_set_x(ls, k, obj):
assert isinstance(o, list)
ls[k] = obj
return None
def even_q(o):
assert isinstance(o, int)
return bool(0 == o & 1)
def odd_q(o):
assert isinstance(o, int)
return bool(1 == o & 1)
def list_tail(o, k):
assert isinstance(o, list)
return o[k:]
def make_list(k, fill):
return [fill for _ in range(k)]
def zero_q(o):
assert isinstance(o, int)
return bool(0 == o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment