Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fsharp record in discriminated union

Tags:

types

record

f#

I was trying this construct:

type Tree<'a> =
| Leaf 
| Node of { value:'a ; left:Tree<'a> ; right:Tree<'a> }

But f# does not seem to support this (strange). But then how to do this with a separate record type, one then gets stuck in a circular dependency.

like image 700
Rob Frohwein Avatar asked Jan 29 '26 02:01

Rob Frohwein


1 Answers

Use the keyword and to deal with circular dependencies:

type Rec<'a> = { value:'a ; left:Tree<'a> ; right:Tree<'a> }

and Tree<'a> =
| Leaf 
| Node of Rec<'a>
like image 159
Gus Avatar answered Jan 31 '26 17:01

Gus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!