home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 876 / hugs.sis / ArrayEx.hs < prev    next >
Encoding:
Text File  |  2000-09-21  |  994 b   |  27 lines

  1. -- Some simple examples using arrays:
  2.  
  3. module ArrayEx where
  4. import Array
  5.  
  6. -- Some applications, most taken from the Gentle Introduction ... -------------
  7.  
  8. timesTable :: Array (Int,Int) Int
  9. timesTable  = array ((1,1),(10,10)) [ ((i,j), i*j) | i<-[1..10], j<-[1..10] ]
  10.  
  11. fibs n = a where a = array (0,n) ([ (0,1), (1,1) ] ++
  12.                                   [ (i, a!(i-2) + a!(i-1)) | i <- [2..n] ])
  13.  
  14. wavefront n = a where a = array ((1,1),(n,n))
  15.                              ([ ((1,j), 1) | j <- [1..n] ] ++
  16.                               [ ((i,1), 1) | i <- [2..n] ] ++
  17.                               [ ((i,j), a!(i,j-1) + a!(i-1,j-1) + a!(i-1,j))
  18.                                            | i <- [2..n], j <- [2..n] ])
  19.  
  20. listwave n = [ [wf!(i,j) | j <- [1..n]] | i <- [1..n] ]
  21.              where wf = wavefront n
  22.  
  23. eg1 :: Array Integer Integer
  24. eg1  = array (1,100) ((1, 1) : [ (i, i * eg1!(i-1)) | i <- [2..100] ])
  25.  
  26. -------------------------------------------------------------------------------
  27.