def combinator_reduce(lst, depth=MAX_DEPTH): """Reduce combinatory logic list of lists. TODO: Incorporate other reduction strategies / evaluation orders. """ if not isinstance(lst, list): return lst elif len(lst) == 0: return list() elif depth < 0: raise CombinatorEvaluationDepthException elif len(lst) > MAX_LENGTH: raise CombinatorEvaluationLengthException else: op, args = lst[0], lst[1:] newdepth = depth-1 if isinstance(op, list): return combinator_reduce( op + args, newdepth ) elif op == 'I' and len(args) >= 1: return combinator_reduce( args[1:], newdepth) elif op == 'K' and len(args) >= 2: x,y,rest = args[0], args[1], args[2:] return combinator_reduce( [x] + rest, newdepth) elif op == 'S' and len(args) >= 3: x,y,z,rest = args[0], args[1], args[2], args[3:] return combinator_reduce( [x, z, [y, z]] + rest, newdepth ) else: rest = map(lambda x: unlist_singleton(combinator_reduce(x, newdepth)), args) if len(rest) == 0: return lst else: return [op, ] + rest
def combinator_reduce(lst, depth=MAX_DEPTH): """ Reduce combinatory logic list of lists. TODO: Incorporate other reduction strategies / evaluation orders. """ if not isinstance(lst, list): return lst elif len(lst) == 0: return list() elif (depth < 0): raise CombinatorEvaluationDepthException elif (len(lst) > MAX_LENGTH): CombinatorEvaluationLengthException else: op, args = lst[0], lst[1:] newdepth = depth-1 if isinstance(op, list): return combinator_reduce( op + args, newdepth ) elif op == 'I' and len(args) >= 1: return combinator_reduce( args[1:], newdepth) elif op == 'K' and len(args) >= 2: x,y,rest = args[0], args[1], args[2:] return combinator_reduce( [x] + rest, newdepth) elif op == 'S' and len(args) >= 3: x,y,z,rest = args[0], args[1], args[2], args[3:] return combinator_reduce( [x, z, [y, z]] + rest, newdepth ) else: rest = map(lambda x: unlist_singleton(combinator_reduce(x, newdepth)), args) if len(rest) == 0: return lst else: return [op,]+rest
def parseScheme(s): """ Return a list of list of lists... for a string containing a simple lambda expression (no quoting, etc) Keeps uppercase and lowercase letters exactly the same e.g. parseScheme("(S (K (S I)) (S (K K) I) x y)") --> ['S', ['K', ['S', 'I']], ['S', ['K', 'K'], 'I'], 'x', 'y'] """ x = sexp.parseString(s, parseAll=True) return unlist_singleton( x.asList() ) # get back as a list rather than a pyparsing.ParseResults
def parseScheme(s): """ Return a list of list of lists... for a string containing a simple lambda expression (no quoting, etc) Keeps uppercase and lowercase letters exactly the same e.g. parseScheme("(S (K (S I)) (S (K K) I) x y)") --> ['S', ['K', ['S', 'I']], ['S', ['K', 'K'], 'I'], 'x', 'y'] """ x = sexp.parseString(s, parseAll=True) return unlist_singleton( x.asList()) # get back as a list rather than a pyparsing.ParseResults