0

I know that one can act a function on every member of a list using the map function, e.g.,

f(x):=block([x:x], x^2)$
foo:[1,2,3];
map('f,foo);

However, I haven't been able to work out how one would do the equivalent with an array of lists, and a function that acts on lists (vs. list members).

E.g., given bar:[[1,2,3],[a,b,c]]$ I'd like to find a general way to obtain a list of each member of bar concatenated into a string, i.e., the output would be the same as:

[apply('sconcat, bar[1]), apply('sconcat,bar[2])];

I'd actually like to then concatenate the resulting list; however, that seems pretty straightforward, once at this point.

Rax Adaam
  • 790
  • 3
  • 11
  • 1
    I think maybe what's needed is to call `apply` for each list of arguments. E.g. `map (lambda ([L], apply ('sconcat, L)), bar)` in this case. – Robert Dodier Nov 12 '21 at 17:31
  • That makes sense & is better than `map('apply, makelist('sconcat,i,length(bar)), bar);` which is all I came up with. Thank you for your help! – Rax Adaam Nov 12 '21 at 21:09
  • @RobertDodier is there an analogous approach for iterating an operation on the same list? E.g. `foo=[1,2,3,4,5];` and `bar:[1,3,5]` and we wanted to return the list `foo` without any elements bar, e.g. via `delete`, or would that be best handled via some other approach? – Rax Adaam Nov 19 '21 at 00:05
  • 1
    Something that comes to mind is `sublist (foo, lambda ([x], not member (x, bar))`. There are probably other ways. I try to set up the code so that the result is all or nothing -- either you get all the way through and the result is what you want, or there's an error and nothing happened. I find that easier to process mentally than an iterative, step by step approach, e.g. delete first element of bar, delete second, etc. Just something to bear in mind. – Robert Dodier Nov 19 '21 at 04:30
  • Would something like: `delete(0, makelist(lambda([x], if member(x,bar) then 0 else x)(foo[i]), i, length(foo)))` follow that rule? Obviously the `sublist` solution is better, I had just forgotten about it & had done this, instead, but feel there's a point at which the one-line commands become opaque (at least on future visits). Thank you for the better alternative! – Rax Adaam Nov 19 '21 at 13:36
  • 1
    Well, that solution does satisfy the all or nothing desideratum, but it does seem a little more involved; a more serious issue is that it only works if 0 is not on the list. Even if it's true that 0 is not on any list you're working on at present, it seems likely that it will happen sooner or later. You could work around the issue by ensuring the replacement value cannot be on the list, but then that's adding more complexity to it. – Robert Dodier Nov 19 '21 at 22:49
  • @RobertDodier thanks Robert -- it's definitely more involved :D (& I did update to the sublist solution); I'm just trying to get a better sense of what counts as "functional programming" and what doesn't. I frequently find that I need to define helper lists that store information temporarily; however, I _have_ managed to start writing scripts in more all-or-nothing chunks. FYI, in this case (as is often the case for me), list members aren't ever numerical & this was the first thing that came to mind; hopefully next time `sublist` will be the firs thing :) – Rax Adaam Nov 21 '21 at 16:20
  • No worries, it's all good. It's just a matter of adjusting one's conceptual organization to better match whatever the programming language is capable of. Maxima is not entirely coherent so that's not always easy. – Robert Dodier Nov 21 '21 at 22:27

0 Answers0