a. Write a function to determine if the elements of a list form a palindrome, palindrome [Int] -> Bool
. For example:
palindrome [ 1, 2, 2, 3, 3] = False
palindrome [ 1, 2, 3, 2, 1] = True
palindrome [3] = True
palindrome [] = True
b. A permutation of a list is another list with the same elements, but in a possibly different order. For example, [1,2,1] is a permutation of [2,1,1], but not of [1,2,2]. Write a function
isPermutation :: [Int] -> [Int] -> Bool
that returns True if its arguments are permutations of each other. For examples:
isPermutation [] [] = True
isPermutation [1,2,1] [2,1,1] = True
isPermutation [1,2,1] [2,1,2] = False
isPermutation [1,2,1] [2,1,1,2] = False
Hint: define a function, removeOnce:: Int->[Int]->[Int]
, that removes the first occurrence of an element from a list, and use it to implement isPermutation
. For examples:
removeOnce 3 [1,3,5,3,4] = [1,5,3,4]
removeOnce 5 [1,2,3,3,4] = [1,2,3,3,4]
removeOnce 3 [] = []