hazlin on Nostr: So, I've been reading in "On Lisp", and there has been a lot I didn't expect, like a ...
So, I've been reading in "On Lisp", and there has been a lot I didn't expect, like a focus on writing the most performant code.
One of the proposed utility functions seems to be a faulty concept.
It says don't use "(> (length x) (length y))" to determine which list is longer, instead compare the lists incrementally with recursion so length doesn't have to reach the end of both lists.
01 (defun simple-longer (x y)
02 (> (length x) (length y)))
CL-USER> (simple-longer '(1 2 3) '(1 2 3 4))
NIL
CL-USER> (simple-longer '(1 2 3 4 5) '(1 2 3 4))
T
The proposed code is
01 (defun OnLisp-longer (x y)
02 (labels ((compare (x y)
03 (and (consp x)
04 (or (null y)
05 (compare (rest x) (rest y))))))
06 (if (and (listp x) (listp y))
07 (compare x y)
08 (> (length x) (length y)))))
CL-USER> (onlisp-longer '(1 2 3) '(1 2 3 4))
NIL
CL-USER> (onlisp-longer '(1 2 3 4 5) '(1 2 3 4))
T
BUT! Unless I've seen wrong, line 08 of OnLisp-longer undoes any saved work, and traverses the remainder of either x or y. This proposed more efficient utility function, does more work overall.
Published at
2023-05-12 03:11:04Event JSON
{
"id": "8d309d0e43c0043d21b5d91a86c837ec755f6080d1b49e527183f02f1a6cca83",
"pubkey": "9e8a3ee2cfda5a10c16c1f23084d1073062010510083f02ef5553430090f0a63",
"created_at": 1683861064,
"kind": 1,
"tags": [
[
"mostr",
"https://poa.st/objects/f3b691e6-1617-418a-b92c-7574132b4ff3"
]
],
"content": "So, I've been reading in \"On Lisp\", and there has been a lot I didn't expect, like a focus on writing the most performant code.\n\nOne of the proposed utility functions seems to be a faulty concept.\n\nIt says don't use \"(\u003e (length x) (length y))\" to determine which list is longer, instead compare the lists incrementally with recursion so length doesn't have to reach the end of both lists.\n\n01 (defun simple-longer (x y)\n02 (\u003e (length x) (length y)))\n\nCL-USER\u003e (simple-longer '(1 2 3) '(1 2 3 4))\nNIL\nCL-USER\u003e (simple-longer '(1 2 3 4 5) '(1 2 3 4))\nT\n\nThe proposed code is\n01 (defun OnLisp-longer (x y)\n02 (labels ((compare (x y)\n03 (and (consp x)\n04 (or (null y)\n05 (compare (rest x) (rest y))))))\n06 (if (and (listp x) (listp y))\n07 (compare x y)\n08 (\u003e (length x) (length y)))))\n\nCL-USER\u003e (onlisp-longer '(1 2 3) '(1 2 3 4))\nNIL\nCL-USER\u003e (onlisp-longer '(1 2 3 4 5) '(1 2 3 4))\nT\n\nBUT! Unless I've seen wrong, line 08 of OnLisp-longer undoes any saved work, and traverses the remainder of either x or y. This proposed more efficient utility function, does more work overall.",
"sig": "0be96e413ce99170f4fe6ac3afb1b2d239161df418e172a9d8110b2e1608773df196e009aefae2eaf4f82f44e267cc16c195bdd9a2b338298c291358432930bb"
}