-- 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 -> Int 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)