(i) Code in SML a function removeFirst \'a\'a list -\'a list that removes from t
ID: 3732763 • Letter: #
Question
(i) Code in SML a function removeFirst 'a'a list -'a list that removes from the given list the first occurrence of the given value (if it occurs there at all), but keeps all later occurrences removeFirst 5 [1,5,3,4,3,5,5,7]; val it- [1,3,4,3,5,5,7] : int list (ii) Code a function remove : ,a-> , a list-> ,a list that rem oves from the given list all occurrences of the given value - remove 5 [1,5,3,4,3,5,5,7] val it - [1,3,4,3,7] : int list (iii) Use remove to code a function nub : , , a list ' a list that makes the given list duplicate- free by keeping only the first occurrence of each value in the list. - nub [1,5,3,4,3,5,5,7]; val ít = [1 ,5 , 3, 4, 3,7] : int listExplanation / Answer
(i) removeFirst(var objectToBeRemoved, List list){
for obj in list {
if(objectToBeRemoved == obj) {
list.remove(obj);
break;
}
}
}
(ii) remove(var objectToBeRemoved, List list){
for obj in list {
if(objectToBeRemoved == obj) {
list.remove(obj);
}
}
}
(iii) var other, list; //consider these as 2 different lists with 'list' list containing values and 'other' list a new list to remove duplicates
for(var v: list) {
if other does not contain v {
other.add(v); // this will remove all the duplicates from the given list and give us a duplicate free list.
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.