Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Well. Obviously. The syntax is much, much more complicated.

But if you were to adopt a ruby syntax subset, enough of the semantics are similar enough to not pose a significant problem.

How easy is lisp to implement in a language without first class functions?



Ruby has a lot of special cases (the Proc versus block dichotomy, implicit conversions, etc). Scheme has shockingly clean semantics in comparison.

Implementing Lisp in a language without first class functions is quite easy. Closure conversion is a simple process:

1) Find every variable in the parent lambda that is both accessed in a child lambda and assigned-to outside the child lambda; demote those variables to heap-allocated cells and rewrite references to those variables to indirect through the cell.

2) Note every free variable in the child lambda, and rewrite all references to those variables to indirect through an environment vector passed in through a hidden argument.

3) Generate each lambda as a top-level function, and generate each lambda expression as the allocation of a callable object referencing the top-level function and a heap-allocated environment vector; at each allocation site, generate code to copy the free variables into the environment vector.

This is literally the entire algorithm. It can be implemented in a couple of hundred lines of C.


Whilst not supporting first class functions, C is pretty easy on you in that respect. I reckon there are suitably small lisp implementations in C (less than 100 lines). It all really depends if your lisp implementation uses the language's VM or a VM layer over the top.


Which class are functions in C? It was my impression that the only thing missing were anonymous functions, but there are pointers to functions if I'm not mistaken.


> Which class are functions in C?

Economy class. :-)

In general, how do you write functions that return functions without some form of anonymous function? For instance, how do you write a function that takes an int a and returns a function that takes an int parameter and returns non-zero if and only if that parameter is greater than a?


You can use function pointers. Functions in C are not first class (they are not a type) but you can emulate higher order functions crudely using function pointers.

It gets ugly pretty quick though.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: