224 points · by ingve · 5y ago · 68 comments
USB mass storage driver: https://github.com/froggey/Mezzano/blob/master/drivers/usb/m...
ext4 file system (read-only): https://github.com/froggey/Mezzano/blob/master/file/ext4.lis...
TCP network stack: https://github.com/froggey/Mezzano/blob/master/net/tcp.lisp
I'm not sure what I was expecting, but 110 lines of keyword arguments was a surprise.
I haven't used Lisp professionally, but find the code terribly confusing to read (mostly due to the look of it, I assume), while I can usually follow code of an unknown language reasonably well.
I used to say that proper indenting helps readability but these days I have departed from the standard indenting and do my own thing.
That said, this guide on formatting lisp is very useful IMO if you want to learn to read lisp code better:
https://link.medium.com/idMRZk93Fdb
I actually wrote it, but now I don’t follow many of its principles :D perhaps because I got a deeper understanding of it. Check it out if and see if it helps
This overloads the subtraction operator. And it makes the code very confusing to read.
And plus, Lisp also uses the hyphen as the subtraction operator.
Think about adding a bunch of numbers using infix
a+b+c+d
In prefix it’d look like
(+ a b c d)
You could as many terms in that list.
And as far as the - in indentifiers, it’s called kebab case and I find it very readable and natural to type as well. Also since identifiers are a lot more flexible than other languages you could have predicates such as empty? Rather than isEmpty and so on. Very very friendly and useful. Not trying to convince you or anything but if you give it a chance it’ll click for you.
Its the next level where it gets hard.
Time to refer to this image again I guess: https://www.thejach.com/imgs/lisp_parens.png (Though that's technically Scheme -- I find Common Lisp more readable and encouraging of smaller functions and modularity. The clean code culture of tiny functions and unit tested everything is not there, though, and a lot of stuff is done by individuals so the benefits of code review aren't often captured.)
`loop` has its own special conventions. People write macros. And so on.
I recommend giving it a shot solely for the fun of it. It's worth the payoff, even if it takes awhile.
Yes there are lots of parentheses, just like there are lots of ampersand, asterisks, curly and square brackets on C derived languages.
It's not a problem with the syntax, though. The problem is that Lisp is so powerful that programmers can and will create very high level abstractions, the language and conventions encourage it, but a side effect of this is that every part of a program has its own DSL. If in addition there is not good enough documentation or a programmer has a knack for obscure elegance over KISS, then this becomes problematic.
In a nutshell, it's perfectly possible to write clean and easy to read Lisp but not many people do it.
Edit: I forgot to say that the code from Mezzano looks like a positive example of readable and clear-cut Lisp code.
The only problems I have are when I'm trying to debug something in macro code, where it becomes too hard to intuitively follow the layers of quoting and unquoting to understand what's going on.
Expressions should be readable in a single flow. Lisp breaks that.
For example:
(+ (* 5 3) (* 3 2))
This is not easily readable because there are breaks in the "flow".You need to go like:
* `+` is going to add things together
* oh, there's a nested expression here, pause...
* `` is multiplying 5 and 3 - cool, back to the previous thing.
oh, another nested thing.* `` is multiplying 3 and 2 - back again...
done... so where were we?Here's how a readable expression would look like IMO:
(5 * 3) + (3 * 2)
This reads very naturally:* 5 times 3 - 15, ok go on...
* nested expression... hm, ok.
* 3 times 2 - 6, let's go back now...
* ah right, 15 + 6 is 21... done.
Alternatively:
5 3 * 3 2 * +
* take 5 and 3, multiply: 15* take 3 and 2, multiply: 6
* add them together: 21
Which is what FORTH does. This is almost perfect, but a little hard for humans to follow (though this is exactly what machines want to do) because you still need to keep the stack of values in your head.
I've been studying about this and came up with something in the middle:
(5 * 3) (3 * 2) +
This has the advantage that you the distance from the values in the stack to the point of reading when going left-to-right is optimal: there is no better way to represent this if your objective is to avoid intermediate values.It is similar to FORTH, but optimized for humans to read instead of machines:
* `5 * 3` is 15.
* `3 * 2` is 6.
* `+` the values, which are 15 and 6: 21.
The parens are not required if you use some alternative syntax to mark boundaries, for example:
5 * 3 >> 3 * 2 >> + ;
The `>>` is used to "pipe" the value to the next expression, while `;` is used to terminate the expression. This also has the benefit of showing the reader the direction of the flow. (5 * 3) + (3 * 2)
That's not that great, since it uses infix precedence - either explicitly (here) or implicitly. I need to read into it to understand that a sum is computed and I need to then find out the boundaries of the subexpressions. But the brain can be trained for that, just as it can be trained to read Lisp, Erlang, or even APL code.Nested lists are the base for Lisp syntax. Thus numeric code is just like other ordinary code. Lisp was developed to also do symbolic computations: computing with expressions with symbols.
Thus to read Lisp you need to learn to read syntax based on nested lists and then numeric expressions are just an application of that. Still one is trained to do math with lot's of fancy syntax - fancy syntax you won't find in normal code anyway. For example the typical graphical root notation is mostly not found in programming languages.
If I (as a Lisp programmer) had problems understanding numeric expressions, I'd reformat them:
(+ (- a b) (* 100 (sqrt y) x) (* 50 (expt x 2)))
as (+ (- a b)
(* 100 (sqrt y) x)
(* 50 (expt x 2)))
That's just another way to format a nested list.The main problem is mapping that back to existing external forms which uses mixed infix/prefix/postfix syntax.
(a - b) + (100 * y^1/2 * x) + (50 * x^2)
Lisp allows one to compute this stuff: CL-USER 18 > '#i( (a - b) + (100 * y^^(1/2) * x) + (50 * x^^2) )
(+ (- A B) (* 100 (EXPT Y (/ 1 2)) X) (* 50 (EXPT X 2)))
CL-USER 19 > (let ((*print-right-margin* 30))
(pprint *))
(+ (- A B)
(* 100 (EXPT Y (/ 1 2)) X)
(* 50 (EXPT X 2)))Previous discussions from a few years ago:
https://news.ycombinator.com/item?id=23950558
https://news.ycombinator.com/item?id=20210227
https://news.ycombinator.com/item?id=14072121
Hopefully one day it reaches that point. Ability to build one's self, and even to host one's own development, is an important sign of completeness in an operating system.
https://github.com/froggey/Mezzano/blob/master/gui/colour.li...
;; Mangle & unmangle the vector ordering so the colour matrix
;; layout makes more sense. This could be fixed up by rearranging
;; the matrix layout, but that's a bit beyond me.
(new-rgb (matrix4-vector4-multiply
matrix
(mezzano.simd:shufps original-rgb original-rgb
#4r0321)))This is beyond amazing.
How long have you been working on this for?
[edit]
I should point out that it's typical for a common-lisp implementation to have builtin tools for manipulating pointers and generating arbitrary instructions (since both of those are needed to implement the compiler anyways, and lisp compilers are typically self-hosting).