-- Computes the factorial of a number fac :: Integer -> Integer fac n = if n == 0 then 1 else n * fac (n - 1) {- fac 2 = if 2 == 0 then 1 else 2 * fac (2 - 1) = if False then 1 else 2 * fac (2 - 1) = 2 * fac (2 - 1) = 2 * fac 1 = 2 * if 1 == 0 then 1 else 1 * fac (1 - 1) = 2 * if False then 1 else 1 * fac (1 - 1) = 2 * 1 * fac (1 - 1) = 2 * 1 * fac 0 = 2 * 1 * if 0 == 0 then 1 else 0 * fac (0 - 1) = 2 * 1 * if True then 1 else 0 * fac (0 - 1) = 2 * 1 * 1 = 2 * 1 = 2 -} -- Computes the n-th Fibonacci number fib1 :: Integer -> Integer fib1 n = if n == 0 then 0 else if n == 1 then 1 else fib1 (n - 1) + fib1 (n - 2) -- Computes the n-th Fibonacci number with accumulators: fib2 :: Integer -> Integer fib2 n = fib2iter 0 1 n where fib2iter fibn fibnp1 n = if n == 0 then fibn else fib2iter fibnp1 (fibn + fibnp1) (n - 1) -- Computes the n-th Fibonacci number with accumulators: fib3 :: Integer -> Integer fib3 n = let fib3iter fibn fibnp1 n = if n == 0 then fibn else fib3iter fibnp1 (fibn + fibnp1) (n - 1) in fib3iter 0 1 n --f :: Int -> Int f :: Float -> Float f x = 2 * (let y = 3 * x z = x + y in 45 * z + y) + 2 * x -- Is a number prime? isPrim :: Int -> Bool isPrim n = (n /= 1) && isSmallerDiv (div n 2) where isSmallerDiv d = d == 1 || ( mod n d /= 0 && isSmallerDiv (d - 1) )