I'm trying to write a function in haskell that reverses lists recursively. I wrote a helper function that takes the original list and an empty list then transfers elements from the first one to the other in a LIFO pattern.
Here's what I have:
myreverse :: [a] -> [a]
myreverse list = myflip list []
myflip :: [a] -> [a] -> [a]
myflip list1 newList
| null list1 = newList
| otherwise = myflip (tail list1) ((head list1) : newList)
I know there's a built-in function that does it for me but the requirement is that I only use head, tail, elem and null (No pattern matching either). So my question is: is there a better solution where I only have one function, myreverse, that consumes only one list? (That meets the requirements above, of course)
Thanks!
(:)
, but it's not on the list of allowed functions ;-) – pat(:)
is a data constructor, not a type constructor. Its type is(:) :: a -> [a] -> [a]
, so it is logically a function due to its type containing at least one arrow. The wikipedia page on algebraic data types describes a data constructor as being somewhat similar to a function, but then in the next sentence says that it is logically a function. Of course, if we want to have any hope of solving the problem, we have to allow its use! – pat