!------------------------------------------------------------------------------
!
!  Program name:
!
!    recursion
!
!  Purpose:
!
!    This program explains that the array syntax assumes no recursion and
!    we should beware of array recursion.
!
!------------------------------------------------------------------------------

PROGRAM recursion

  IMPLICIT NONE
  INTEGER, PARAMETER :: n = 5
  INTEGER, DIMENSION(n) :: ia
  INTEGER :: i


  ! Recursion in ia:

  ia = (/ 1, 20, 300, 4000, 50000 /)

  DO i=2,n
    ia(i) = ia(i-1) + ia(i)
  END DO
  WRITE(*,'(5I10)') ia


  ! Fortran 90 uses original values of ia => no recursion:

  ia = (/ 1, 20, 300, 4000, 50000 /)
  
  ia(2:n) = ia(1:n-1) + ia(2:n)
  WRITE(*,'(5I10)') ia

  ! Fortran 90 equivalent to the recursive loop:

  ia = (/ 1, 20, 300, 4000, 50000 /)
  
  ia(2:n) =  (/ (SUM(ia(1:i)),i=2,n) /)
  WRITE(*,'(5I10)') ia
  
END PROGRAM recursion
