How to get Mathematica to stream a 2-variable function over two lists using functional programming methods?

Suppose I have a function f[x_, y_] and two lists l1 , l2 . I would like to evaluate f[x,y] for each pair of x,y with x in l1 and y in l2 , and I would like to do this without doing all pairs of the form {l1[[i]],l2[[j]]} .

Essentially, I want something like Map [Map [f [# 1, # 2] &, l1], l2], where # 1 takes values โ€‹โ€‹from l1 and # 2 takes values โ€‹โ€‹from l2, but that doesnโ€™t work .

(Motivation: I'm trying to implement some basic Haskell programs in Mathematica. In particular, I would like to be able to code the Haskell program

 isMatroid::[[Int]]->Bool isMatroid b =and[or[sort(union(xs\\[x])[y]'elem'b|y<-ys]|xs<-b,ys<-b, xs<-x] 

I think I can do the rest if I can figure out the original question, but I would like the code to look like Haskell. Any suggestions for implementing Haskell-like code in Mathematica would be appreciated.)

+4
source share
3 answers

To evaluate the function f for all pairs from the two lists l1 and l2 , use Outer :

 In[1]:= Outer[f, {a,b}, {x,y,z}] Out[1]:= {{f[a,x],f[a,y],f[a,z]}, {f[b,x],f[b,y],f[b,z]}} 

Outer by default, runs at the lowest level of lists provided; you can also specify the level with an additional argument:

 In[2]:= Outer[f, {{1, 2}, {3, 4}}, {{a, b}, {c, d}}, 1] Out[2]:= {{f[{1,2},{a,b}], f[{1,2},{c,d}]}, {f[{3,4},{a,b}], f[{3,4},{c,d}]}} 

Note that this creates a nested list; You can Flatten if you want.

My original answer pointed to Thread and MapThread , which are two ways to apply a function to the corresponding pairs from lists, for example MapThread[f,{a,b},{1,2}] == {f[a,1], f[b,2]} .

PS I think that by studying these things you will find the documentation very useful. There are many common topic pages, such as applying functions to lists and manipulating a list . They are usually linked in the โ€œmoreโ€ section at the bottom of specific documentation. This greatly facilitates the search for things when you do not know what they will be called.

+9
source

Raise an OP query for suggestions for implementing Haskell type code in Mathematica. A few things you will have to deal with:

  • Haskell evaluates lazily; by default, Mathematica does not; he is very thirsty. You will need to struggle with Hold [] and its relatives to write lazily evaluating functions, but this can be done. You can also undermine the Mathematica evaluation process and mess with Prolog and Epilog, etc.
  • The Haskell type and type system is probably more stringent than the default values โ€‹โ€‹for Mathematica, but Mathematica has functions to implement strong type checking.

I am sure there is much more, but I am not very familiar with Haskell.

+3
source

In [1]: = list1 = Range [1, 5];

In [2]: = list2 = Range [6, 10];

In [3]: = (f @@ #) and / @ Transpose [{list1, list2}]

Out [3] = {f [1, 6], f [2, 7], f [3, 8], f [4, 9], f [5, 10]}

0
source

Source: https://habr.com/ru/post/1305811/


All Articles