Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0e9f3f5c52
Fetching contributors…

Cannot retrieve contributors at this time

file 55 lines (42 sloc) 1.214 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
include "Prelude.e"

{- Depending how much memory you have, you may need to reduce 'nine' -}

main () -> Unit =
    printInt(natToInt(fact(nine)))

natrec (x:Data, z:Any, s:Fun) -> Any = natrectail(x,s,z)

natrectail (x:Data, s:Fun, acc:Data) -> Any =
  case x of {
     Con 0 () -> acc
   | Con 1 (k:Data) -> natrectail(k,s,s(k,acc))
  }

zero () -> Data = Con 0 ()
one () -> Data = Con 1 (zero)
two () -> Data = Con 1 (one)
three () -> Data = Con 1 (two)
four () -> Data = Con 1 (three)
five () -> Data = Con 1 (four)
six () -> Data = Con 1 (five)
seven () -> Data = Con 1 (six)
eight () -> Data = Con 1 (seven)
nine () -> Data = Con 1 (eight)
ten () -> Data = Con 1 (nine)

{-
natToInt (x:Data) -> Int =
  case x of {
     Con 0 () -> 0
   | Con 1 (k:Data) -> 1+natToInt(k)
  }
-}

natToInt (x:Data) -> Int =
    natrec(x,0,n2isuc)

n2isuc (k:Data, ih:Int) -> Int = 1+ih

plus (x:Data, y:Data) -> Data =
    natrec(x,y,plussuc)

plussuc (k:Data, ih:Data) -> Data = Con 1 (ih)

mult (x:Data, y:Data) -> Data =
  natrec(x, Con 0 (), multsuc(y))

multsuc (y:Data, k:Data, ih:Data) -> Data = plus(y, ih)

fact (x:Data) -> Data =
  case x of {
     Con 0 () -> one()
   | Con 1 (k:Data) -> mult(x, fact(k))
  }
Something went wrong with that request. Please try again.