def regexist(context: dict, name: str, optcheck=None) -> dict: if optcheck is None: context[name + "?"] = pythoniclam( lambda x: boolean(type(x) == dict and x["type"] == name), name + "?") else: context[name + "?"] = pythoniclam( lambda x: boolean(tryexplode(optcheck, x)), name + "?") return context
def makemethod(gc, base, extensions): gc["make-%s" % base] = export_to_lam(lambda ls: { "type": base, "const": ls }) for ext in range(0, len(extensions)): gc["%s-%s" % (base, extensions[ext])] = pythoniclam( lambda x, ix=ext: ensure(x, base)["const"][ix], "%s-%s" % (base, extensions[ext])) return gc
def export_listcomp(context): listcomps = [ foldr_src, compose_src, filter_src, map_src, andmap_src, ormap_src, blist_src ] for comp in listcomps: context = handle_define(context, comp) context["apply"] = pythonlamwcontext(apply, "apply") context["ztnmi"] = pythoniclam( lambda x: quicklist( list(map(num, list(range(intify(trynum(x)["value"])))))), "ztnmi") return context
def makestruct(gc, lc, code): # (define-struct base [ getters ] ) base = code[1] extensions = code[2] gc = regexist(gc, base) gc = makemethod(gc, base, extensions) return gc def tryexplode(optcheck, x): try: optcheck(x) return True except: return False def regexist(context: dict, name: str, optcheck=None) -> dict: if optcheck is None: context[name + "?"] = pythoniclam( lambda x: boolean(type(x) == dict and x["type"] == name), name + "?") else: context[name + "?"] = pythoniclam( lambda x: boolean(tryexplode(optcheck, x)), name + "?") return context equal_exported = pythoniclam(equal, "equal?")
if len(params) == 2: return string(ostr[start:]) elif len(params) == 3: end = trynum(params[2])["value"] return string(ostr[start: end]) def appendstrings(params): return string(foldr(list(valuesof(map(trystring, params))), lambda x, y: x + y, "")) def areprefixes(a, b): if len(b) <= len(a): return b == a[:len(b)] lenstr = pythoniclam(lambda x: num(len(trystring(x)["value"])), "string-length") appstr = export_to_lam(appendstrings, "string-append") streq = pythoniclam(lambda x, y: boolean(trystring(x)["value"] == trystring(y)["value"]), "string=?") strle = pythoniclam(lambda x, y: boolean(trystring(x)["value"] < trystring(y)["value"]), "string<?") strleq = pythoniclam(lambda x, y: boolean(trystring(x)["value"] <= trystring(y)["value"]), "string<=?") strge = pythoniclam(lambda x, y: boolean(trystring(x)["value"] > trystring(y)["value"]), "string>?") strgeq = pythoniclam(lambda x, y: boolean(trystring(x)["value"] >= trystring(y)["value"]), "string>=?") strlo = pythoniclam(lambda x: string(trystring(x)["value"].lower()), "string-downcase") strup = pythoniclam(lambda x: string(trystring(x)["value"].upper()), "string-upcase") strcont = pythoniclam(lambda s, n: boolean(trystring(n)["value"] in trystring(s)["value"]), "string-contains?") strpref = pythoniclam(lambda s, p: boolean(areprefixes(trystring(s)["value"], trystring(p)["value"])), "string-prefix?") strsuff = pythoniclam(lambda sr, su: boolean(areprefixes(trystring(sr)["value"][::-1], trystring(su)["value"][::-1])), "string-suffix?") strne = pythoniclam(lambda x: boolean(len(trystring(x)["value"]) > 0), "non-empty-string?") substr = export_to_lam(substringrack, "substring")
def getListLen(lox): if equal(lox, list_empty)["value"]: return num(0) elif isCons(lox): return num(1 + getListLen(lox["const"][1])["value"]) else: raise Exception("did not take the length of a list: %s" % pttyobj(lox)) def list_racket_append(lists_to_append): pylists = map(topylist, lists_to_append) return quicklist(foldr(list(pylists), lambda x, y: list(x + y), [])) sym_eq = pythoniclam( lambda symx, symy: boolean( trysymbol(symx)["value"] == trysymbol(symy)["value"]), "symbol=?") sym_tostr = pythoniclam(lambda symx: string(trysymbol(symx)["value"][1:]), "symbol->string") list_empty = symbol("'()") list_fst = pythoniclam(lambda x: ensure(x, "cons")["const"][0], "first") list_rst = pythoniclam(lambda x: ensure(x, "cons")["const"][1], "rest") list_snd = compose(list_fst, list_rst, "second") list_trd = compose(list_fst, compose(list_rst, list_rst), "third") list_len = pythoniclam(lambda x: getListLen(x), "length") list_cons = pythoniclam(cons, "cons") list_isempty = pythoniclam(lambda x: equal(x, list_empty), "empty?") list_iscons = pythoniclam(lambda x: boolean(isCons(x)), "cons?") list_rev = pythoniclam(lambda x: quicklist(topylist(x)[::-1]), "reverse") list_range = pythoniclam( lambda s, e, st: quicklist(
from casting import tryboolean from lamstructs import pythoniclam from shortcuts import boolean, string eq_bool = pythoniclam( lambda x, y: boolean(tryboolean(x)["value"] == tryboolean(y)["value"]), "boolean=?") not_bool = pythoniclam(lambda x: boolean(not tryboolean(x)["value"]), "not") bool_tostr = pythoniclam(lambda x: string(str(tryboolean(x)["value"])), "boolean->string")
q = list(map(trynum, nums)) return {"type": "number", "value": max(valuesof(q))} def num_min(nums: List): q = list(map(trynum, nums)) return {"type": "number", "value": min(valuesof(q))} add_num = export_to_lam(num_add, "+") mul_num = export_to_lam(num_mul, "*") sub_num = export_to_lam(num_sub, "-") div_num = export_to_lam(num_div, "/") max_num = export_to_lam(num_max, "max") min_num = export_to_lam(num_min, "min") add1_num = pythoniclam(lambda x: num(trynum(x)["value"] + 1), "add1") sub1_num = pythoniclam(lambda x: num(trynum(x)["value"] - 1), "sub1") abs_num = pythoniclam(lambda x: num(abs(trynum(x)["value"])), "abs") eq_num = pythoniclam( lambda x, y: boolean(trynum(x)["value"] == trynum(y)["value"]), "=") ge_num = pythoniclam( lambda x, y: boolean(trynum(x)["value"] > trynum(y)["value"]), ">") geq_num = pythoniclam( lambda x, y: boolean(trynum(x)["value"] >= trynum(y)["value"]), ">=") le_num = pythoniclam( lambda x, y: boolean(trynum(x)["value"] < trynum(y)["value"]), "<") leq_num = pythoniclam( lambda x, y: boolean(trynum(x)["value"] <= trynum(y)["value"]), "<=") modu_num = pythoniclam( lambda x, y: num(trynum(x)["value"] % trynum(y)["value"]), "modulo") sqr_num = pythoniclam(lambda x: num(trynum(x)["value"]**2), "sqr")