4 ms·
I implemented cdr just to respect a Lisp tradition. Lisphp's Lisphp_List type isn't a linked list but a subtype of ArrayObject, which supports random access. So
by dahlia 16y ago
I implemented cdr just to respect a Lisp tradition. Lisphp's Lisphp_List type isn't a linked list but a subtype of ArrayObject, which supports random access. So instead of doing:
(car (cdr (cdr (cdr list))))
you should just do:
(at list 3)
- jacquesm 16y agoYes, that's what I thought, and I don't see a real way around it. The bigger problem is in code that consumes the lists item by item, every time an extra copy of the remainder of the list will be made, there it is much harder to work around this. Very neat code by the way, thanks a ton for making this. I'll be playing around with it for a bit, I've written a chunk of code for a small site a while ago in a 'functional' PHP style (as in, no assignments except at the highest level), I'll see if I can convert that project to your lisp dialect and get it to work.
- Scriptor 16y agoIt could be mitigated in some cases as long as the garbage collector is working fine. For example, in a tail-recursive function that uses a first/rest pattern to go through an array a new copy would normally be allocated with each recurse. If LispHP can do some form of tail recursion elimination, it will theoretically take up constant memory and hopefully the GC takes care of the practical part.
- sedachv 16y agoSince Lisphp is a new dialect, you don't need to worry about backward-compatibility. You should get rid of car/cdr entirely. There is no inherent reason for using linked lists for anything - even s-expressions. For example, no one has ever complained about the lack of car/cdr in Parenscript (although to be fair, Parenscript is not homoiconic, just a translator from CL to JS). Having a good backquote-comma templating syntax is more valuable. You should even get rid of a custom type for Lisphp sequences and use PHP arrays directly. Ditto all other datatypes. Then if you use PHP's calling convention you should be able to call PHP code directly without needing USE and FROM, and more importantly PHP code can do the same to Lisphp with minimal pain. That way writing Lisphp libraries for others to use becomes viable.
- jacquesm 16y agoI think it's actually quite neat that it tries to stay close to the lisp code that you see in examples and books about lisp.
- sedachv 16y agoWhat's the point of sacrificing something like platform integration for car/cdr? Anyone who is going to want to use Lisphp for real work is not going to have a problem writing their code to use arrays. Anyone who thinks that car/cdr is more valuable than platform integration is not going to use Lisphp for real work. And everyone else will come by and say "Lisphp has those weird car/cdr and it can't even work with PHP libraries without glue code, Lisp is a toy."
- KirinDave 16y agoCall them what you want. First/car & rest/cdr are the classic tool for writing code that iterates across datasets.
- sedachv 16y agoRecursion, map, and fold can be used for iteration. car/cdr is just one way to access elements of a sequence.