Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Incomplete structured construct

Tags:

f#

I recently started learning F# and today I got error that i can't get rid of. I have following code:

open System

[<EntryPoint>]
let main argv =

    type BinaryTree =
        | Node of int * BinaryTree * BinaryTree
        | Empty

    let rec printInOrder tree = 
        match tree with
        | Node (data, left, right)
            -> printInOrder left
               printfn "Node %d" data
               printInOrder right
        | Empty
            -> () 

    let binTree = 
         Node(2, 
            Node(1, Empty, Empty),
            Node(4, 
                Node(3, Empty, Empty),
                Node(5, Empty, Empty)
            )
    )

printInOrder binTree
0

With this code I'm getting following error:

Incomplete structured construct at or before this point in binding

Unfortunately I have no idea how to fix it. This is code example from book Programming F# 3.0.

I would very much appreciate any kind of answer that could help me understand how to avoid these kind of mistakes in future.

like image 646
Divh Avatar asked Nov 15 '13 19:11

Divh


1 Answers

You need to define the types and functions in the proper context (outside of the function).

open System 

type BinaryTree =
    | Node of int * BinaryTree * BinaryTree
    | Empty  

let rec printInOrder tree = 
    match tree with
    | Node (data, left, right)
        ->  printInOrder left
            printfn "Node %d" data
            printInOrder right
    | Empty
        -> () 

let binTree = 
    Node(2, 
        Node(1, Empty, Empty),
            Node(4, 
                Node(3, Empty, Empty),
                Node(5, Empty, Empty)
            )
    )

[<EntryPoint>]
let main argv =  
    printInOrder binTree
    0
like image 186
ChaosPandion Avatar answered Sep 21 '22 04:09

ChaosPandion