Skip to content

Instantly share code, notes, and snippets.

@Sintrastes
Created September 16, 2019 20:01
Show Gist options
  • Save Sintrastes/d353af006ce6a897efb8c8b97e3cebb2 to your computer and use it in GitHub Desktop.
Save Sintrastes/d353af006ce6a897efb8c8b97e3cebb2 to your computer and use it in GitHub Desktop.
An implementation of the Calkin-Wilf sequence in Haskell
--
-- An implementation of the Calkin-Wilf sequence in Haskell -- gives a bijection between the natural numbers
-- and the (positive) rationals.
--
-- See https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree for more info
--
import Data.Ratio
calkinWilfNext q = 1 / (2 * ( (floor q) % 1 ) - q + 1)
calkinWilfSequence = iterate calkinWilfNext (1%1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment