这篇文章小编给大家分享一下在Python实现Scheme的方法,感兴趣的小伙伴一定要耐心阅读完这篇文章。

Python如何实现Scheme?这篇文章看了就知道

我们运行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', [' (for (set i 23) (< i 45) (set i (+ i 2)) (begin (print i) (if (eq? i3) break)))
 ['for', ['set', 'i', 23], [' (env)
 ['env']
 variables ...
 
 +  :  -  :  *  :  /  :  >  :  <  :  >=  :  <=  :  =  :  not  :  eq?  :  equal?  :  max  
 :  min  :  abs  :  round  :  car  :  <function  at 0x0000cdr  :  <function  at 0x0000list  : 
  <function  at 0x000list-ref  :  <function  at 0append  :  len  :  map  :  print  :  exit  :  
  Use exit() or Ctrl-Z plus R
 open  :  <function  at 0x000call/cc  :  <function callcc at 0x00procedure?  :  <built-in function 
 canull?  :  <function  at 0x00number?  :  <function  at 0xstring?  :  <function  at 0xlist?  : 
  <function  at 0x00struct?  :  <function  at 0xdict?  :  <function  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  :  <built-in function copycos  :  cosh  :  degrees  :  <built-in function
   degreerf  :  erfc  :  exp  :  expm1  :  fabs  :  factorial  :  <built-in function facfloor  :  
   fmod  :  frexp  :  fsum  :  gamma  :  gcd  :  hypot  :  isclose  :  <built-in function 
   iscloisfinite  :  <built-in function isfiisinf  :  isnan  :  ldexp  :  lgamma  : 
    <built-in function lgammalog  :  log1p  :  log10  :  log2  :  modf  :  pow  :  radians  
    :  <built-in function radiasin  :  sinh  :  sqrt  :  tan  :  tanh  :  trunc  : 
     pi  :  3.141592653589793e  :  2.718281828459045tau  :  6.283185307179586inf  :  infnan  
     :  nani  :  43struct ...

反正scheme目标它是能够调用python里面的各种常用函数,以上env结果中的函数和变量都是我们可以正常使用的。现在我们可以执行以行为单位的文件了:

python ZhScheme.py test.s

test.s文件内容:

(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加入静态数据类型的想法,就是除了能define x 12之外,还可以使用 int y 34,定义一个整形变量,如果set y 2.3会导致一个类型错误 -- 把浮点值付给了整型值,多数静态类型语言都是这么做的。

以上就是小编给大家带来的在Python实现Scheme的方法,希望大家通过阅读小编的文章之后能够有所收获!如果大家觉得小编的文章不错的话,可以多多分享给有需要的人。

更多python相关文章请访问分类:python

【版权声明】本文图文出自大盘站@dapan.cc,转载请注明出处!