From: Eduardo O. <edu...@gm...> - 2023-10-24 13:30:50
|
Hi Michel, map sometimes recurses on trees, not on lists: f : lambda([x], x+10); g : lambda([ab], 10*ab[1]+ab[2]); f([3,4]); map(f, [[1,2],[3,4],[5,6]]); /* [[11, 12], [13, 14], [15, 16]] */ map(g, [[1,2],[3,4],[5,6]]); /* [12, 34, 56] */ map(f, [[1,2],[3,4],5]); /* [[11, 12], [13, 14], 15] */ map(g, [[1,2],[3,4],5]); /* error */ It tries to do the right thing and it usually does, but there are some cases in which its dtrt-iness is not what I expected... so I thought that it would be nice to also have some functions with a simpler logic. Cheers, Eduardo On Tue, 24 Oct 2023 at 05:48, Michel Talon <ta...@lp...> wrote: > What is the problem with the simpler: > > (%i1) map(lambda([x],x+a),[2,3,4]); > (%o1) [a + 2, a + 3, a + 4] > (%i2) x:5; > (%o2) 5 > (%i3) map(lambda([x],x+a),[2,3,4]); > (%o3) [a + 2, a + 3, a + 4] > (%i4) a:3; > (%o4) 3 > (%i5) map(lambda([x],x+a),[2,3,4]); > (%o5) [5, 6, 7] > > Le 24/10/2023 à 01:24, Eduardo Ochs a écrit : > > Hi list, > > what is the right/recommended way to define something like Lisp's > "map" in Maxima? I tried this, > > to_lisp(); > (defun foo (x) (* x 10)) > (map 'list 'foo '(2 3 5 8)) ;;-> (20 30 50 80) > (to-maxima) > > map_list(f, as) := makelist(apply(f, [a]), a, as); > map_list(lambda([x], 10*x), [2, 3, 5, 8]); /* [20, 30, 50, 80] */ > map_list(lambda([x], x+b), [2, 3, 5, 8]); /* [b+2, b+3, b+5, b+8] */ > map_list(lambda([x], x+a), [2, 3, 5, 8]); /* [4, 6, 10, 16] */ > > but it doesn't work well in cases like the last one, in which f > mentions a global variable with the same name as the variable used in > the makelist... > > Thanks in advance! > Cheers =), > Eduardo Ochs > > > _______________________________________________ > Maxima-discuss mailing lis...@li...https://lists.sourceforge.net/lists/listinfo/maxima-discuss > > -- > Michel Talon > > _______________________________________________ > Maxima-discuss mailing list > Max...@li... > https://lists.sourceforge.net/lists/listinfo/maxima-discuss > |