home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / hugs_1 / demos_hs_ArrayEx < prev    next >
Encoding:
Text File  |  1996-08-12  |  973 b   |  26 lines

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