Zulip Chat Archive

Stream: Is there code for X?

Topic: list operation


Kenny Lau (Jul 04 2020 at 16:58):

Is there a function that sends the lists [a,b,c] and [d,e,f] to [a*d, a*e, a*f, b*d, b*e, b*f, c*d, c*e, c*f] given any binary operator *?

Kenny Lau (Jul 04 2020 at 17:01):

something like (L.product M).map (function.uncurry (*))

Chris Hughes (Jul 04 2020 at 18:14):

zip_with?

Chris Hughes (Jul 04 2020 at 18:14):

No that's different actually.

David Wärn (Jul 04 2020 at 19:09):

This is a job for control theory

def foo {α β γ} (f : α  β  γ) (as : list α) (bs : list β) : list γ := f <$> as <*> bs
#reduce foo (+) [0, 1, 2] [10, 20, 30] -- [10, 20, 30, 11, 21, 31, 12, 22, 32]

Scott Morrison (Jul 05 2020 at 00:11):

Surely something like do a <- L, b <- M, return a*b works, and requires understanding fewer hieroglyphics. (Not at a computer, didn't test.)

Simon Hudon (Jul 05 2020 at 00:16):

It does work too. It uses the monad structure on list where as @David Wärn's solution only requires an applicative functor

Bhavik Mehta (Jul 05 2020 at 15:16):

Should we port ApplicativeDo to lean :big_smile:

Simon Hudon (Jul 05 2020 at 18:08):

I wonder if we can implement that as a library


Last updated: Dec 20 2023 at 11:08 UTC