# A practical Lisp in Ruby

**URL:** <https://rubytalk.org/t/a-practical-lisp-in-ruby/44881>\
**Category:** ruby-talk\
**Created:** [1 March 2008 16:59 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881 "2008-03-01T16:59:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elliott\_Hird1](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@Elliott\_Hird1](https://rubytalk.org/u/Elliott_Hird1)\
**Post date:** [1 March 2008 16:59 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881/1 "2008-03-01T16:59:59Z")

</div>

Hi! I've been working on a Lisp interpreter in Ruby that is fully  
integrated (I mean -- fully. The aim is to have basically nothing  
specific to the interpreter and use Ruby for everything. Basic rule of  
thumb: you should be able to use it to write a Rails app with no extra  
pain than in Ruby.)

Some examples:

&nbsp;&nbsp;'(1 2 3) =\> (1 2 3)  
&nbsp;&nbsp;[1 2 3] =\> [1 2 3]  
&nbsp;&nbsp;([] empty?) =\> true  
&nbsp;&nbsp;(puts (if ([] empty?) "hello world" "oh no")) =\> hello world

Now, I hope to have some code released Real Soon Now (it really isn't  
usable enough yet; needs some reworking). But I'd like the Ruby  
community's opinion on some few things, to try and make it as Ruby-ish  
as possible. So, dump of things I've thought about:

- should # be used as a comment marker (instead of ;)? it might get in  
the way of \*something\*, lisp dialects tend to use it a lot, but #\c  
will probably be ?c, #t/#f is true/false, and #(...) is [...] so I'm  
not sure.

- should 'a be ruby's :a? that could mess up pretty printing, e.g.:  
&nbsp;&nbsp;&nbsp;&nbsp;'(a b c) =\> (:a :b :c)  
&nbsp;&nbsp;if 'a is something other than :a, what should  
&nbsp;&nbsp;it be? a subclass of Symbol that just overrides pretty-printing, i  
&nbsp;&nbsp;guess. and then :a would be ruby's :a.

- syntax for dicts? if we take the above idea, I think something like:  
&nbsp;&nbsp;&nbsp;&nbsp;{:a 2 :b 3} =\> {:a =\> 2, :b =\> 3}  
&nbsp;&nbsp;would be nice, but again there's the problem that pretty-printing loses out.  
&nbsp;&nbsp;however I don't think that Ruby's syntax should be re-used directly, because  
&nbsp;&nbsp;it's not lisp-like.

- how to call out to ruby. what happens if you pass a list to a ruby function?  
&nbsp;&nbsp;I could recursively translate them to arrays, but that's expensive and  
&nbsp;&nbsp;if you're calling out to something that wants to directly use the object  
&nbsp;&nbsp;or is one of the functions that doesn't care what type its object is,  
&nbsp;&nbsp;it might not even be expected behaviour. also, should i translate a\_b to a-b?  
&nbsp;&nbsp;vise-versa?

- on a similar note, what class is (lambda (x) x)? if a Proc I have to  
&nbsp;&nbsp;have a trampoline in the Proc, which isn't nice. a subclass of Proc?

- how to handle blocks. some ideas:  
&nbsp;&nbsp;&nbsp;&nbsp;([1 2 3] map (lambda (x) (+ x 1)))  
&nbsp;&nbsp;that means I have to do arity stuff, and presents a problem -- what if  
&nbsp;&nbsp;'map' there was variadic? are we passing a function, or a block?  
&nbsp;&nbsp;&nbsp;&nbsp;([1 2 3] map (do (x) (+ x 1)))  
&nbsp;&nbsp;here 'do' creates something internally treated as a block. again the  
&nbsp;&nbsp;variadic problem, relating to my "what if the func/method doesn't care  
&nbsp;&nbsp;about the type it gets"?  
&nbsp;&nbsp;&nbsp;&nbsp;([1 2 3] map do (x) (+ x 1))  
&nbsp;&nbsp;this is a little bit hard to read, but unambigious. of course, now you  
&nbsp;&nbsp;can't use "do" in most places. a multiline example of this:  
&nbsp;&nbsp;&nbsp;&nbsp;([0 1 2 3 4 5] map do (x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (= (% x 2) 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(+ x 5)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(- x 4)))  
&nbsp;&nbsp;this is my preferred syntax at the moment, actually. maybe i could hijack  
&nbsp;&nbsp;{...} somehow (don't really like that idea).

- (def (func ...) ...) or (def func (...) ...)? the former seems more rubyish  
&nbsp;&nbsp;to me, i guess. (def (foo) ...) instead of (def foo () ...).

- this thing:  
&nbsp;&nbsp;&nbsp;&nbsp;\>\> foo = 3  
&nbsp;&nbsp;&nbsp;&nbsp;=\> 3  
&nbsp;&nbsp;&nbsp;&nbsp;\>\> def foo; 4 end  
&nbsp;&nbsp;&nbsp;&nbsp;=\> nil  
&nbsp;&nbsp;&nbsp;&nbsp;\>\> foo  
&nbsp;&nbsp;&nbsp;&nbsp;=\> 3  
&nbsp;&nbsp;&nbsp;&nbsp;\>\> method(:foo).call  
&nbsp;&nbsp;&nbsp;&nbsp;=\> 4  
&nbsp;&nbsp;since this thing will emulate a Lisp-1 (basically), how should this be  
&nbsp;&nbsp;handled? maybe in the same way.

- and closely linked to that: since a 0-arg function is used often in Ruby  
&nbsp;&nbsp;in place of a variable (but -- mostly in a method, where this would be  
&nbsp;&nbsp;(obj foo) so it wouldn't be a problem). you'd have to do (foo) currently,  
&nbsp;&nbsp;which would differenciate it from foo.

- also related: watch this imaginary session in the Lisp REPL  
&nbsp;&nbsp;&nbsp;&nbsp;\>\> (def foo 3)  
&nbsp;&nbsp;&nbsp;&nbsp;=\> 3  
&nbsp;&nbsp;&nbsp;&nbsp;\>\> (def (foo) 4)  
&nbsp;&nbsp;&nbsp;&nbsp;=\> #\<LProc:0xF00BAR00@(lisp):2\>  
&nbsp;&nbsp;&nbsp;&nbsp;\>\> foo  
&nbsp;&nbsp;&nbsp;&nbsp;=\> 3 or #\<LProc:0xF00BAR00@(lisp):2\>?!  
&nbsp;&nbsp;&nbsp;&nbsp;\>\> (foo)  
&nbsp;&nbsp;&nbsp;&nbsp;=\> "you can't call a Fixnum!" or 4?

this clashes with Lisp-1-ness, certainly.

Anyway, that's the end of this long post for now. Feel free to ignore  
my ramblings, but to leave, here's a few code examples that don't actually work  
right now and may look completely different when they do 🙂

&nbsp;&nbsp;;; Output "I love Ruby"  
&nbsp;&nbsp;(def say "I love Ruby")  
&nbsp;&nbsp;(puts say)

&nbsp;&nbsp;;; Output "I \*LOVE\* RUBY"  
&nbsp;&nbsp;(set! (say "love") "\*love\*") ;; set! name?  
&nbsp;&nbsp;(puts (say upcase))

&nbsp;&nbsp;;; Output "I \*love\* Ruby" five times  
&nbsp;&nbsp;(5 times do (puts say))

and the second:

&nbsp;&nbsp;(class Numeric  
&nbsp;&nbsp;&nbsp;&nbsp;(def (plus x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(self + x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;; or (the same, but variadic)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(+ self x)))

Well, OK. Have a third.

&nbsp;&nbsp;(def search-engines  
&nbsp;&nbsp;&nbsp;&nbsp;(%w[Google Yahoo MSN] map do (engine)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(+ "[http://www](http://www)." (engine downcase) ".com")))

-ehird

---

<div class="post-metadata">

**Author:** ![Kyle\_Brooks](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kyle\_Brooks](https://rubytalk.org/u/Kyle_Brooks)\
**Post date:** [1 March 2008 21:00 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881/2 "2008-03-01T21:00:05Z")

</div>

This is a VERY interesting idea, but why do you want to do this?

- Kyle

> **···**
>
> On Mar 1, 11:59 am, Elliott Hird \<penguinoftheg...@googlemail.com\> wrote:
> 
> > Hi! I've been working on a Lisp interpreter in Ruby that is fully  
> > integrated (I mean -- fully. The aim is to have basically nothing  
> > specific to the interpreter and use Ruby for everything. Basic rule of  
> > thumb: you should be able to use it to write a Rails app with no extra  
> > pain than in Ruby.)
> > 
> > Some examples:
> > 
> > '(1 2 3) =\> (1 2 3)  
> > [1 2 3] =\> [1 2 3]  
> > ( empty?) =\> true  
> > (puts (if ( empty?) "hello world" "oh no")) =\> hello world
> > 
> > Now, I hope to have some code released Real Soon Now (it really isn't  
> > usable enough yet; needs some reworking). But I'd like the Ruby  
> > community's opinion on some few things, to try and make it as Ruby-ish  
> > as possible. So, dump of things I've thought about:
> > 
> > - should # be used as a comment marker (instead of ;)? it might get in  
> > the way of \*something\*, lisp dialects tend to use it a lot, but #\c  
> > will probably be ?c, #t/#f is true/false, and #(...) is [...] so I'm  
> > not sure.
> > 
> > - should 'a be ruby's :a? that could mess up pretty printing, e.g.:  
> > '(a b c) =\> (:a :b :c)  
> > if 'a is something other than :a, what should  
> > it be? a subclass of Symbol that just overrides pretty-printing, i  
> > guess. and then :a would be ruby's :a.
> > 
> > - syntax for dicts? if we take the above idea, I think something like:  
> > {:a 2 :b 3} =\> {:a =\> 2, :b =\> 3}  
> > would be nice, but again there's the problem that pretty-printing loses out.  
> > however I don't think that Ruby's syntax should be re-used directly, because  
> > it's not lisp-like.
> > 
> > - how to call out to ruby. what happens if you pass a list to a ruby function?  
> > I could recursively translate them to arrays, but that's expensive and  
> > if you're calling out to something that wants to directly use the object  
> > or is one of the functions that doesn't care what type its object is,  
> > it might not even be expected behaviour. also, should i translate a\_b to a-b?  
> > vise-versa?
> > 
> > - on a similar note, what class is (lambda (x) x)? if a Proc I have to  
> > have a trampoline in the Proc, which isn't nice. a subclass of Proc?
> > 
> > - how to handle blocks. some ideas:  
> > ([1 2 3] map (lambda (x) (+ x 1)))  
> > that means I have to do arity stuff, and presents a problem -- what if  
> > 'map' there was variadic? are we passing a function, or a block?  
> > ([1 2 3] map (do (x) (+ x 1)))  
> > here 'do' creates something internally treated as a block. again the  
> > variadic problem, relating to my "what if the func/method doesn't care  
> > about the type it gets"?  
> > ([1 2 3] map do (x) (+ x 1))  
> > this is a little bit hard to read, but unambigious. of course, now you  
> > can't use "do" in most places. a multiline example of this:  
> > ([0 1 2 3 4 5] map do (x)  
> > (if (= (% x 2) 0)  
> > (+ x 5)  
> > (- x 4)))  
> > this is my preferred syntax at the moment, actually. maybe i could hijack  
> > {...} somehow (don't really like that idea).
> > 
> > - (def (func ...) ...) or (def func (...) ...)? the former seems more rubyish  
> > to me, i guess. (def (foo) ...) instead of (def foo () ...).
> > 
> > - this thing:  
> > \>\> foo = 3  
> > =\> 3  
> > \>\> def foo; 4 end  
> > =\> nil  
> > \>\> foo  
> > =\> 3  
> > \>\> method(:foo).call  
> > =\> 4  
> > since this thing will emulate a Lisp-1 (basically), how should this be  
> > handled? maybe in the same way.
> > 
> > - and closely linked to that: since a 0-arg function is used often in Ruby  
> > in place of a variable (but -- mostly in a method, where this would be  
> > (obj foo) so it wouldn't be a problem). you'd have to do (foo) currently,  
> > which would differenciate it from foo.
> > 
> > - also related: watch this imaginary session in the Lisp REPL  
> > \>\> (def foo 3)  
> > =\> 3  
> > \>\> (def (foo) 4)  
> > =\> #\<LProc:0xF00BAR00@(lisp):2\>  
> > \>\> foo  
> > =\> 3 or #\<LProc:0xF00BAR00@(lisp):2\>?!  
> > \>\> (foo)  
> > =\> "you can't call a Fixnum!" or 4?
> > 
> > this clashes with Lisp-1-ness, certainly.
> > 
> > Anyway, that's the end of this long post for now. Feel free to ignore  
> > my ramblings, but to leave, here's a few code examples that don't actually work  
> > right now and may look completely different when they do 🙂
> > 
> > ;; Output "I love Ruby"  
> > (def say "I love Ruby")  
> > (puts say)
> > 
> > ;; Output "I \*LOVE\* RUBY"  
> > (set! (say "love") "\*love\*") ;; set! name?  
> > (puts (say upcase))
> > 
> > ;; Output "I \*love\* Ruby" five times  
> > (5 times do (puts say))
> > 
> > and the second:
> > 
> > (class Numeric  
> > (def (plus x)  
> > (self + x)  
> > ;; or (the same, but variadic)  
> > (+ self x)))
> > 
> > Well, OK. Have a third.
> > 
> > (def search-engines  
> > (%w[Google Yahoo MSN] map do (engine)  
> > (+ "[http://www](http://www)." (engine downcase) ".com")))
> > 
> > -ehird

---

<div class="post-metadata">

**Author:** ![Elliott\_Hird1](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@Elliott\_Hird1](https://rubytalk.org/u/Elliott_Hird1)\
**Post date:** [1 March 2008 21:04 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881/3 "2008-03-01T21:04:50Z")

</div>

For a start - because it can be done ;-).

For another - because I want to see what happens when you combine Lisp  
awesomeness like macros & such with Ruby's elegance and libraries. I  
am expecting an explosion of some sort.

Who knows? Maybe it'll target YARV one day, and be a first-class  
citizen, just like Ruby itself.

-ehird

> **···**
>
> On 01/03/2008, Kyle Brooks \<anotequaltob@gmail.com\> wrote:
> 
> > This is a VERY interesting idea, but why do you want to do this?
> > 
> > - Kyle

---

<div class="post-metadata">

**Author:** ![James\_Britt3](https://avatars.discourse-cdn.com/v4/letter/j/b782af/32.png) [@James\_Britt3](https://rubytalk.org/u/James_Britt3)\
**Post date:** [1 March 2008 22:09 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881/4 "2008-03-01T22:09:30Z")

</div>

Elliott Hird wrote:  
ome sort.

> Who knows? Maybe it'll target YARV one day, and be a first-class  
> citizen, just like Ruby itself.

Perhaps combined with Rubinious you really could have Lisp-like macros.

> **···**
>
> --  
> James Britt
> 
> "Discover the recipes you are using and abandon them."  
> &nbsp;&nbsp;- Brian Eno and Peter Schmidt, Oblique Strategies

---

<div class="post-metadata">

**Author:** ![Ezra\_Zygmuntowicz](https://avatars.discourse-cdn.com/v4/letter/e/958977/32.png) [@Ezra\_Zygmuntowicz](https://rubytalk.org/u/Ezra_Zygmuntowicz)\
**Post date:** [1 March 2008 22:28 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881/5 "2008-03-01T22:28:02Z")

</div>

Elliot-

&nbsp;&nbsp;You may want to look at these two projects:

[http://chaosforge.org/taw/rlisp/](http://chaosforge.org/taw/rlisp/)

[http://bus-scheme.rubyforge.org/](http://bus-scheme.rubyforge.org/)

&nbsp;&nbsp;rlisp is a lisp to ruby compiler with nice lisp -\> ruby integration that solves most of the problems you are asking about. Bus Scheme is a scheme written in ruby with ruby integration.

&nbsp;&nbsp;Rlisp is more complete but I prefer scheme to lisp myself so bus scheme is pretty interesting, bus scheme already runs on rubinius too 😉

Cheers-  
- Ezra Zygmuntowicz  
-- Founder & Software Architect  
-- ezra@engineyard.com  
-- EngineYard.com

> **···**
>
> On Mar 1, 2008, at 1:04 PM, Elliott Hird wrote:
> 
> > On 01/03/2008, Kyle Brooks \<anotequaltob@gmail.com\> wrote:
> > 
> > > This is a VERY interesting idea, but why do you want to do this?
> > > 
> > > - Kyle
> > 
> > For a start - because it can be done ;-).
> > 
> > For another - because I want to see what happens when you combine Lisp  
> > awesomeness like macros & such with Ruby's elegance and libraries. I  
> > am expecting an explosion of some sort.
> > 
> > Who knows? Maybe it'll target YARV one day, and be a first-class  
> > citizen, just like Ruby itself.
> > 
> > -ehird

---

<div class="post-metadata">

**Author:** ![Elliott\_Hird1](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@Elliott\_Hird1](https://rubytalk.org/u/Elliott_Hird1)\
**Post date:** [1 March 2008 22:22 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881/6 "2008-03-01T22:22:59Z")

</div>

You mean, in Ruby itself? That would be crazy awesome. 🙂

But they will probably be ugly. Ruby parse trees aren't as nice as Lisp ones 😉

> **···**
>
> On 01/03/2008, James Britt \<james.britt@gmail.com\> wrote:
> 
> > Perhaps combined with Rubinious you really could have Lisp-like macros.

---

<div class="post-metadata">

**Author:** ![Elliott\_Hird1](https://avatars.discourse-cdn.com/v4/letter/e/d9b06d/32.png) [@Elliott\_Hird1](https://rubytalk.org/u/Elliott_Hird1)\
**Post date:** [1 March 2008 23:25 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881/7 "2008-03-01T23:25:08Z")

</div>

> Elliot

t 🙂

> -
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You may want to look at these two projects:
> 
> [http://chaosforge.org/taw/rlisp/](http://chaosforge.org/taw/rlisp/)
> 
> [http://bus-scheme.rubyforge.org/](http://bus-scheme.rubyforge.org/)
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rlisp is a lisp to ruby compiler with nice lisp -\> ruby integration  
> that solves most of the problems you are asking about. Bus Scheme is  
> a scheme written in ruby with ruby integration.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rlisp is more complete but I prefer scheme to lisp myself so bus  
> scheme is pretty interesting, bus scheme already runs on rubinius too 😉

I've seen rlisp briefly and Bus Scheme too. Bus Scheme isn't really  
that integrated; more Scheme than Ruby, IMO. RLisp is quite nice but  
not quite perfect, IMO -- the philosophy's quite different.

My idea is a Ruby Lisp, really, not a Lisp integrated with Ruby. 🙂

-ehird

> **···**
>
> On 01/03/2008, Ezra Zygmuntowicz \<ezmobius@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [2 March 2008 04:29 UTC](https://rubytalk.org/t/a-practical-lisp-in-ruby/44881/8 "2008-03-02T04:29:59Z")

</div>

Bummer. I work from my home typically, so I don't think I can  
contribute to Bus Scheme 😉

> **···**
>
> On Mar 1, 5:28 pm, Ezra Zygmuntowicz \<ezmob...@gmail.com\> wrote:
> 
> > On Mar 1, 2008, at 1:04 PM, Elliott Hird wrote:
> > 
> > \> On 01/03/2008, Kyle Brooks \<anotequal...@gmail.com\> wrote:  
> > \>\> This is a VERY interesting idea, but why do you want to do this?
> > 
> > \>\> - Kyle
> > 
> > \> For a start - because it can be done ;-).
> > 
> > \> For another - because I want to see what happens when you combine Lisp  
> > \> awesomeness like macros & such with Ruby's elegance and libraries. I  
> > \> am expecting an explosion of some sort.
> > 
> > \> Who knows? Maybe it'll target YARV one day, and be a first-class  
> > \> citizen, just like Ruby itself.
> > 
> > \> -ehird
> > 
> > Elliot-
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;You may want to look at these two projects:
> > 
> > [http://chaosforge.org/taw/rlisp/](http://chaosforge.org/taw/rlisp/)
> > 
> > [http://bus-scheme.rubyforge.org/](http://bus-scheme.rubyforge.org/)
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rlisp is a lisp to ruby compiler with nice lisp -\> ruby integration  
> > that solves most of the problems you are asking about. Bus Scheme is  
> > a scheme written in ruby with ruby integration.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rlisp is more complete but I prefer scheme to lisp myself so bus  
> > scheme is pretty interesting, bus scheme already runs on rubinius too 😉
