Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.9K Views
|
1 Reply
|
1 Total Like
View groups...
Share
Share this post:

Writing a function that will take lists and return the list lengths?

Posted 9 years ago

This seemed like a much easier idea in my head than it was to actually implement into Mathematica ..

This is what I have so far, it is obviously wrong..

Clear[findlength, h]
findlength[x___] := h[x] /. {List -> Length[h[x]], h -> List};
findlength[{x, y, z}, {4, 2}]

I also tried something along the lines of this, which is also wrong..

Clear[findlength]
findlength[x___] := {x} /. {List -> Length[{x}]}
findlength[{c, s, d, a}, {4, 2, 5}]

The outputs for both are quite strange. I'm very new to "rules" but I feel like I am certainly on the right track.

Any suggestions or advice is always appreciated

B

POSTED BY: Brandon Davis
Posted 9 years ago

Since you want to "do the same thing to every item in a sequence of items", perhaps think "Map", but your input isn't a list so add a layer of {} to make it into a list

findlength[x___] := Map[Length, {x}]
findlength[{x, y, z}, {4, 2}]
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard