CIT 594 Example Funl Functions
Spring 2013, David Matuszek

These functions have been tested and seem to work.

Notice that:

def eq x y =
    if x - y then 0
             else if y - x then 0
                           else 1
                  end
    end
end

def gt x y = x - y end

def lt x y = y - x end

def ge x y =
    if gt(x, y)
    then 1
    else if eq(x, y)
         then 1
         else 0
         end
    end
end

def le x y = if lt(x, y) then 1
                         else if eq(x, y) then 1
                                          else 0 
                              end
             end
end

def ne x y = if eq(x, y) then 0 else 1 end end

def factorial n =
	if n - 1 then n * factorial(n - 1)
	         else 1
	end
end

def or x y =
	if x then x else y end
end

def fibonacci n =
	if or(eq(n, 1), eq(n, 2))
	then 1
	else fibonacci(n - 1) + fibonacci(n - 2)
	end
end

def abs x = if x then x else 0 - x end end

def nearly_equal x y = lt(abs(x - y), 0.00001) end

def sqrt x = sqrt_helper(x, 1) end

def sqrt_helper x y =
    val y_squared = y * y,
    if nearly_equal(x, y_squared)
    then y
    else val next_guess = (y + x / y) / 2,
         sqrt_helper(x, next_guess)
    end
end

def summer =
	val total = 0,
	summer_helper(total)
end

def summer_helper total =
    val n = read "Give me a number (0 to stop): ",
    if eq(n, 0) then total else summer_helper(total + n) end
end

def power x n = if eq(n, 1) then x else x * power(x, n - 1) end end

def identity x = x end

def add x y = x + y end

def multiply x y = x * y end

def square x = multiply(x, x) end

def for first last apply combine =
    val init = apply(first),
    if ge(first, last)
        then init
        else combine(init, for((first +1), last, apply, combine))
    end
end

def factorial2 n = for(1, n, identity, multiply) end
    
def sum_of_1_to_n function n =
	if eq(n, 0) then 0
	            else function(n) + sum_of_1_to_n(function, n - 1)
     end
end

def square x = x * x end

def sum_of_squares first last = for(first, last, square, add) end
Funl>> load
Functions loaded: [sqrt_helper, for, or, sqrt, factorial2,
  multiply, ge, sum_of_squares, lt, add, sum_of_1_to_n,
  identity, summer_helper, abs, le, ne, power, summer, gt,
  factorial, square, fibonacci, eq, nearly_equal]
  
Funl>> 5
5.0

Funl>> val a = 10
10.0

Funl>> a
10.0

Funl>> eq(a, 10)
1.0

Funl>> eq(a, 11)
0.0

Funl>> gt(a, 10)
0.0

Funl>> gt(a, 5)
5.0

Funl>> ge(a, 5)
1.0

Funl>> factorial(10)
3628800.0

Funl>> or(0, 1)
1.0

Funl>> or(lt(a, 5), gt(a, 5))
5.0

Funl>> fibonacci(0)
Exceeded recursion depth limit of 1000.

Funl>> fibonacci(1)
1.0

Funl>> fibonacci(2)
1.0

Funl>> fibonacci(10)
55.0

Funl>> abs(5)
5.0

Funl>> abs(0 - 5)
5.0

Funl>> nearly_equal(7, 7.00000000001)
9.999989999999173E-6

Funl>> sqrt(2)
1.4142156862745097

Funl>> sqrt(10)
3.162277665175675

Funl>> sqrt(100)
10.000000000139897

Funl>> summer()
Give me a number (0 to stop):  3
Give me a number (0 to stop):  2
Give me a number (0 to stop):  100
Give me a number (0 to stop):  0
105.0

Funl>> power(2, 10)
1024.0

Funl>> identity(3.5)
3.5

Funl>> square(7)
49.0

Funl>> for(-10, 10, abs, add)
  In REPL: Error in argument list.
  
Funl>> for(0 - 10, 10, abs, add)
110.0

Funl>> factorial2(10)
3628800.0

Funl>> sum_of_1_to_n(square, 10)
385.0

Funl>> sum_of_squares(1, 10)
385.0

Funl>>