MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: The Mathematica equivalent of Cons (as in Lisp)?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73929] Re: [mg73849] The Mathematica equivalent of Cons (as in Lisp)?
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Sat, 3 Mar 2007 01:06:58 -0500 (EST)
  • References: <200703021125.GAA03273@smc.vnet.net>

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


  • Prev by Date: Re: Number of digits
  • Next by Date: Re: The Mathematica equivalent of Cons (as in Lisp)?
  • Previous by thread: The Mathematica equivalent of Cons (as in Lisp)?
  • Next by thread: Re: The Mathematica equivalent of Cons (as in Lisp)?