新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python實現(xiàn)Scheme
近一周,學習Scheme有點上癮,我對Scheme的興趣源自其簡單的語法,只有成對的()。學習Scheme,加上自己的python編程,能很快的抄出一個Scheme解釋器:

https://github.com/zhangyun00...
仔細看程序注釋里的論文鏈接,介紹了紹python實現(xiàn)的scheme子集。我拿來改了改,加了for和while循環(huán),break跳出循環(huán),可以使用元組、列表,字典,class定義類。
運行python ZhScheme.py
ZhScheme> (tuple 1 2)
['tuple', 1, 2]
(1, 2)
ZhScheme> (list 5 6 6 )
['list', 5, 6, 6]
[5, 6, 6]
ZhScheme> (dict (list (list 5 6) (list 56 34 )))
['dict', ['list', ['list', 5, 6], ['list', 56, 34]]]
{5: 6, 56: 34}
ZhScheme> (sin 12)
['sin', 12]-0.5365729180004349ZhScheme> (sin 34)
['sin', 34]0.5290826861200238ZhScheme> (list 3 4 5)
['list', 3, 4, 5]
(3 4 5)
ZhScheme> (list (list 3 4 5) (list 456 45))
['list', ['list', 3, 4, 5], ['list', 456, 45]]
((3 4 5) (456 45ZhScheme> (define i 4)
['define', 'i', 4]
ZhScheme> ii4ZhScheme> (while (< i 23) (begin (print i) (set i (+ i 1))(if (eq? i 12) break))
)
['while', ['<', 'i', 23], ['begin', ['print', 'i'], ['set', 'i', ['+', 'i', 1]],
['if', ['eq?', 'i', 12], 'break']]]4567891011ZhScheme> (for (set i 23) (< i 45) (set i (+ i 2)) (begin (print i) (if (eq? i3) break)))
['for', ['set', 'i', 23], ['<', 'i', 45], ['set', 'i', ['+', 'i', 2]], ['begin'
['print', 'i'], ['if', ['eq?', 'i', 43], 'break']]]2325272931333537394143ZhScheme> (env)
['env']
variables ...
+ : - : * : / : > : < : >= : <= : = : not : eq? : equal? : max : min : abs : round : car : at 0x0000cdr : at 0x0000list : at 0x000list-ref : at 0append : len : map :
print : exit : Use exit() or Ctrl-Z plus R
open : at 0x000call/cc : at 0x00number? : at 0xstring? : at 0xlist? : at 0x00struct? : at 0xdict? : at 0x00int : {}
__name__ : math
__doc__ : This module is always av
mathematical functions defined by th
__package__ :
__loader__ : , origin='built-in')acos : acosh : asin : asinh : atan : atan2 : atanh : ceil : copysign : cosh : degrees : erfc : exp : expm1 : fabs : factorial : fmod : frexp : fsum : gamma : gcd : hypot : isclose : isnan : ldexp : lgamma : log1p : log10 : log2 : modf : pow : radians : sinh : sqrt : tan : tanh : trunc : pi : 3.141592653589793e : 2.718281828459045tau : 6.283185307179586inf : infnan : nani : 43struct ... 總之,這個scheme的目標是可以調(diào)用Python里的各種常用函數(shù),以上env結(jié)果中的變量和函數(shù)都是你可以使用的。
現(xiàn)在可解釋執(zhí)行,以行為單位的文件了:
python ZhScheme.py test.s
test.s文件內(nèi)容:
(quote must write code as lines) (+ 2 5) (define i 1)i(if (< i 19) (print (+ i 1))) (while (< i 23) (begin (print i) (set i (+ i 1))(if (eq? i 12) break))) (for (set i 23) (< i 45) (set i (+ i 2)) (begin (print i) (if (eq? i 43) break))) (define f (open test.ss r)) ((. f read)) (define define 12) define (class point (list (list n 2)(list m (lambda x (* 2 x))))) (define x (point)) (. x n) ((. x m) 4)
有在ZhScheme加入靜態(tài)數(shù)據(jù)類型的想法,也就是說,除了能define x 12之外,還可以使用 int y 34,定義一個整形變量,如果set y 2.3會導致一個類型錯誤 -- 把浮點值付給了整型值,多數(shù)靜態(tài)類型語言都是這么做的。
當前名稱:創(chuàng)新互聯(lián)Python教程:Python實現(xiàn)Scheme
當前URL:http://www.dlmjj.cn/article/djjgsih.html


咨詢
建站咨詢
