0
$\begingroup$

As we know, one of the simplest way to do a summation (with special intervals) over list elements is using

Sum[list[[i]], {i, 3, 8, 2 }] (*with steps of 2*)

for a list such as list={a, b, c, d, e, f, g, h, i, j, k, l} which returns c + e + g.

How can we use Replacelist or FoldList to this aim?

I am learning important functions (FoldList and ReplaceList) which are used instead of (Accumulation) in sum over list, in which I faced to some problems and could not find correct things. Instead of accumulation, for a simple summation what does it work!?

$\endgroup$
4
  • 7
    $\begingroup$ Why would you want to use a likely less efficient mechanism like ReplaceList or FoldList for this than what you've shown, or better yet, Tr@list[[3;;8;;2]]? Is this going to be a moving-goal-post question, and there's really something more complex you want to accomplish, or are you just looking for slower ways? $\endgroup$
    – ciao
    Aug 11, 2015 at 1:35
  • 3
    $\begingroup$ @ciao You're starting to mind-read. Beware of what you find :) $\endgroup$ Aug 11, 2015 at 1:47
  • $\begingroup$ "...you allow yourself to insult me...", huh? I have no idea what you're talking about. If I'd wanted to insult you (and I don't, no reason to and this is not the place for such nonsense in any case), I'd invite you to a chat room for a lashing... $\endgroup$
    – ciao
    Aug 11, 2015 at 5:19
  • $\begingroup$ If I made mistake I do apologize, I am so sorry. Please pardon me, $\endgroup$ Aug 11, 2015 at 5:49

1 Answer 1

3
$\begingroup$

I'd allow myself some guesswork here, that you're going after alternatives as a form of excercise in Mathematica programming. However, you've clearly stated, that the list is

list={a, b, c, d, e, f, g, h, i, j, k, l}

and the expected output is

c + e + g

Note, that in the question you've linked, ReplaceList and FoldList are utilized to generate lists of results, analogous to Accumulate. But even then, the functions used in the replacement rules or passed as arguments to FoldList are the conventional Plus. In this question, only a single expression is expected.

Off the top of my head I can only come up with this improvement to your code:

Total@list[[3;;8;;2]]
(* c + e + g *)
$\endgroup$
3
  • $\begingroup$ …and before Span[] became built-in, one used Take[]. $\endgroup$ Aug 11, 2015 at 13:37
  • $\begingroup$ @Guesswhoitis. m[[i;;j;;k]] is equivalent to Take[m,{i,j,k}]. Interesting. I like to compare the FullForms: Part[list, Span[3, 8, 2]] or Take[list, List[3, 8, 2]]. They might as well have used a short form for Take. Can you say something regarding the performance of either function? $\endgroup$
    – LLlAMnYP
    Aug 11, 2015 at 13:41
  • $\begingroup$ Not really much I can say ; to be honest, I tend to use Take[] more than Span[] as a force of habit. $\endgroup$ Aug 11, 2015 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.