Skip to content

Instantly share code, notes, and snippets.

@JonasGroeger
Created June 22, 2012 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonasGroeger/2973869 to your computer and use it in GitHub Desktop.
Save JonasGroeger/2973869 to your computer and use it in GitHub Desktop.
Cantorsche Paarungsfunktion
module Cantor (
c,
cx,
cy,
d,
h,
c_umkehr,
findmax)
where
-- Calculates the c function from cantor
c x y = y + 0.5 * (x+y+1) * (x+y)
-- Calculates "backwards" the x value of given c
cx z = _q(z) - cy(z) - 1
-- Calculates "backwards" the y value of given c
cy z = z - _f(_q(z))
c_umkehr z = (cx z, cy z)
-- Added this method for convenience, as x isn't
-- important. Tt's always length(list) - 1
d x list = _d list
h list = 1 + (c (fromIntegral(index)) (d index list))
where index = length(list) - 1
-- Finding the value for q in a fast way that is calculatable
-- by hand even for large numbers.
findmax z = floor (sqrt (2*z))
{-----------------------------------------------
------------ HELPER FUNCTIONS BELOW ------------
-----------------------------------------------}
_f x = 0.5 * x * (x-1)
_q x = maximum [v | v <- [1..x], _f v <= x]
_d list
| (length(list) == 1) = head list
| otherwise = c (head list) (_d (tail list))
@JonasGroeger
Copy link
Author

Unter Linux mit der Konsole starten:

ghci cantor.hs

Funktion c

c 4 1

Funktionen cx und cy

cx 28

und

cy 28

Schon haben wir die x und y Werte von 28, nämlich cx=7 und cy=0

Funktion h

h [8, 6, 2]

Funktion d

d 3 [8, 7, 6, 0]

wobei die erste Zahl der Index ist und die Liste die Argumente für d.
Da der Index nur die Anzahl der Parameter-1 ist, kann man auch

_d [8, 7, 6, 0]

verwenden und so den Index weglassen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment