In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how python implements a simple lisp. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Example:
# 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 cartons = cdr: return True return [] def and2 (car, cdr): # implement an and with lisp if cargoes = [] and cdrums = []: 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 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') Variable if type (code) is S: try: return env.find (code) except: print (''% code '') raise RuntimeError # constant # for empty list and no list elif code== [] or type (code) is not list: return code # sets the value of the variable elif code [0] is seti: if type (code [1]) is not S: print (""% code [1],'') raise RuntimeError # variable reference For quote exp elif code [0] is quote: # Special function, for def, fun, let elif code [0] is define: env [code [1]] = run (code [2], env) # if code [1] = 'inner' or code [1] =' estrangement: # print'- define--', code [1], code [2] Env return env [code [1]] # executes the function body 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: # condition, for cond, [cond [p1Magne e1], [p2Magee 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 an iterator print ('') raise RuntimeError Thank you for reading! This is the end of this article on "how to achieve a simple lisp in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.