Haskell - Functions with lists P25054


Statement
 

pdf   zip

thehtml

In this problem you have to define some functions about lists in Haskell.

  1. Define a function myLength :: [Int] -> Int that, given a list of integers, returns its length.
  2. Define a function myMaximum :: [Int] -> Int that, given a non-empty list of integers, returns its maximal element.
  3. Define a function average :: [Int] -> Float that, given a non-empty list of integers, returns its average.
  4. Define a function buildPalindrome :: [Int] -> [Int] that, given a list, returns its palindrome that starts with the reserved list.
  5. Define a function remove :: [Int] -> [Int] -> [Int] that given a list of integers x and a list of integers y, returns x after having removed all the ocurrences of the elements in y.
  6. Define a function flatten :: [[Int]] -> [Int] that flattens a list of lists yielding a single list of elements.
  7. Define a function oddsNevens :: [Int] -> ([Int],[Int]) that, given a list of integers, returns two lists: Onw with all the even numbers and one with all the odd numbers, each of them in the same relative order as in the original list.
  8. Define a function primeDivisors :: Int -> [Int] that returns the list of prime divisors of a non-zero natural.

Scoring

Esch function scores 12 points and the sample 4.

Public test cases
  • Input

    myMaximum [4,3,1,5,4,5,2]
    average [1,2,3]
    buildPalindrome [2,4,6]
    flatten [[2,6],[8,1,4],[],[1]]
    remove [1,4,5,3,4,5,1,2,7,4,2] [2,4]
    myLength [1,3..10]
    oddsNevens [1,4,5,3,4,5,1,2,7,4,2]
    primeDivisors 255
    

    Output

    5
    2.0
    [6,4,2,2,4,6]
    [2,6,8,1,4,1]
    [1,5,3,5,1,7]
    5
    ([1,5,3,5,1,7],[4,4,2,4,2])
    [3,5,17]
    
  • Information
    Author
    Albert Rubio / Jordi Petit
    Language
    English
    Translator
    Jordi Petit
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    Haskell
    User solutions
    Haskell