[Date Index] [Thread Index] [Author Index]
Re: The Mathematica equivalent of Cons (as in Lisp)?
On Mar 2, 2007, at 6:25 AM, wooks wrote: > For the benefit of someone with a Scheme background. > > Would it be Prepend. Prepend takes its arguments in the opposite order of cons, but I suppose that's the closest analog, you could always make: cons[x_,l_List]:=Prepend[l,x] > Are there any pitfalls to be wary of? Note that Mathematica lists should not be thought of as implicit cons. They behave more like arrays with respect to algorithmic complexity: indexing into a list is an O(1) operation, and when lists are composed entirely of elements of one atomic type they consume about as much memory as an array of the same type not a linked list. This also leads to consing being inefficient, it's an O(n) operation in Mathematica and it's more efficient to construct lists by mapping functions over existing lists of the right size or using built-ins like Array where possible. Regards, Ssezi