Message Boards Message Boards

0
|
6925 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Using list of elements as arguments for a function?

Posted 4 years ago

If you have a list of numbers

v = {3, 9/2, 4}

the command GCD will not work on a list and returns

{3, 9/2, 4}

but

GCD[3, 9/2, 4]

returns an answer of 1/2 which is correct. How do you 'unlist' a list so that GCD will work with a list?

POSTED BY: Jesse Sheinwald
4 Replies
Posted 2 years ago

All the following methods do the trick:

In[504]:= A = {3, 9/2, 4};
GCD @@ A
Apply[GCD, A]
GCD[A /. List -> Sequence]
GCD[Sequence @@ A]

Out[505]= 1/2

Out[506]= 1/2

Out[507]= 1/2

Out[508]= 1/2

For some related discussions, see here.

POSTED BY: Hongyi Zhao
Posted 4 years ago

You can apply Sequence[] to turn a list into arguments:

GCD[Apply[Sequence, {3, 9/2, 4}]]

or

GCD[Sequence @@ {3, 9/2, 4}]
POSTED BY: Mike Besso

Thanks.

POSTED BY: Jesse Sheinwald
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract