progs/defs.rec
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 24 Nov 2014 00:42:04 +0000
changeset 311 6719e8d10a0d
parent 224 70198792c2aa
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
def zero(x) = 0;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
def suc(x) = x + 1;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
def pred(x) = if x == 0 then x else x - 1;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
def add(x, y) = if x == 0 then y else suc(add(x - 1, y));
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
def mult(x, y) = if x == 0 then 0 else add(y, mult(x - 1, y));
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
def pow(x, y) = if y == 0 then 1 else mult(x, pow(x, y - 1));
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
def fib(n) = if n == 0 then 0 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
             else if n == 1 then 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
             else fib(n - 1) + fib(n - 2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
def fact(n) = if n == 0 then 1 else n * fact(n - 1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
def ack(m, n) = if m == 0 then n + 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
                else if n == 0 then ack(m - 1, 1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
                else ack(m - 1, ack(m, n - 1));
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
def stack_test(x) = x + 1 + 2 + 3 + 4 + 5;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
def div(x, y) = x / y;   //integer division
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
def rem(x, y) = x % y;   //remainder
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
def gcd(a, b) = if b == 0 then a else gcd(b, a % b);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
def is_prime_aux(n, i) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
  if n % i == 0 then 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  else if (i * i) <= n then is_prime_aux(n, i + 1)  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  else 1;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
def is_prime(n) = if n == 2 then 1 else is_prime_aux(n, 2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
def primes(n) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  if n == 0 then 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
  else if is_prime(n) == 1 then (write n; primes(n - 1)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
  else primes(n - 1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
def is_collatz(n) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
  if n == 1 then 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  else if n % 2 == 0 then is_collatz(n / 2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  else is_collatz(3 * n + 1);  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
def collatz_aux(n, i) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
  if i > n then 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  else if is_collatz(i) == 1 then (write i; collatz_aux(n, i + 1)) 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  else collatz_aux(n, i + 1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
def collatz(n) = collatz_aux(n, 1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
def facT(n, acc) = if n == 0 then acc else facT(n - 1, n * acc);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
//zero(3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
//suc(8)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
//pred(7)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    62
//add(3, 4)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    63
//mult(4,5)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
//pow(2, 3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
//fib(20)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
//(write(fact(5)) ; fact(6))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
//(write(1) ; 2)
311
6719e8d10a0d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 224
diff changeset
    68
ack(3, 12)   // for tail-rec test
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
//stack_test(0)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
//(write (div(11, 3)); rem(11, 3))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
//gcd(54, 24)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
//is_prime(2)
311
6719e8d10a0d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 224
diff changeset
    73
//primes(1000000)
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
//collatz(5000)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
//facT(6, 1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76