New answers tagged

1

I think you are misunderstanding the nature of Map. It applies a head to a series of elements, then evaluates the complete expression. Please see my answer to Scan vs. Map vs. Apply for examples and further description. What you want might be handled by redefining f rather than redefining list. ClearAll[f, list, counter] f[element_] := (If[element == 1, ...


1

A solution is to manage the loop yourself, i.e. list = {0, 0, 1, 0, 0, 1, 1, 1, 1}; p = MapIndexed[Prepend[#2, #1] &, list]; f[{element_}] := (If[First[element] == 1, p = Select[p, First[#] != 1 &]]; counter++) f[{}] := counter++ counter = 0; Array[f[Cases[p, {_, #}]] &, Length[p]]; counter 9 list = Map[First, p] {0, 0, 0, 0}


1

You may use Scan and Return. Return from f after you reset the list. f[element_] := (If[element == 1, list = Select[list, # != 1 &]; Return[]]; counter++;) Then Scan the function with the Return . Scan[f, list] counter (* 4 *) Hope this helps.


3

Some of these could be implemented differently, of course, but I've gone the way of making all of them pure functions (in the Mathematica sense). Every single one takes a Sequence of arguments as the inputs, but some of them accept function names as inputs first, and the projection function accepts an integer for which argument is chosen (I have chosen to ...


2

This is fairly elegant way for dealing with your example, but I don't see how to generalize it a multi-dimensional Fold, what ever that might be. p1 = a x^5 + (x + 2 y)^3 + x y z + 1; vars = {x, y, z}; cl = CoefficientList[p1, vars]; p2 = Fold[FromDigits[Reverse[#1], #2] &, cl, vars]; p1 - p2 // Expand 0 BTW, I unashamedly stole this from the 2nd ...



Top 50 recent answers are included