-- Import our simplified Prelude: import SimplePrelude -- Computes the square of an integer number. square :: Int -> Int square x = x * x -- Computes the minimum of two integer numbers. mymin :: Int -> Int -> Int mymin x y = if x < y then x else y -- Computes the factorial of an integer. fac :: Int -> Integer fac n = if n == 0 then 1 else n * fac (n - 1) -- Computes the n-th Fibonacci number (inefficient exponential version). fib :: Int -> Int fib n = if n == 0 then 0 else if n == 1 then 1 else fib (n-1) + fib (n-2) -- Computes the n-th Fibonacci number (efficient version). fib2 :: Int -> Int fib2 n = fib2iter 0 1 n fib2iter :: Int -> Int -> Int -> Int fib2iter fibn fibnplus1 n = if n==0 then fibn else fib2iter fibnplus1 (fibn + fibnplus1) (n-1) -- Computes the n-th Fibonacci number (efficient version with local decl.). fib3 :: Int -> Int fib3 n = iter 0 1 n where iter :: Int -> Int -> Int -> Int iter fibn fibnplus1 n = if n==0 then fibn else iter fibnplus1 (fibn + fibnplus1) (n-1) -- Computes the n-th Fibonacci number (efficient version with local decl.). fib4 :: Int -> Int fib4 n = let iter fibn fibnplus1 n = if n==0 then fibn else iter fibnplus1 (fibn + fibnplus1) (n-1) in iter 0 1 n test = let x = 2 y = let z = x + x*2 in z*z in y - x -- Check if a number is prime. isPrim :: Int -> Bool isPrim n = n > 1 && notDivide (div n 2) where notDivide m = m == 1 || mod n m /= 0 && notDivide (m - 1) -- notDivide n m = m == 1 || (mod n m /= 0 && notDivide n (m - 1))