Im currently learning Haskell for University and doing old Assignments. Regarding a Task I have a question. Im supposed to write a Function that Takes an Int a Char and a List and puts the Char at the beginning and at an intervall given by the number into the List, should the List be divisable by the given Int the Char should also be put at the end od the list.
like this
> example_wrap_1 = wrap 1 '+' "a" == "+a+"
> example_wrap_2 = wrap 2 '+' "a" == "+a"
> example_wrap_3 = wrap 2 '+' "abcd" == "+ab+cd+"
Now I have some code that kinda works it just loops forever.
wrap _ _ [] = []
wrap n y xs = y : (insertAfter n xs) where
insertAfter 0 [] = y : wrap n y xs
insertAfter m [] = []
insertAfter m (x:xs) = x : insertAfter (m-1) xs
the basecase is never reached I understand as much. Why isnt it reached? Is the List Im passing the wrap function in the pattern of insertAfter not the reduced List after insertAfter has run trough it m times? Hints on how to solve this are appreciated or the solution too.
Thanks in advance.