Group Abstract Group Abstract

Message Boards Message Boards

1
|
7.6K Views
|
7 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Changing Head of an Expression

Posted 11 years ago

Hello everyone,

I'm trying to define a Function, which changes the Head of an Expression to be List. i.e.: $$ f[a,b,c,d,e] \mapsto List[a,b,c,d,e] $$ I found a way to change Plus into List, but it's quite long:

chang[var__] := ToExpression[ StringReplacePart[ToString[FullForm[var]], "List", {1, 4(Plus has 4 letters)}]]

Can you think of doing this in a more general & elegant way for arbitrary Head f? Thank you for your reply,

Best regards, Josip

POSTED BY: Josip Turkalj
7 Replies
Posted 11 years ago

Well thank you for your help!!

I think I made this FullForm-mistake somehow also in the pre-fix version already, too. Without FullForm it works well in one line

Apply[List, Expand[Product[1 + C[i], {i, 1, 2}]]]
POSTED BY: Josip Turkalj
Posted 11 years ago

Now an additional problem came up. How can I combine the two lines into one?

Product[1 + C[i], {i, 1, 2}] // Expand // FullForm
Out[-1] // Apply[List]

The simple Code doesn't do it:

Product[1 + C[i], {i, 1, 2}] // Expand // FullForm // Apply[List]

I once learnt it, but meanwhile it slipped my mind what the reason for this problem is and the way out. I also forgot where I could look it up. Could you help me out, please?

POSTED BY: Josip Turkalj
POSTED BY: David Reiss
Posted 11 years ago
POSTED BY: Josip Turkalj

The only problem with what you tried is that you used the arguments backwards. Here is what you want to do for your case (and, sorry, I should have given you an example!):

Apply[List, f[a, b, c, d, e]]

then gives

{a, b, c, d, e}

Note that there is a very common shorthand for using Apply in this simple way. it's to use @@ in the following way:

List @@ f[a, b, c, d, e]
POSTED BY: David Reiss
Posted 11 years ago
POSTED BY: Josip Turkalj

The function that you are looking for is Apply:

http://reference.wolfram.com/language/ref/Apply.html

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