home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Demos / arrayEx.gs < prev    next >
Text File  |  1994-06-23  |  1KB  |  31 lines

  1. -- Some simple examples using arrays.  Requires array.gs.
  2.  
  3. -- Some applications, most taken from the Gentle Introduction ... -------------
  4.  
  5. timesTable = array ((1,1),(10,10)) [ (i,j) := i*j | i<-[1..10], j<-[1..10] ]
  6.  
  7. fibs n = a where a = array (0,n) ([ 0 := 1, 1 := 1 ] ++
  8.                                   [ i := a!(i-2) + a!(i-1) | i <- [2..n] ])
  9. fibs10 = fibs 10
  10.  
  11. wavefront n = a where a = array ((1,1),(n,n))
  12.                              ([ (1,j) := 1 | j <- [1..n] ] ++
  13.                               [ (i,1) := 1 | i <- [2..n] ] ++
  14.                               [ (i,j) := a!(i,j-1) + a!(i-1,j-1) + a!(i-1,j)
  15.                                            | i <- [2..n], j <- [2..n] ])
  16.  
  17. wave10 = wavefront 10
  18.  
  19. listwave n = [ [wf!(i,j) | j <- [1..n]] | i <- [1..n] ]
  20.              where wf = wavefront n
  21.  
  22. eg1 = array (1,100) ((1 := 1) : [ i := i * eg1!(i-1) | i <- [2..100] ])
  23.  
  24. -------------------------------------------------------------------------------
  25.  
  26. a1 = array (-5,5) []
  27. a2 = a1 // [ 1 := True ]
  28. a3 = a1 // [ 0 := a1 ]
  29. a4 = array (-5,5) [ i := i*i | i <- [-5..0] ]
  30.  
  31.