python如何实现一个简单的lisp
发表于:2024-09-21 作者:千家信息网编辑
千家信息网最后更新 2024年09月21日,这篇文章给大家分享的是有关python如何实现一个简单的lisp的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。示例:#coding=utf-8import types,sy
千家信息网最后更新 2024年09月21日python如何实现一个简单的lisp
这篇文章给大家分享的是有关python如何实现一个简单的lisp的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
示例:
#coding=utf-8import types,sysimport operator as opfrom functools import reducefrom inspect import getargspec##########################def quote(code): return codedef atom(code): if type(code) is list and code != []: return [] return Truedef eq(car, cdr): if car==cdr: return True return []def noteq(car, cdr): if car != cdr: return True return []def and2(car, cdr):#用lisp实现一个and吧 if car!=[] and cdr!=[]: return True return []def car(code): if code == [] or code == '':#fix map func error return [] return code[0]def cdr(code): return code[1:]def cons(arg1, arg2): res = [arg1] res.extend(arg2); return resdef cond(code):passdef define():passdef fun():passdef fun_seq():passclass S(str):pass#extradef lt(car, cdr):#< if car < cdr: return True return []def gt(car, cdr):#> if car > cdr: return True return [] return reduce(op.mul, args) return reduce(op.div, args) return reduce(op.sub, args) return reduce(op.add, args)def mod(car, cdr):#% return car % cdrdef show(code): print( code) return codedef seti(key, val): pass# def yf():# pass# def yd():# passdef nexti(sq, n): if not hasattr(sq, '__next__'): sq = iter(sq) while n: if n == 1: return next(sq) next(sq) n = n - 1def use(pg, _as=None): try: mod = __import__(pg) return mod, _as except Exception as e: print('') return None, Nonedef attr(obj, prop): return getattr(obj, prop)###############################class Env(dict): def __init__(self, outer, params=None, args=None): params = params if params else [] args = args if args else [] #for currying for i in range(len(args),len(params)): args.append([]) #end self.update(zip(params,args))# self.outer = outer def find(self, key): ks = key.split('.') if len(ks) > 1: if ks[0] in self: for k in ks[1:]: ks[0] = getattr(self[ks[0]], k) return ks[0] return self.outer[key] else: if key in self: return self[key] return self.outer.find(key) def seti(self, key,value): if key in self: self[key] = value else: self.outer.seti(key,value)#############################lisp_env = {'quote':quote,'_':quote,'atom':atom,'eq':eq,'car':car, 'cdr':cdr,'cons':cons,'cond':cond,'define':define,'fun':fun, 'lt':lt,'mul':mul,'sub':sub,'gt':gt,'div':div,'mod':mod, 'add':add,'noteq':noteq,'and2':and2, 'next':next, 'nexti':nexti, 'show':show, 'seti':seti, 'True':True, 'False':False, 'use':use, 'attr':attr }#############################def run(code,env): # print '===code===', code, '======' # print '===env===', env, '======' # print '===outer===', env.outer, '======' #return S('val'),变量 if type(code) is S: try: return env.find(code) except: print( ' ' % code, '') raise RuntimeError #常量 #for empty list and no list elif code==[] or type(code) is not list: return code #设置变量的值 elif code[0] is seti: if type(code[1]) is not S: print (" " % code[1], '') raise RuntimeError try: key = env.seti(code[1], run(code[2], env)) return env.find(code[1]) except: print( " " % code[1], '') raise RuntimeError #变量引用,for quote exp elif code[0] is quote: #特别函数,for def, fun, let elif code[0] is define: env[ code[1]] = run(code[2],env) # if code[1] == 'inner' or code[1]=='E': # print '--define--', code[1], code[2], env return env[ code[1]] #执行function体 elif code[0] is fun_seq: last = None codelen = len(code[1:]) - 1 for i, c in enumerate(code[1:]): res = run(c, env) if i == codelen: last = res return last elif code[0] is fun: # if code[1] and code[1][-1] == 'ident' or code[1] and code[1][-1] == '__outer__': # print '--eval--', env if len(code[1]) == 0: if type(code[1]) is S: #条件,for cond, [cond [p1,e1],[p2,e2],...] elif code[0] is cond: for c in code[1:]: if run(c[0], env): res = [run(ii, env) for ii in c[1:]] return res[-1] elif code[0] is use: mod, _as = None,None if len(code) > 2: mod, _as = use(code[1], code[2]) else: mod, _as = use(code[1]) if not mod: raise RuntimeError if _as: env[_as] = mod else: env[code[1]] = mod return env[_as] if _as else env[code[1]] #[S] elif type(code[0]) is S and len(code) == 1: try: return run(env.find(code[0]), env) except: print( ' ' % code[0], '') raise RuntimeError else: exps = [ run(exp, env) for exp in code] f = exps.pop(0) if not (type(f) is types.FunctionType) and not (type(f) is types.BuiltinFunctionType) and not f is type: return [f]+exps try: #print f, exps except TypeError as e:#pass arg for next(), if not a iterator print (' ') raise RuntimeError
感谢各位的阅读!关于"python如何实现一个简单的lisp"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
变量
内容
更多
篇文章
不错
实用
函数
常量
文章
条件
看吧
知识
示例
参考
帮助
有关
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库怎么存储地址数据
qq怎么找网络安全警察
近5年网络技术在汽车上的应用
浪潮服务器报警图标
宝信高性能分布式实时数据库
文件服务器账户
登入小米账号时服务器错误怎么办
软件开发前三语言
阿里云服务器改密码
广东定制软件开发哪家好
dz数据库中有哪些表
网络技术人才标准
普陀区优势软件开发服务有哪些
厦门聊天软件开发计划
数字网络技术与人性发展
服务器硬盘灯一直闪显示屏不亮
网络安全千亿元市场拐点已至
艾尔登法环联机连不上服务器
数据库设计 数据结构
一台服务器主机相当于几台电脑
如何防范网络安全翻译
云服务器安全
武汉网络安全大学是什么
软件开发项目概算编制原则
数据库的发展的3个阶段
计算机网络技术创新设计
福建众聚互联网科技信息
服务器硬盘数据安全
中小学教育和网络安全视频
表格如何引用数据库数据