Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created January 24, 2026 16:26
Show Gist options
  • Select an option

  • Save Garciat/5de67b60eeab0ec5277784f10c053cd2 to your computer and use it in GitHub Desktop.

Select an option

Save Garciat/5de67b60eeab0ec5277784f10c053cd2 to your computer and use it in GitHub Desktop.
Wallis product in Haskell - https://en.wikipedia.org/wiki/Wallis_product
import Data.Ratio
rat f g r = f (numerator r) % g (denominator r)
num f = rat f id
den f = rat id f
-- fun way
wallis = (2 % 1) : zipWith ($) (cycle [den (+2), num (+2)]) wallis
-- direct way
wallis' = map (\n -> (4 * n**2) % (4 * n**2 - 1)) [1..]
-- >>> pi/2
-- 1.5707963267948966
-- >>> fromRational $ product $ take 10000 wallis
-- 1.5707177967949542
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment