I'm trying to simulate the Length function in Mathematica v.8 to get the length of a list. Given this facts:
It's my first year using mathematica and I'm not too good at this so there's probably something (or everything) wrong with what I'm doing:
Ej1[l_List] := Module[{i, v},
v = {{}};
i = 1;
While[l != v, l = Rest[l]; i++]
Return[i]
]
l={a,b,c,d,e};
When I try to run it the loop never ends and it gives me this warnings:
Set::shape: Lists {a,b,c,d,e} and {b,c,d,e} are not the same shape. >>
Set::shape: Lists {a,b,c,d,e} and {b,c,d,e} are not the same shape. >>
Set::shape: Lists {a,b,c,d,e} and {b,c,d,e} are not the same shape. >>
General::stop: Further output of Set::shape will be suppressed during this calculation. >>
The main problems were that you were trying to modify the input variable, l
, which is not possible, and you had a missing semi-colon.
Ej1[l_List] := Module[{i = 0, v = {}, thisl},
thisl = l;
While[thisl != v, thisl = Rest[thisl]; i++];
i]
You can also use NestWhile
:
Clear[f];
f[l_List] := NestWhile[{Rest[#[[1]]], (#[[2]]) + 1} &, {l, 0},
(#[[1]] != {}) &][[2]]
This code isn't bounded by $RecursionLimit
or $IterationLimit
so it also works for very large lists. The downside is that it isn't very efficient since in every iteration step a copy is made of the remaining list. A faster way of counting elements in a list is to do something like
f2[l_List] := Fold[(# + 1) &, 0, l]
As a comparison:
list=RandomReal[1,10000];
Timing[f[list]]
(* ==> {3.35747, 10000} *)
Timing[f2[list]]
(* ==> {0.000658, 10000} *)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With