Inspired by this question:
Is explicit type recursion possible in F#?
type 'a Mu = In of 'a Mu 'a
let unIn (In x) = x
This code unfortunatly gives "Type parameter cannot be used as type constructor.
Remarks: This construct is used in the paper Functional Programming with Overloading and Higher-Order Polymorphism, for example.
Example of usage (taken from here):
type ('a, 'b) ListX =
| Nil
| Cons of 'a * 'b
type 'a List = ListX Mu
It's common to write F# code that recursively processes something with an inner and outer function, as the previous example shows. The inner function uses tail recursion, while the outer function has a better interface for callers.
Answer: The rec keyword is used together with the let keyword to define a recursive function.
In programming terms, a recursive function can be defined as a routine that calls itself directly or indirectly. Using the recursive algorithm, certain problems can be solved quite easily. Towers of Hanoi (TOH) is one such programming exercise.
No, this is not possible. Specifically, generics in F# have the same limitation as the CLR, namely a <T> or an <'a> must have kind " * ". This same limitation is what means you cannot author "type classes" directly in F#, since e.g. "Monad m" would take a higher-kinded argument 'm' (e.g. "* -> *", where e.g. 'list' and 'option' could be instances, those each themselves being generic type constructors), but this is not allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With