Haskell - Functions with numbers P77907


Statement
 

pdf   zip

thehtml

In this problem you have to implement several functions in Haskell. You do not need to ask permission to write auxiliary functions, of course you can!

  1. Write a function absValue :: Int -> Int that, given an integer, returns its absolute value.
  2. Write a function power :: Int -> Int -> Int that, given an integer x and a natural p, returns thep-th power of x, that is, xp.
  3. Write a function isPrime :: Int -> Bool that, given a natural, tells whether it is a prime number or not.
  4. Write a function slowFib :: Int -> Int that returns the n-th element of the Fibonacci sequence using the recursive algorithm that defines it (f(0)=0, f(1)=1, f(n)=f(n−1)+f(n−2) for n≥ 2).
  5. Write a function quickFib :: Int -> Int that returns the n-th element of the Fibonacci sequence using a more efficient algorithm.

Scoring

Each function scores 20 points.

Public test cases
  • Input

    absValue (-666)
    power 2 3
    isPrime 17
    slowFib 5
    quickFib 40
    

    Output

    666
    8
    True
    5
    102334155
    
  • Information
    Author
    Albert Rubio / Jordi Petit
    Language
    English
    Translator
    Jordi Petit
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    Haskell
    User solutions
    Haskell