Programing for lists

You can build program to perform same operations on each item of a list.
see tutorial about program to know more about programs.
Use the ForEach function.

Basic Example

Stack :
2 :  { 2 5 6 4 8 }
1 :     « FACT » 
If you perform ForEach on this stack,
you will execute the program « FACT » on each item of the list

The result will be : {2 120 720 24 40320}

Case study

The goal of the program is to compute the sum of reverse of the Xth first prime number;
Σx=1..150( 1/prime(x) ).

«
  PRIMES
  « 1/x » ForEach
  sum
»
First, we are using PRIMES to get the prime numbers.
and ForEach to compute reverse(1/x) on each primes.
abd finaly we just have to perform sum to get the sum of these numbers..

Ok, now before using run to execute the program, you must enter a value for PRIMES, thats because the program wait for an argument, let's enter 150, 150 SWAP, and then run

you got 2.1788.. as result


So, Σx=1..150( 1/prime(x) ) = 2.1788...

Advanced Notes

Stack

If you need to know what's happend to the stack, let me explain :
RPNe copy content of the stack into a new virtual stack and take item of the list one by one and put it into this new virtual stack a run the program on it.
Example :
4:        123
3:          1
2: { 1 2 3 4 }
1: « + + »
Original stack
now we run ForEach
RPNe will first create this virtual stack :
3: 123
2:   1
1:   1
and then run ++
Result will be 125
and, RPNe will create this virtual stack :
3: 123
2:   1
1:   2
and then run ++
Result will be 126
and, RPNe will first create this virtual stack :
3: 123
2:   1
1:   3
and then run ++
Result will be 127
finaly, RPNe will first create this virtual stack :
3: 123
2:   1
1:   4
and then run ++
Result will be 128
At each step, ForEach collect level 1 stack item, and give it into a list :{125 126 127 128}

Note : that you can left something into temporary virtual stacks, ForEach just take item on level 1, other item will be lost.

Note that a new stack a created and objects from original stack is copied, please note that copying heavy object can slow down RPNenh..

Scripts

Please notes that ForEach accept :
  • a program [RECOMMANDED]
  • a script (QLE or String Source)
    If you prefer to use the QLE script notation, here is the same program :
    «
      PRIMES
      { 1/x }  ForEach
      sum
    »
    
    a QLE script is used at ForEach call instead of a program.