Exercises on Haskell Programming

  1. Write a function bubbleSort that takes a nonempty list of numbers and returns the sorted list of the same numbers using Bubble Sort Algorithm. You may find that writing a helper function and using it in bubbleSort makes your task easier.
  2. Write a function insertSort that takes a nonempty list of numbers and returns the sorted list of the same numbers using Insertion Sort Algorithm. You may find that writing a helper function and using it in insertSort makes your task easier.
  3. Write a function selectSort that takes a nonempty list of numbers and returns the sorted list of the same numbers using Selection Sort Algorithm. You may find that writing helper functions and using them in selectSort makes your task easier.
  4. Write a function rotate1 of type [a] -> [a] that takes a list and returns the same list but with the first element moved to the end. For example, rotate1 [1, 2, 3, 4] should return [2, 3, 4, 1] and rotate1 "cycle" should return "yclec".
  5. Write a function sqsum of type Int -> Int that takes a non-negative Int n and returns the sum of the squares of all the Int’s 0 through n. Your function need not behave well on inputs less than zero.
  6. Write a function rotate of type Int -> [a] -> [a] that takes an Int n and a list as input in curried format, and returns the same list, but with the first element ratated to the end of the list n times. For example, rotate 2 [1, 2, 3, 4, 5, 6] should return [3, 4, 5, 6, 1, 2] and rotate 3 "tiktok" should return "toktik". Your function need not behave well on inputs n less than zero, i.e., you may assume n is non-negative.
  7. Write a function isPrime of type Integer -> Bool that returns True if and only if its integer parameter is a prime number. Make sure that isPrime n returns False if n is less than 2.
  8. Write a function member of type Eq a => a -> [a] -> Bool such that calling member x list returns True if and only if x is an element of list.
  9. Write a function containsRepeats of type Eq a => [a] -> Bool such that calling containsRepeats list returns True if and only if list contains two equal elements next to each other.
  10. A positive integer d is a proper divisor of a positive integer n if d < n and d divides evenly into n. A positive integer n is deficient, perfect, or abundant if the sum of all proper divisors of n is less than, equal to, or greater than n, respectively. Write a function number_type that takes an integer parameter n and returns the string “none”, “deficient”, “perfect”, or “abundant” according to whether n is less than 1, deficient, perfect, or abundant, respectively.
  11. Two distinct positive numbers n and m are amicable if the sum of all the proper divisors of n equals m, and the sum of all the proper divisors of m equals n. Write a function are_amicable that takes a tuple of integers (n, m) as parameter and if both n and m are distince, positive, and amicable, then returns True, otherwise returns False.