New to SML / NJ. Making a custom insert function
Define a function that, given a list L, an object x, and a positive
integer k, returns a copy of L with x inserted at the k-th position. For
example, if L is [a1, a2, a3] and k=2, then [a1, x, a2, a3] is returned.
If the length of L is less than k, insert at the end. For this kind of
problems, you are supposed not to use, for example, the length function.
Think about how the function computes the length. No 'if-then-else' or any
auxiliary function.
I've figured out how to make a function to find the length of a list
fun mylength ([]) = 0
| mylength (x::xs) = 1+ mylength(xs)
But, as the questions states, I can't use this as an auxiliary function in
the insert function. Also, i'm lost as to how to go about the insert
function? Any help or guidance would be appreciated!
No comments:
Post a Comment