SHARE
TWEET

Haskell unbound range with max

linearstream Feb 10th, 2020 (edited) 139 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 ([1..])
  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.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top