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

  1. -- Some examples of functional programming for Hugs
  2.  
  3. import Gofer
  4.  
  5. -- Factorials:
  6.  
  7. fact n = product [1..n]                     -- a simple definition
  8.  
  9. fac n  = if n==0 then 1 else n * fac (n-1)  -- a recursive definition
  10.  
  11. fac' 0 = 1                                  -- using two equations
  12. fac' n = n * fac (n-1)
  13.  
  14. facts, facts' :: (Enum a, Num a) => [a]
  15. facts          = scanl (*) 1 [1..]            -- infinite list of factorials
  16. facts'         = 1 : zipWith (*) facts' [1..] -- another way of doing it
  17.  
  18. facFix :: Num a => a -> a
  19. facFix = fixedPt f                          -- using a fixed point combinator
  20.          where  f g 0       = 1             -- overlapping patterns
  21.                 f g n       = n * g (n-1)
  22.                 fixedPt f = g where g = f g -- fixed point combinator
  23.  
  24. facCase :: Integral a => a -> a
  25. facCase  = \n -> case n of
  26.                    0     ->  1
  27.                    (m+1) -> (m+1) * facCase m
  28.  
  29. -- Fibonacci numbers:
  30.  
  31. fib 0     = 0                               -- using pattern matching:
  32. fib 1     = 1                               -- base cases...
  33. fib (n+2) = fib n + fib (n+1)               -- recursive case
  34.  
  35. fastFib n = fibs !! n                       -- using an infinite stream
  36.             where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
  37.  
  38. -- Perfect numbers:
  39.  
  40. factors n    = [ i | i<-[1..n-1], n `mod` i == 0 ]
  41. perfect n    = sum (factors n) == n
  42. firstperfect = head perfects
  43. perfects     = filter perfect [(1::Int)..]
  44.  
  45. -- Prime numbers:
  46.  
  47. primes      :: Integral a => [a]
  48. primes       = map head (iterate sieve [2..])
  49. sieve (p:xs) = [ x | x<-xs, x `rem` p /= 0 ]
  50.  
  51. -- Pythagorean triads:
  52.  
  53. triads n     = [ (x,y,z) | let ns=[1..n], x<-ns, y<-ns, z<-ns, x*x+y*y==z*z ]
  54.  
  55. -- The Hamming problem:
  56.  
  57. hamming     :: [Int]
  58. hamming      = 1 : (map (2*) hamming || map (3*) hamming || map (5*) hamming)
  59.                where (x:xs) || (y:ys)  | x==y  =  x : (xs || ys)
  60.                                        | x<y   =  x : (xs || (y:ys))
  61.                                        | y<x   =  y : (ys || (x:xs))
  62.  
  63. -- Digits of e:
  64.  
  65. eFactBase ::  [Int]
  66. eFactBase  =  map head (iterate scale (2:repeat 1))
  67.  
  68. scale     ::  Integral a => [a] -> [a]
  69. scale      =  renorm . map (10*) . tail
  70. renorm ds  =  foldr step [0] (zip ds [2..])
  71.  
  72. step (d,n) bs | (d `mod` n + 9) < n  = (d `div` n) : b : tail bs
  73.               | otherwise            = c           : b : tail bs
  74.               where b' = head bs
  75.                     b  = (d+b') `mod` n
  76.                     c  = (d+b') `div` n
  77.  
  78. -- Pascal's triangle
  79.  
  80. pascal :: [[Int]]
  81. pascal  = iterate (\row -> zipWith (+) ([0]++row) (row++[0])) [1]
  82.  
  83. showPascal = putStr ((layn . map show . take 14) pascal)
  84.  
  85.