The F Language

Software Careers Fall 1997 Dr. Dobb's Journal

by Walt Brainerd, David Epstein, and Dick Hendrickson


Listing One
!One style of writing modules is to define a public defined type with
!private parts and to have as the only other public entities the
!procedures that operate on that defined type.

  module m_employee
   ! List the names of the public procedures
   public :: GetName,     SetName
   public :: GetIdNumber, SetIdNumber
   public :: GetSalary,   SetSalary
   ! define a constant that is private to this module
   integer, private, parameter :: E_NAME_LEN = 64
   ! define a public type with private insides
   type, public :: t_employee
      private
       character(len=E_NAME_LEN) :: name
       integer                   :: id_number
       integer                   :: salary
    endtype t_employee
  contains
   ! define the procedures
   function GetName(an_employee) result(name)
     type(t_employee), intent(in) :: an_employee
     character(len=E_NAME_LEN) :: name
      name = an_employee%name
   endfunction GetName
   subroutine SetName(an_employee, name)
     type(t_employee), intent(inout) :: an_employee
     character(len=*), intent(in)    :: name
      an_employee%name = name
   endsubroutine SetName
   ! ... functions and subroutines for IdNumber and Salary not shown
   ! ...
  endmodule m_employee

Listing Two
!  a simple solution to the heat equation using arrays
!  and pointers
program heat_with_pointers
 real, dimension(10,10),target :: plate
 real, dimension(8,8)          :: temp
 real, pointer, dimension(:,:) :: n, e, s, w, inside

 real    :: diff
 integer :: i,j, niter

  !  set up initial conditions
  plate = 0
  plate(1:10,1) = 1.0                                  ! boundary value
  plate(1,1:10) = (/ ( 0.1*j, j = 10, 1, -1 ) /)

  !  point to parts of the plate
  inside => plate(2:9,2:9)
  n => plate(1:8,2:9)
  s => plate(3:10,2:9)
  e => plate(2:9,1:8)
  w => plate(2:9,3:10)

  niter = 0
  do
    temp = (n + e + s + w)/4.0
    diff = maxval(abs(temp-inside))
    inside = temp
    niter = niter + 1
    print *, niter, diff  ! to show how the convergence is progressing
    if (diff < 1.0e-4) then
      exit
    endif
  enddo

  do i = 1,10
    print '(10f7.3)', plate(1:10,i)
  enddo

endprogram heat_with_pointers

Listing Three
! Replace the non-diagonal, non-zero elements with their inverse.
! A 100-processor system could do this in one step.
forall (i = 1,10, j = 1,10,   i/=j .and. a(i,j) /=0.0)
     a(i,j) = 1.0 / a(i,j)
endforall

End Listings
DDJ

Return to Article