4 ms·
Recursion, map, and fold can be used for iteration. car/cdr is just one way to access elements of a sequence.
by sedachv 16y ago
Recursion, map, and fold can be used for iteration. car/cdr is just one way to access elements of a sequence.
- KirinDave 16y agoThink about what you just said and why it's not a valid rebuttal. I know you know better. Hint: via recursion, how do you advance along a sequence in a collection-agnostic way?
- sedachv 16y agoSo now iterators mean car/cdr is "classic" so it should be present in all Lisp dialects from here to eternity?
- KirinDave 16y agoAgain, you can call them "first" and "rest" if you like. It is the ability to decompose an arbitrary sequential data structure efficiently that is important. It's not important because of any historical bias; it's important because it is incredibly useful. If you can't support first/rest in an efficient manner, it is a bad sign.
- sedachv 16y agoSo all those other programming languages that come with arrays and not linked lists are completely useless? Mildly useless? Only kind of useless? Or is it the case that it really doesn't matter?
- KirinDave 16y agoMost of them DO provide a way to do this. For example, Ruby is array based but provides a way to do this through enum. The lispish way is first/rest. Clojure, a lisp with very novel data structures, does this just fine even with associative arrays and vectors. It introduces a seq abstraction to support it. It's important when you need to write your own iteration primitives. Map, fold, etc are all well and good, but sometimes they won't do because the iteration may require some unusual steps. In your particular dialect of lisp, what does the code for writing map or fold look like? The one that doesn't copy memory egregiously, that is. And how bad must the performance of javascript linked lists be to scare you away from them so damn hard, anyways? Or perhaps, what you're supporting is a "lispish" language that isn't really something we should directly compare to lisp, in the vein of Nu?
- sedachv 16y agoI don't have a dialect of Lisp. I work on a translator from a subset of Common Lisp to JavaScript (http://common-lisp.net/project/parenscript/ http://common-lisp.net/project/parenscript/). In particular, this subset does not have car/cdr, or any other list manipulation functions. No one has ever complained about this. This is map: (defun map (fn arr) "Call FN on each element in ARR and return the returned values in a new array." ;; In newer versions of ECMAScript, this may call Array.map, too (let ((idx 0) (result (array))) (dolist (el arr) (setf (aref result idx) (fn el)) (setf idx (1+ idx))) (return result))) This is reduce: (defun reduce (func list &optional init) ;; the use of init here is actually a bit broken wrt null (let* ((acc)) (do* ((i (if init -1 0) (1+ i)) (acc (if init init (elt list 0)) (func acc (elt list i)))) ((>= i (1- (length list))))) (return acc))) What would a linked list data structure provide? Incompatibility with all other JavaScript code and a mandatory runtime library? Thanks for introducing me to Nu btw. What makes you say that it is not a Lisp?
- dahlia 16y agoLisphp's map/filter/reduce functions are implemented in the same manner that Parentscript does. This is map: http://github.com/dahlia/lisphp/blob/81849070ac7ff13948786b5dbc675806d8fd2c5c/Lisphp/Runtime/List.php#L98 http://github.com/dahlia/lisphp/blob/81849070ac7ff13948786b5... This is filter: http://github.com/dahlia/lisphp/blob/81849070ac7ff13948786b5dbc675806d8fd2c5c/Lisphp/Runtime/List.php#L129 http://github.com/dahlia/lisphp/blob/81849070ac7ff13948786b5... This is fold: http://github.com/dahlia/lisphp/blob/81849070ac7ff13948786b5dbc675806d8fd2c5c/Lisphp/Runtime/List.php#L142 http://github.com/dahlia/lisphp/blob/81849070ac7ff13948786b5... Lisphp respect the iterator protocol of PHP instead of Lisp tradition in the practical reason.