progs/defs.fun
author Christian Urban <urbanc@in.tum.de>
Sun, 17 Nov 2019 09:18:47 +0000
changeset 691 991849dfbcb1
parent 656 cfc0e730bcda
child 695 484b74bc057e
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
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     6
def pred(x) =
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     7
  if x == 0 then x else x - 1;
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
     9
def add(x, y) =
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    10
  if x == 0 then y else suc(add(x - 1, y));
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    11
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    12
def mult(x, y) =
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    13
  if x == 0 then 0 else add(y, mult(x - 1, y));
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    14
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    15
def pow(x, y) =
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    16
  if y == 0 then 1 else mult(x, pow(x, y - 1));
224
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 fib(n) = if n == 0 then 0 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
             else if n == 1 then 1 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
             else fib(n - 1) + fib(n - 2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    22
def fact(n) =
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    23
  if n == 0 then 1 else n * fact(n - 1);
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
def ack(m, n) = if m == 0 then n + 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
                else if n == 0 then ack(m - 1, 1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
                else ack(m - 1, ack(m, n - 1));
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
def stack_test(x) = x + 1 + 2 + 3 + 4 + 5;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
def div(x, y) = x / y;   //integer division
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
def rem(x, y) = x % y;   //remainder
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    35
def gcd(a, b) =
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    36
  if b == 0 then a else gcd(b, a % b);
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
def is_prime_aux(n, i) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  if n % i == 0 then 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  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
    41
  else 1;
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
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
    44
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
def primes(n) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
  if n == 0 then 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  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
    48
  else primes(n - 1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
def is_collatz(n) =
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
  if n == 1 then 1
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
  else if n % 2 == 0 then is_collatz(n / 2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
  else is_collatz(3 * n + 1);  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
def collatz_aux(n, i) = 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
  if i > n then 0
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
  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
    58
  else collatz_aux(n, i + 1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    59
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
def collatz(n) = collatz_aux(n, 1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    62
def facT(n, acc) =
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    63
  if n == 0 then acc else facT(n - 1, n * acc);
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
//zero(3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
//suc(8)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
//pred(7)
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    69
//write(add(3, 4))
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
//mult(4,5)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
//pow(2, 3)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    72
//fib(20)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
//(write(fact(5)) ; fact(6))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    74
//(write(1) ; 2)
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    75
//write(ack(3, 12))   // for tail-rec test
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
//stack_test(0)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
//(write (div(11, 3)); rem(11, 3))
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
//gcd(54, 24)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
//is_prime(2)
656
cfc0e730bcda updated
Christian Urban <urbanc@in.tum.de>
parents: 626
diff changeset
    80
primes(1000000)
224
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
//collatz(5000)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    82
//facT(6, 1)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83