Skip to content

Instantly share code, notes, and snippets.

@Ivoz
Last active December 20, 2015 10:09
Show Gist options
  • Save Ivoz/6113373 to your computer and use it in GitHub Desktop.
Save Ivoz/6113373 to your computer and use it in GitHub Desktop.
def up(a, b, n):
"""Calculates a ↑ⁿ b"""
if a == 1:
return 1
elif b == 1:
return a
elif n == 1:
return a**b
else:
return up(a, up(a, b - 1, n), n - 1)
@mr-
Copy link

mr- commented Jul 30, 2013

up 1 _ _ = 1
up a 1 _ = a
up a b 1 = a^b
up a b n = up a (up a (b-1) n) (n-1)

@quchen
Copy link

quchen commented Jul 30, 2013

Memoized version, using the data-memocombinators package

import qualified Data.MemoCombinators as Memo

up = Memo.integral up'
      where up' 1 _ _ = 1
            up' a 1 _ = a
            up' a b 1 = a ^ b
            up' a b n = up a (up a (b-1) n) (n-1)

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