Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Simulate Length function' topicStudent Support Forum > General > "Simulate Length function"

Help | Reply To Topic
Author Comment/Response
carlos
10/22/11 10:32am

[Sounds like homework, but it is a Mathematica question. - Forum Moderator (hint: call by name vs. call by value)]


Hi! I'm trying to simulate the length function to get the length of a list. I'm supossed to use:

- Empty list is represented as {}
- l = Rest[l]
- a while loop

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]
]

When I try to run it the loop never ends and it gives me this warning:

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. >>

Please help! Thank you ^^u

URL: ,
Help | Reply To Topic