LAZY EVALUATION AND INFINITE DATA STRUCTURES


SUBMITTED BY: smart4two

DATE: Jan. 1, 2018, 12:25 p.m.

FORMAT: Text only

SIZE: 4.5 kB

HITS: 176

  1. {-
  2. HASKELL - LAZY EVALUATION AND INFINITE DATA STRUCTURES
  3. CSE 341, Autumn 2008
  4. -}
  5. module InfiniteDataStructures
  6. where
  7. {- Since Haskell uses lazy evaluation, we can have infinite data structures --
  8. we only produce as much of the structure as is needed. We've seen various
  9. examples of this, for example
  10. [1.. ]
  11. [1,3 ..]
  12. verylargetree (from TypesNotes.hs)
  13. -}
  14. my_member :: Eq a => a -> [a] -> Bool
  15. my_member x [] = False
  16. my_member x (y:z) | x==y = True
  17. | otherwise = my_member x z
  18. -- this works: my_member 10 [1..]
  19. -- this searches forever: my_member 3 [10..]
  20. -- MEMBER FUNCTION FOR SORTED LISTS
  21. sorted_member :: Ord a => a -> [a] -> Bool
  22. sorted_member x [] = False
  23. sorted_member x (y:z) | x==y = True
  24. | x<y = False
  25. | x>y = sorted_member x z
  26. {- this version of member will always terminate. For example
  27. sorted_member 3 [10..]
  28. terminates immediately
  29. -}
  30. -- SQUARE ROOT
  31. sqt x = head (filter close_enough (approximations x))
  32. where approximations a = a : approximations ((a + x/a)/2)
  33. close_enough root = abs (x-root*root) < 1.0e-6
  34. -- to show the list of successive approximations:
  35. trial_roots x = approximations x
  36. where approximations a = a : approximations ((a + x/a)/2)
  37. -- INFINITE LIST OF PRIME NUMBERS
  38. factors n = [k | k <- [1..n], n `mod` k == 0]
  39. dullprimes = filter isprime [2..]
  40. where
  41. isprime p = (factors p == [1,p])
  42. {- Fibonacci numbers (clear but inefficient recursive version) -}
  43. rec_fib 1 = 1
  44. rec_fib 2 = 1
  45. rec_fib n = rec_fib (n-1) + rec_fib (n-2)
  46. {- map2 is like map but takes a 2-argument function (we'll use it
  47. for another version of Fibonacci numbers) -}
  48. map2 :: (a -> b -> c) -> [a] -> [b] -> [c]
  49. map2 f [] [] = []
  50. map2 f (x:xs) (y:ys) = f x y : map2 f xs ys
  51. map2 f [] (y:ys) = error "different length lists to map2"
  52. map2 f (x:xs) [] = error "different length lists to map2"
  53. {- some would argue that better style would be to replace the last two
  54. definitions with
  55. map2 f xs ys = error "different length lists to map2"
  56. This has one less pattern, but depends on the order in which they are given
  57. (the other version is order-independent). -}
  58. {- Infinite list of Fibonacci numbers. This uses a common pattern for
  59. Haskell infinite list definitions, in which you have a prime-the-pump
  60. sort of recursive definition. -}
  61. fibs = 1 : 1 : map2 (+) fibs (tail fibs)
  62. {- HAMMING NUMBERS
  63. The Hamming numbers are defined as the list of all numbers of the form
  64. 2^a * 3^b * 5*c
  65. for non-negative integers a, b, and c; in other words, numbers whose
  66. only prime factors are 2, 3. and 5. -}
  67. my_merge (x:a) (y:b) | x<y = x : my_merge a (y:b)
  68. | x==y = y : my_merge a b
  69. | x>y = y : my_merge (x:a) b
  70. ham = 1: my_merge ham2 (my_merge ham3 ham5)
  71. ham2 = map (*2) ham
  72. ham3 = map (*3) ham
  73. ham5 = map (*5) ham
  74. {-
  75. take 100 ham
  76. evaluates to:
  77. [1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27, 30, 32, 36,
  78. 40, 45, 48, 50, 54, 60, 64, 72, 75, 80, 81, 90, 96, 100, 108, 120, 125,
  79. 128, 135, 144, 150, 160, 162, 180, 192, 200, 216, 225, 240, 243, 250, 256,
  80. 270, 288, 300, 320, 324, 360, 375, 384, 400, 405, 432, 450, 480, 486, 500,
  81. 512, 540, 576, 600, 625, 640, 648, 675, 720, 729, 750, 768, 800, 810, 864,
  82. 900, 960, 972, 1000, 1024, 1080, 1125, 1152, 1200, 1215, 1250, 1280, 1296,
  83. 1350, 1440, 1458, 1500, 1536]
  84. -}
  85. {- Infinite list of prime numbers using the Sieve of Eratosthenes.
  86. See http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes -}
  87. interestingprimes = sieve [2..]
  88. where
  89. sieve (p:x) = p : sieve [n | n <- x, n `mod` p > 0]
  90. {- Why Infinite Lists?
  91. Are infinite lists (and other infinite data structures) simply curiosities
  92. or something more important?
  93. Simon Thompson in "The Craft of Functional Programming" argues that they
  94. are more important, for two reasons:
  95. First, an infinite version of a program can be more abstract and simpler to
  96. write, since we don't need to know in advance how long to make the list
  97. (e.g. the fibonacci number list).
  98. Second, we can often modularize the generation of values, and separate out
  99. some transformations we perform on them. We can then link process-style
  100. programs that involve recursion. (Thompson gives a simulation example.)
  101. -}

comments powered by Disqus