Introduction to Programming

Assignment 2


Date: Fri, 27 August 2004

Due on: Fri, 3 September 2004


  1. Define the function majority that takes as input a list of values of type Bool and returns True if half or more of them are True. For instance:
               majority [True,True,False,False] = True
               majority [False,True,False] = False
               
  2. Define the function remdup that takes as input a list of Int and removes all duplicate values from the list. The function should retain the first copy of each duplicate value. For instance:
               remdup [3,1,3,2,1] = [3,1,2]
               remdup [3,4,1] = [3,4,1]
               
  3. A subsequence of a list is a sublist of elements of the original list in the same order, but not necessarily contiguous. For instance, [3,1,2] is a subsequence of [3,1,7,2,3] but [3,1,2] is not a subsequence of [1,3,2,3,7]. Define the function subseqs that takes as input a list of Int and returns the list of all subsequences of that list. The subsequences may be listed out in any order, but there should be no duplicates. For instance:
               subseqs [1,2] = [[],[1],[1,2],[2]]
               subseqs [1,2,3] = [[],[1],[1,2],[1,3],[1,2,3],[2],[2,3],[3]]
               
  4. One String is a substring of another if the first String appears as a contiguous part of the second String. For instance "abra" is a substring of "abracadabra" but "brad" is not a substring of "abracadbra". The first example shows that a substring may match more than one portion of the second string. Write the function strpos that takes as input two Strings and returns the earliest position in the second String where the first String occurs as a substring. strpos should return -1 if the first String is not a substring of the second String. The positions of a String of length n are defined to run from 0 to n-1. For instance:
               strpos "abra" "abracadabra" = 0
               strpos "cad" "abracadabra" = 4
               strpos "brad" "abracadabra" = -1
               

Madhavan Mukund
Last modified: Fri Aug 27 13:34:45 IST 2004