PyShen tutorial

[PyShen]

Installing PyShen

$ git clone git://github.com/yminer/pyshen.git $ cd pyshen $ ipython

Loading environment

An ipython extension is available in file pyshen/smagic.py. Load it with the command below.

With extension loaded, lines prefixed with %S are executed in the PyShen interpreter.

A multiple-line command must be prefixed by %%S.

In [1]:
%load_ext pyshen.smagic
Shen 2010, copyright (C) 2010 Mark Tarver
released under the Shen license
www.shenlanguage.org, version 11
running under Python, implementation: pyshen
port 0.135 ported by Matthieu Lagacherie and Yannick Drant

Basic operations and symbols

In [2]:
%S hello
hello
Out[2]:
hello
In [10]:
%S (+ 42 51)
93.0
Out[10]:
93.0
In [12]:
%S(and true false)
Out[12]:
False

Lambda and partial applications

In [13]:
%S (/. X (* 2 X))
<function <lambda> at 0x35dc5f0>
Out[13]:
<function pyshen.<lambda>>
In [14]:
%S ((/. X (* 2 X)) 5)
10
Out[14]:
10
In [16]:
%S (+ 1)
<functools.partial object at 0x3604050>
Out[16]:
<functools.partial at 0x3604050>
In [17]:
%S ((+ 1) 5)
6
Out[17]:
6

Define a simple recursive function.

In [6]:
%%S
(define factorial
 0 -> 1
 X -> (*  X (factorial (- X 1))))
factorial
Out[6]:
factorial
In [8]:
%S (factorial 6)
720
Out[8]:
720