SOS Sheet
SOS Sheet
For when you’re stuck on a problemo and unsure of how to continue
1. Conceptualise the problem
- What are you trying to do?
- What information are you given, and what do you need to do with this information?
- Have you done a similar problem before? What strategies did you use then?
For example- if we’re trying to delete an element from a list, we’ll want to find the element in the list, somehow split the list in two around this element, then stick these two mini-lists back together.
2. Visualise the problem with an example- try to imagine edge cases, where it’s not as obvious as to what your code should do
- If you’re using an unfamiliar data type (e.g. trees!) make sure you draw some pictures ~\ (^ 0^)/~
- What happens for different inputs? Is there a pattern to what you’re doing?
For our deleting from list example, this could involve writing out a short example list, and then how it will look after having an element removed. It could also be helpful to write out the list in that long-winded (x:(y:(z:[]))) form.
3. Write down the names and type signatures of the functions you’ll need
- TRUST ME, TYPE SIGNATURES HELP!
- Think about what each function will do- will it be able to do its task with the inputs you’ve given it?
When deleting an element in a list, you may want to write an “indexOf” function, and maybe a “split” function too. Make sure your function names are equally clear, so you don’t get lost in the code, and know what you’re trying to achieve.
4. Start writing!
- YOU CAN DO IT!!!
If you’ve written something but it doesn’t feel right…
1. Explain your code to someone else
- Does the logic all make sense? If not, you may need to re-write some functions/change your approach
- If the logic all makes sense, check you’re giving the correct inputs to functions, and manipulating data in the right way
E.g.: “Hi Tony! So this code is going to delete an element from a list by finding the element through recursion, then returning the list after that element.” “Cool stuff Tiggy-senpai, but what about that first part of the list? You’re just throwing that away!” “Oooohhhhhh….”
2. Write a trace (i.e. try an example input)
- Is your function recursive? If so, what is your base case, and what are you recursing over? Make sure your recursion will terminate!
- Have you understood the question correctly? Ensure you are returning what the problem needs!
- Are there any inputs that won’t work (e.g. an empty list, a “Nothing” if your input is a Maybe type…)? How can you deal with these?
E.g. try and delete 1 from [], or 2 from [2,2,3]… what should happen? What do you want to happen?