4 ms·
Speaking of Lisp and Forth, I was really stunned when I noticed that adding syntactic support for "structs" in the language is about 10 lines in Forth (https://
by lukego 9y ago
Speaking of Lisp and Forth, I was really stunned when I noticed that adding syntactic support for "structs" in the language is about 10 lines in Forth (https://github.com/openbios/openfirmware/blob/master/forth/lib/struct.fth https://github.com/openbios/openfirmware/blob/master/forth/l...) and about 2000 lines in Lisp (https://github.com/sbcl/sbcl/blob/master/src/code/defstruct.lisp https://github.com/sbcl/sbcl/blob/master/src/code/defstruct....).
Mitch Bradley wrote up an explanation of the Forth library, and lots of other cool bits of Openfirmware, at http://wiki.laptop.org/go/Forth_Lesson_18 http://wiki.laptop.org/go/Forth_Lesson_18.
- kazinator 9y agoYou could also make some ten line hack to mock up structs in Lisp over a vector. That SBCL implementation has to conform to an ANSI standard. The TXR Lisp implementation of structs (the syntactic part) is 300 Lines: http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/struct.tl http://www.kylheku.com/cgit/txr/tree/share/txr/stdlib/struct... of which the defstruct macro is about half. The underlying implementation is in C: http://www.kylheku.com/cgit/txr/tree/struct.c http://www.kylheku.com/cgit/txr/tree/struct.c, which is some 1600 lines. TXR Lisp structs are an OOP system; they support inheritance, static and instance slots, inheritance of both, late injection of methods into a hierarchy and such. There is a fairly sophisticated initialization model and even support for C++-like RAII style. Structs are real objects that know their own type. They have a print and read representation, they know what slots they have and will reject invalid accesses and so on. You don't get something for nothing; the LOC's are spent for a reason. Looks like there is a tutorial on what appear to be these same Forth structs here: http://wiki.laptop.org/go/Forth_Lesson_18 http://wiki.laptop.org/go/Forth_Lesson_18 I don't see any example of how to create a struct, how to access it, how to show that two different struct types are incompatible, how accessing a field in a struct that it doesn't have results in an error, etc. It just looks like definitions of constants denoting offsets.