Skip to main content

Common Lisp for Python

Project description

The library cl4py (pronounce as clappy) allows Python programs to call Common Lisp libraries.

Motivation

You are a Python programmer, but you want access to some of the powerful features of Lisp, for example to compile code at run time? Or you want to use some awesome Lisp libraries? In that case, cl4py is here to help you.

Tutorial

You can start any number of Lisp subprocesses within Python, like this:

>>> import cl4py
>>> lisp = cl4py.Lisp()

Of course, this requires you have some Lisp installed. If not, use something like apt install sbcl, pacman -S sbcl or brew install sbcl to correct this deficiency. Once you have a running Lisp process, you can execute Lisp code on it:

>>> lisp.eval("(+ 2 3)")
5

>>> add = lisp.eval("(function +)")
>>> add(1, 2, 3, 4)
10

>>> div = lisp.eval("(function /)")
>>> div(2, 4)
Fraction(1, 2)

Some Lisp data structures have no direct equivalent in Python, most notably, cons cells.

>>> lisp.eval("(cons 1 2)")
cl4py.Cons(1, 2)

>>> lst = lisp.eval("(cons 1 (cons 2 nil))")
cl4py.List(1, 2)
>>> lst.car
1
>>> lst.cdr
cl4py.List(2) # an abbreviation for cl4py.Cons(2, None)

# conversion works vice versa, too:
>>> lisp.eval(cl4py.List('+', 2, 9))
11

It soon becomes clumsy to look up individual Lisp functions by name. Instead, it is possible to convert entire Lisp packages to Python modules, like this:

>>> cl = lisp.find_package('CL')
>>> cl.oppd(5)
True

>>> cl.cons(5, None)
cl4py.List(5)

>>> cl.remove(5, [1, 5, 2, 7, 5, 9])
[1, 2, 3, 4]

# Higher-order functions work, too!
>>> cl.mapcar(cl.constantly(4), Quote(1, 2, 3))
cl4py.List(4, 4, 4)

For convenience, Python strings are not treated as Lisp strings, but inserted literally into the evaluated Lisp code. This means that in order to actually send a string to Lisp, it must be wrapped into a cl4py.String, like this:

>>> lisp.eval(cl4py.String("foo"))
cl4py.String("foo")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cl4py-1.1.1.tar.gz (8.9 kB view details)

Uploaded Source

File details

Details for the file cl4py-1.1.1.tar.gz.

File metadata

  • Download URL: cl4py-1.1.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for cl4py-1.1.1.tar.gz
Algorithm Hash digest
SHA256 68da05bb3e7f6dc7e9e6b4d05023ef2dc3609bc73274472a9884d61b2107dbd6
MD5 21fc6868f64b48e2cf141c9b607e5b5a
BLAKE2b-256 fabbb0a91204f35fd2faeb7a812a3dfc9b6bd2a3aceb88dd3671b0e158f64094

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page