æå°éLisp
Web-DB Vol.60ãæå
ã«ãã人ã¯ãã²Lispã®ç« ãè¦ã¦ã¿ã¦ãã ããããLispã®ã³ã¼ããã»ã¨ãã©æ§ææ¨ã¨åãã§ãã£ã¦ããã®æ§ææ¨ãå®è¡ããã¨ãã«ã©ããªãããã¨ãã話ã
ä¸ã®ã³ã¼ããè¦ãã°ãããã¨ãããLispã®ã³ã¼ãã§ã¯ãªã¹ãã®åºåãã«ã«ã³ãããªããã©ãPythonã«ã¯ããã£ã¦ã¨ããã¨ãLispã®ã¢ãã ã¯Pythonã§ã¯æååã§è¡¨ç¾ãã¦ããã®ã§å¼ç¨ç¬¦ããã£ã¤ãã¦ããããããããã®éããããªããã§ããã®æ§ææ¨ããããããã®ã³ã¼ãã§å®è¡åºæ¥ããã¨ã
ã§ã¾ããããã¯ã³ã¼ãã«ç¹æ®å½¢å¼ããªãããå ¨é¨ãåè¦ç´ ãevalãã¦ããè¨ç®ããããã¨ããæµãã«ãªã£ã¦ããã®ã ãã©ããããããifã¨ãå®ç¾ã§ããªãã¦å°ãããï¼ã©ãããã°ããã¨æãï¼ã¨ããã®ã¯æ©ä¼ãããã°ã¾ãä»åº¦æ¸ãã
# -*- encoding: utf-8 -*- """ mini-Lisp Lisp解説ã®ããã®ãLisp風æ§ææ¨ãåãåã£ã¦ãããå®è¡ããã³ã¼ã """ # (print (* 3 (+ 1 2))) ã«ç¸å½ ast = ("print", ("*", 3, ("+", 1, 2))) def eval(ast): if not isinstance(ast, tuple): return ast # already evaled func = ast[0] if func == "*": return eval(ast[1]) * eval(ast[2]) elif func == "+": return eval(ast[1]) + eval(ast[2]) elif func == "print": print eval(ast[1]) return None eval(ast)