
Hang tight folks, this is a trippy one:
As many have noted in my most recent
post, I ve taken a shine to lisps hosted in Python.
While
clojure-py is badass, it s not fit for using
from python. I hit some problems in the way it handles atomic variable access (well atomic , it s actually not ;) ), so I ve decided to give it a shot myself.
That s right. I wrote a Lisp.
A very basic, unmacroed lisp, but a lisp.
So, let s take a look a
trivial hy file pair
The first file (
really_lisp.hy), is as follows:
; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp
; Copyright (c) Paul Tagliamonte, in sofar as any of this is at all
; copyrightable.
(def hello "World!")
(def square (fn [arg]
(* arg arg)))
(def factorial (fn [n]
(if (<= n 1)
1
(* n (factorial (- n 1))))))
(def fib (fn [n]
(if (< n 2)
n
(+ (fib (- n 1)) (fib (- n 2))))))
And, the example Python script
example.py is as follows:
import hy.lang.importer
import really_lisp
print really_lisp.hello
# "World!"
print really_lisp.square(4)
# 16
print really_lisp.factorial(10)
# 3628800
print [really_lisp.fib(x) for x in range(1, 20)]
# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
# 233, 377, 610, 987, 1597, 2584, 4181]
Please note two awesome things firstly, we re importing a non-python script into a Python module. Awesome. Secondly, we re able to access symbols natively, as well as functions.
Let s also demonstrate the other way (
just hython this time):
; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp
; Copyright (c) Paul Tagliamonte, in sofar as any of this is at all
; copyrightable.
(puts "Hello, World!")
; "Hello, World!"
(import ["os"
"sys"])
(puts (os.path.abspath "."))
; "/home/tag/tmp"
(puts sys.argv)
; ['/home/tag/.virtualenvs/hy/bin/hython', 'test.hy']
(sys.stdout.write "Fuggn' test\n")
; "Fuggn' test"
Naturally, not to be outdone, I decided to amp it up.
Django in Lisp
While insane, the big stuff looks like this:
I ve replaced
urls.py with
urls.hy:
; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp
(import ["django.conf.urls"])
(def urlpatterns (django.conf.urls.patterns ""
(django.conf.urls.url "^$" "djlisp.hellisp.views.home")
))
And,
views.py with
views.hy:
; vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 filetype=lisp
(import ["django.shortcuts"])
(def home (fn [request]
(django.shortcuts.render_to_response "home.html" )))
And lastly, the
settings.hy file.
I m not going to paste it because it s huge.
But, it works!