Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8526399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:15:43+00:00 2026-06-11T08:15:43+00:00

I am trying to represent a tree data-structure in F# using the following type:

  • 0

I am trying to represent a tree data-structure in F# using the following type:

type Node = 
 val mutable left: Node option
 val mutable right: Node option
 val mutable value: int 

 new (l, r, v) = {left = l; right = r; value = v}

I can create a tree like this:

let root = new Node (Some (new Node (None, None, 2)), Some (new Node (None, None, 3)), 1)

I now want to traverse using pattern matching. However, i dont know how to pattern match objects. I tried something like this:

let rec traverse r =
  match r with
  | Node (None, None, v) -> printfn "%d" v

and stopped right there because the pattern-matching is wrong.

EDIT:
After Petricek’s answer, I did not use all his suggestions because I haven’t read about discriminated unions and other class features yet. I will implement once i know more about these things. But for time-being, this is how it looks:

type Node = 
 val left: Node option
 val right: Node option
 val value: int 

 new (l, r, v) = {left = l; right = r; value = v}

let (|Node|) (nd: Node) = (nd.left, nd.right, nd.value)

let root = new Node (Some (new Node (None, None, 2)), Some (new Node (None, None, 3)), 1)

let rec traverseLRtR r =
  match r with
  | Node (None, None, v) -> printfn "%d" v
  | Node (left, right, v) -> traverseLRtR left.Value; traverseLRtR right.Value; printfn "%d" v; 

traverseLRtR root

Output is:

2
3
1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:15:44+00:00Added an answer on June 11, 2026 at 8:15 am

    Your Node type needs to provide members for reading the left and right fields. The most idiomatic F# way to write such object is to use:

    type Node(left:Node option, right:Node option, value : int) =
      member x.Left = left
      member x.Right = right 
    

    I removed the mutable attribute on the fields, because you were not mutating them in your example (but you can use get & set properties to make them mutable, if that’s what you want). I also changed the code to use implicit constructor syntax, which is simpler.

    As a next step, you can write an active pattern that returns the component of a node:

     let (|Node|) (nd:Node) = (nd.Left, nd.Right)
    

    However, is there any reason (i.e. C# compatibility) why you do not use F# discriminated union? This way, you can define tree data structure really easily and you get pattern matching for free:

    type Node = Node of Node option * Node option * int
    

    Then you can construct nodes by writin Node(None, Some ..., 42) and pattern match on them using the pattern you wrote.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to represent a graph using disjoint union and record. The following
I am trying to represent to genetic variation data in a database for my
I am trying to deserialize a list of key value pairs that represent a
The Zipper data structure is great when one wants to traverse a tree and
I am trying to represent the maximum 64-bit unsigned value in different bases. For
I'm trying to understand how to best represent data which eventually I'll pull out
I'm currently trying to build a lookup table for a huffman tree using a
I am trying to write a typed abstract syntax tree datatype that can represent
I'm trying to use a TreeCtrl to represent a folder structure. For each folder
I'm trying to represent the following in an XSLT if: if (status != Disconnected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.