5 ms·
That is not exactly an apples-to-apples comparison. You've taken a very simple C function and compared it to Lisp code written in two different ways to take ad
by lisper 7y ago
That is not exactly an apples-to-apples comparison. You've taken a very simple C function and compared it to Lisp code written in two different ways to take advantage of features available only in specific compilers. Equivalent C code would be chock-full of #ifdefs and look just as ugly. I could produce an equally contrived example where to do the same thing as two lines of Lisp would require pages and pages of C.
Second, these code snippets are not comparable in their functionality. The Lisp code generates the crc table, the C code assumes it has already been done. If you wanted to add bounds checking to the C code you would have to make major changes. If you wanted to add bounds checking to the Lisp code, all you would have to do is change the optimization settings. (This is one of the reasons that today's computing world is a swiss-cheese of security holes.)
Third, there are any number of ways to make the Lisp code look significantly prettier. Comparable code would look something like:
(defun crc (crc l)
(dolist (item l)
(setf crc (logxor (aref crc-table (logand 255 (logxor crc item))) (ash c 8))))
crc)
It's even easy to embed an infix parser [1] in Lisp so that you could write:
(defun crc (crc l)
(dolist (item l)
(setf crc infix(crc_table[(crc ^ item) & #xff] ^ (crc >> 8)))
crc)
if you wanted do. You could also embed all this in a little DSL so that the resulting semantics and even the generated code would be exactly the same as the C code.
Fourth, most Lisp systems have a foreign function interface, so if you want to you can call C functions directly and so get all the benefits of C (such as they are) from within Lisp.
---
[1] http://www.cs.cmu.edu/Groups/AI/util/lang/lisp/code/syntax/parcil/parcil.cl http://www.cs.cmu.edu/Groups/AI/util/lang/lisp/code/syntax/p...
- ncmncm 7y agoYes, I can call C from Lisp. Or just not bother with the Lisp at all. It's an easy choice.
- kazinator 7y agoThe wide-spread production practice of calling C libraries from dynamic (and other) languages is pretty much eating the world. The naysayers are being tripped over and trampled by throngs of doers. There are excellent reasons not to write the whole darn program in C, just because it happens to need a SHA256, CRC, access to graphics, or some number crunching or whatever.
- jstimpfle 7y ago> The Lisp code generates the crc table I don't think it's generating a table, just calling generate-crc32-table. Let's remove that line and the difference is still ridiculous (even if you look at only one of the two implementations). > Comparable code would look something like: Can't see any machine types here. > (setf crc infix(crc_table[(crc ^ item) & #xff] ^ (crc >> 8))) You really don't see how ridiculous that is? You are chasing exactly the target that is unholy in your eyes. Except, you will never reach it (the lisp version is really ugly, probably has really bad tooling, poor error messages, poor standardization and mindshare, and so on)...). While you could just use the right tool for the job. > and compared it to Lisp code written in two different ways question is, WHY was the Lisp code written in two different ways, but not the C code? Go figure. > If you wanted to add bounds checking to the C code you would have to make major changes. If you want automated and perfect bounds checking, you need a few more flavours of GC/OOP (because you need some automated notion where the "length" field is, and that length field needs to be authoritative), and you will end up with less modular, less portable code (because you can't just pass sub-arrays / smaller lengths, but always need to pass object handles. Can't support sub-allocators, etc). It's a tradeoff. If you go for automated bounds checking, that's a total different game. Personally I don't like to do GC, and I like to manually insert explicit "bound" checks at strategic locations. But YMMV.
- lisper 7y ago> I don't think it's generating a table, just calling generate-crc32-table. Well, yeah. What do you suppose a function called generate-crc32-table could plausibly do? > Let's remove that line and the difference is still ridiculous (even if you look at only one of the two implementations). OK, lets' do that experiment: (defun update-crc32-checksum (crc buffer end) (declare (type (unsigned-byte 32) crc) (type (simple-array (unsigned-byte 8) (*)) buffer) (type fixnum end) (optimize (speed 3) (debug 0) (space 0) (safety 0))) (let ((cur (logxor crc #xffffffff))) (declare (type (simple-array (unsigned-byte 32) (256)) table) (type (unsigned-byte 32) cur)) (dotimes (i end) (declare (type fixnum i)) (let ((index (logand #xff (logxor cur (aref buffer i))))) (declare (type (unsigned-byte 8) index)) (setq cur (logxor (aref table index) (ash cur -8))))) (logxor cur #xffffffff))) That's 14 LOC, and 8 of those are declarations. For an example that you cherry-picked to be a perfect match for the kind of task that C was designed to do. > You really don't see how ridiculous that is? You are chasing exactly the target that is unholy in your eyes. You are completely missing the point, which was: > It's not that they are without flaws, it's that it is a lot easier to fix/ignore/work-around the flaws than in other languages, where the flaws are unchangeable (except by the standards committee) and constantly in your face. And specifically, you were responding to this: > I'll bet that for any flaw you name, I'll be able to show you an easy way to work around it. You haven't actually done that, you've just exhibited some nice clean C code and some ugly CL code. I was just showing you some of the possible ways that the Lisp code could be improved. I threw the infix in there not because I was advocating it, just to show you that it was possible. So what exactly is it about that code that makes it "ridiculous" in your eyes? Whatever it is, I'll bet I can fix it with very little effort. For example: > Can't see any machine types here. That's right, I left them out (because you can do that in CL if you want). Is that what you want to see, a cleaner syntax for type declarations? Because that's trivial, a few DEFTYPEs and a two-line macro. > WHY was the Lisp code written in two different ways, but not the C code? Because the Lisp code was trying to leverage system-specific optimizations while remaining portable. Do you seriously doubt that I could find an example of ugly #idef-laden C code that does the same? > If you want automated and perfect bounds checking, you need [a bunch of stuff] That's right. And why is it that you don't see that in C? Because adding that to C is hard. Really really hard. So hard that in 47 years it still hasn't been done in any standardized way. Yes, it's true that CL is not a particularly good systems programming language out of the box. But turning Lisp into a good systems programming language (if that's what you want) is relatively easy, almost an elementary exercise. And it can be done at the user level. No need to wait for a language design committee. You want to know what is ridiculous? A language that after 47 years still doesn't have a linked list as part of its standard library because the language design makes it impossible.