# Distributing a list of heads over a list of arguments

**URL:** <https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479>\
**Category:** Questions\
**Tags:** mathematica\
**Created:** [September 2, 2013, 5:13pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479 "2013-09-02T17:13:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vitaliyk](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/vitaliyk/32/45591_2.png) [@Vitaliyk](https://community.wolfram.com/u/Vitaliyk)\
**Post date:** [September 2, 2013, 5:13pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/1 "2013-09-02T17:13:56Z")

</div>

Given two lists, one for the heads another for their arguments

```auto
heads = {f, g, h, k};
arguments = {w, x, y, z};

```

**is there less clumsy way to do this?**

```auto
In[1]:= Diagonal[Through /@ Distribute[heads[arguments], List]]
Out[1]= {f[w], g[x], h[y], k[z]}

```

---

<div class="post-metadata">

**Author:** ![iliang](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/iliang/32/11263_2.png) [@iliang](https://community.wolfram.com/u/iliang)\
**Post date:** [September 2, 2013, 5:48pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/2 "2013-09-02T17:48:20Z")

</div>

Perhaps

```auto
MapThread[Compose, {heads, arguments}]

```

---

<div class="post-metadata">

**Author:** ![sghuisman](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sghuisman/32/27850_2.png) [@sghuisman](https://community.wolfram.com/u/sghuisman)\
**Post date:** [September 2, 2013, 6:22pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/3 "2013-09-02T18:22:07Z")

</div>

Compose is currently superseded by composition, but that works slightly different, so another solution is:

```auto
MapThread[#1@#2 &, {heads,arguments}]

```

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [September 2, 2013, 6:25pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/4 "2013-09-02T18:25:09Z")

</div>

```auto
In[1]:= Inner[#1[#2] &, heads, arguments, List]
Out[1]= {f[w], g[x], h[y], k[z]}

```

---

<div class="post-metadata">

**Author:** ![itais](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/itais/32/7284_2.png) [@itais](https://community.wolfram.com/u/itais)\
**Post date:** [September 2, 2013, 7:34pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/5 "2013-09-02T19:34:06Z")

</div>

Sander Huisman, - this would be my solution as well.

---

<div class="post-metadata">

**Author:** ![schmidtsimon](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@schmidtsimon](https://community.wolfram.com/u/schmidtsimon)\
**Post date:** [September 2, 2013, 8:11pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/6 "2013-09-02T20:11:00Z")

</div>

Some more just for fun

```auto
(* Clearly that? is a transpose symbol *)
{heads, arguments}?/. {f_, a_} :> f@a

(* Listability is *always* good *)
Attributes[headThread] = {Listable};
headThread[f_, a_] := f@a;
headThread[heads, arguments]

(* .. especially in pure functions *)
Function[, #1@#2, {Listable}][heads, arguments]

```

---

<div class="post-metadata">

**Author:** ![schmidtsimon](https://avatars.discourse-cdn.com/v4/letter/s/c0e974/32.png) [@schmidtsimon](https://community.wolfram.com/u/schmidtsimon)\
**Post date:** [September 2, 2013, 8:40pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/7 "2013-09-02T20:40:40Z")

</div>

No worries, but for the inconvenience you have to suffer reading this code, in the category:  
_Way clumsier_

```auto
result = {};
For[
  i = 1,
  i <= Max[Length /@ {heads, arguments}],
  i += Length[IntegerPartitions[i]]/PartitionsP[i],
  AppendTo[result, heads[[i]][arguments[[i]]]]
  ];
result

```

---

<div class="post-metadata">

**Author:** ![Vitaliyk](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/vitaliyk/32/45591_2.png) [@Vitaliyk](https://community.wolfram.com/u/Vitaliyk)\
**Post date:** [September 2, 2013, 8:48pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/8 "2013-09-02T20:48:08Z")

</div>

@Simon Schmidt - Wow this is a real fun one 😉 +1 !

---

<div class="post-metadata">

**Author:** ![arnoudb](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/arnoudb/32/41177_2.png) [@arnoudb](https://community.wolfram.com/u/arnoudb)\
**Post date:** [September 4, 2013, 2:01pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/9 "2013-09-04T14:01:11Z")

</div>

```auto
Dot[heads, arguments] /. Times -> (Operate[##, 0] &)

```

---

<div class="post-metadata">

**Author:** ![itais](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/itais/32/7284_2.png) [@itais](https://community.wolfram.com/u/itais)\
**Post date:** [September 4, 2013, 4:28pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/10 "2013-09-04T16:28:51Z")

</div>

Nice, but the Operate is slightly heavy handed. I think the following is both shorter and easier to understand:

```auto
In[7]:= Dot[heads, arguments] /. Times -> (#1[#2] &)

Out[7]= f[w] + g[x] + h[y] + k[z]

```

Of course, this will break if one of the arguments has head Times.

```auto
arguments = {w, x, y, z b};

In[18]:= Dot[heads, arguments] /. Times -> (Operate[##, 0] &)

During evaluation of In[18]:= Operate::argt: Operate called with 4 arguments; 2 or 3 arguments are expected. >>

Out[18]= f[w] + g[x] + h[y] + Operate[b, k, z, 0]

In[19]:= Dot[heads, arguments] /. Times -> (#1[#2] &)

Out[19]= b[k] + f[w] + g[x] + h[y]

```

---

<div class="post-metadata">

**Author:** ![brettc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/brettc/32/39223_2.png) [@brettc](https://community.wolfram.com/u/brettc)\
**Post date:** [September 4, 2013, 4:40pm UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/11 "2013-09-04T16:40:43Z")

</div>

> In[7]:= Dot[heads, arguments] /. Times → (#1[#2] &)
> 
> Out[7]= f + g + h + k
> 
> Of course, this will break if one of the arguments has head Times.

And while it’s frequently safer to use **Replace** at a specific level is rather than **ReplaceAll** (/.) in code, even that won’t help you in this case. That’s probably why Dan’s answer used **Inner** instead of **Dot**.

---

<div class="post-metadata">

**Author:** ![murta](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/murta/32/7023_2.png) [@murta](https://community.wolfram.com/u/murta)\
**Post date:** [September 5, 2013, 2:26am UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/12 "2013-09-05T02:26:45Z")

</div>

```auto
Compose@@@Thread@{heads, arguments}

```

---

<div class="post-metadata">

**Author:** ![chetiyas](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/chetiyas/32/38741_2.png) [@chetiyas](https://community.wolfram.com/u/chetiyas)\
**Post date:** [September 6, 2013, 5:36am UTC](https://community.wolfram.com/t/distributing-a-list-of-heads-over-a-list-of-arguments/479/13 "2013-09-06T05:36:50Z")

</div>

```auto
 #[[1]][#[[2]]] & /@ Transpose[{heads, arguments}]

```
