often it helps to come up with some kind of invariant and write down some conservation laws for it. Note here that
reverse xs = reverse xs ++ [] reverse (x:xs) = (reverse xs ++ [x]) ++ [] = reverse xs ++ ([x] ++ []) = reverse xs ++ (x:[]) reverse (x:(y:xs)) = = reverse (y:xs) ++ (x:[]) = reverse xs ++ (y:x:[]) ...... reverse (x:(y:...:(z:[])...)) = = reverse [] ++ (z:...:y:x:[])
therefore if we define
reverse xs = rev xs [] where rev (x:xs) acc = rev xs (x:acc) rev [] acc = acc
we are configured. :) That is, to call rev ab concatenation of the inverse of a and b preserved when we transform the taking of the head element from a and adding it to b until a is empty and then it's just b . This can even be expressed using a higher order until function in accordance with the English description, since
{-
We can also define now, for example. a revappend using the same internal function with a little tweaking as we call it:
revappend xs ys = rev xs ys where rev (x:xs) acc = rev xs (x:acc) rev [] acc = acc
source share