Warming Up with Lists

Prelude

You’ve been accompanying Tony on his garbage collections for some weeks now. You’ve come so far- you have the quickest sorting algorithm for rubbish, and garbage collectors far and wide admire your efficient use of lazy evaluation to plan your collection routes. You’ve even implemented compression in your weekly sojourns- something no-one had dared to try before. But you have still more to learn from the master… (Insert name here), Tony says to you one morning, as you do the rounds. “I want to test your knowledge- are you ready?” Your heart races- you feel dizzy with uncertainty- can you do this? What if you fail? Will your pristine reputation go to ruin? You grip your copy of “The Craft of Functional Programming” tightly to your chest. And gazing at the falling rubbish- tattered, scattered dreams, long ago forgotten in fear by those who had once cherished them- you let your fear go. What does it mean to be ready? Who cares? Why not just give it a shot? You feel strong- capable- completely functional.

“I am ready, Tony-sensei.” You say firmly. He smiles proudly at you, putting his foot to the floor. The truck’s roar echoes the excitement in his words as he cries, “Then let’s delete some elements from lists!!!”

Deleting Elements from a List

  1. Write a function that finds the position of an element in a list, and returns this index

  2. Write a function that splits a list at an index ‘n’ (you can keep the element at this index in either list) and outputs the two lists as a tuple

  3. Write a function that deletes an element ‘x’ from a list and returns the list without that element (hint: using your previous functions may be helpful)