Advertisement
linearstream

Haskell unbound range with max

Feb 10th, 2020
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Contrived Haskell example of problem I am trying to solve
  2.  
  3. -- Given a function that doubles a number
  4. double :: Int -> Int
  5. double x = 2*x
  6. -- And a function that gives an array of even numbers with a max cutoff
  7. evens :: Int -> [Int]
  8. evens max = [double(x) | x <- [1..], double(x) <= max]
  9.  
  10. -- How do I keep this from hanging when it gets to max?
  11. --   Prelude> evens 19
  12. --   [2,4,6,8,10,12,14,16,18  -- this hangs due to the unbound range
  13. --   ^CInterrupted.
  14. -- So how can I get the unbound range to stop?
  15. -- New to Haskell so I assume there is an easier/better way to do this.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement