So I understand recursion relatively well, however in F# with the ability to create your own sets of data types I am not comprehending how to write a function to reference that particular scenario. Thanks for any help
type 'element mylist = PEANUT | BUTTER of 'element * 'element mylist
let exampleList = BUTTER (1, BUTTER (2, BUTTER (3, PEANUT)))
Attempting to write a tail recursive function that reverses this list?
Here is how I would write it traditionally
let rec helper a b =
match a with
| [] -> b
| h::t -> helper t (h::b)
let rev L = helper L []
Now here is what I've been attempting:
let rec tailrevMylist a L =
match a with
| [] -> []
| PEANUT::t -> tailrevMylist t (BUTTER::L)
let revMylist L =
tailrevMylist L []
*** BEGIN UPDATES/CHANGES ***
let rec tailrevMylist a b =
match a with
| PEANUT -> b
| h::t -> tailrevMylist BUTTER (a, b)
let revMylist L =
tailrevMylist L []
Still getting incorrect types -- attempted to use CONS instead of h but cannot due it expecting a union. *** END UPDATES ***
however when I try and run revMylist exmapleList
I get an error due to the function expecting a 'a mylist list
but having type int mylist
in my test. I need to get the function to expect an int mylist
.
*** SOLUTION ***
let rec tailrevMyList a b =
match a with
| PEANUT -> b
| BUTTER (head, tail) -> (tailrevMyList tail (BUTTER (head, b)))
let revMylist L = tailrevMyList L PEANUT
The definition of a list is essentially:
which you might notice looks very similar to your definition of
mylist
. The only difference is the use of[]
in place ofPEANUT
and::
in place ofBUTTER
, with::
also being an infix operator.You might also notice that this means you're mixing
list
andmylist
constructors in ways that make very little sense. So instead of trying to fix your current implementation I'll just tell you how to mechanically translate yourhelper
function to usemylist
by applying a couple of very simple rules:[]
withPEANUT
.a :: b
withBUTTER (a, b)
.That's really all there is to it. So instead of just giving you the answer I'll leave it to you to derive it yourself using these rules. Good luck!