Frage

I have looked at many similar questions and searched google for a few hours and I am getting nowhere.

I am trying to access my binary tree module which is in my Tree.fs file from my Program.fs file and in the explorer window I have moved the Tree.fs file above the Program.fs file as suggested in almost all of the other similar questions but this is till not working for me.

My Tree.fs file

namespace Lab2

type Elem = System.IComparable

type Tree = E | T of Tree * Elem * Tree

module BinaryTree =
    let emptyTree = E

    let rec add = function
                    | x, E -> T(E, x, E)
                    | x, T(a, y, b) when x < y -> T(add(x, a), y, b)
                    | x, T(a, y, b) when y < x -> T(a, y, add(x, b))
                    | _, s -> s

    let rec addList = function
                        | [], T(a, y, b) -> T(a, y, b)
                        | x::xs, T(a, y, b) when x < y -> addList(xs, T(add(x, a), y, b))
                        | x::xs, T(a, y, b) when y < x -> addList(xs, T(a, y, add(x, b)))
                        | _, s -> s


    let rec preorder = function
                        | T(a, y, b) when a <> E -> y::preorder a
                        | T(a, y, b) when b <> E -> y::preorder b
                        | _ -> []

    let rec inorder = function
                        | T(a, y, b) when a <> E -> y::inorder a
                        | T(a, y, b) when b <> E -> y::inorder b
                        | _ -> []

    let rec postorder = function
                        | T(a, y, b) when b <> E -> y::preorder b
                        | T(a, y, b) when a <> E -> y::preorder a
                        | _ -> []

    let rec treeToString = function
                            | T(a, y, b) when a <> E -> "(node " + y.ToString() + treeToString a
                            | T(a, y, b) when b <> E -> y.ToString() + treeToString b + ")"
                            | _ -> "Empty"

My Program.fs file

module Lab2.Program

open Lab2.BinaryTree

let tree = addList([5; 2; 1; 6; 7], emptyTree);;

let inorder_tree = inorder(tree);;

let char_tree = addList(['e';'b';'a';'f';'g'], emptyTree);;

let inorder_char_tree = inorder(char_tree);;

let print = treeToString(char_tree);;

I have tried not using the Lab2 namespace in both of my files and instead just using both files as modules but this still gives me the same error.

I have no blue or red lines in my code, no errors untill I debug

The error only happens on the following line

let tree = addList([5; 2; 1; 6; 7], emptyTree);;

So when debugging it looks like this

Program.fs(66,6): error FS0039: The namespace or module 'Lab2' is not defined
> val inorder_tree : Elem list = []
> val char_tree : Tree = E
> val inorder_char_tree : Elem list = []
> val print : string = "Empty"
War es hilfreich?

Lösung

If you compile and run the project, there is no error. Based on your results, I guess you manually copied contents of two files into F# Interactive except the first lines with namespace and module declaration. In this case, Lab2 namespace wasn't known by F# Interactive hence the error message. A quick fix is to change

open Lab2.BinaryTree 

to

open BinaryTree

It works on both fsc and fsi because Program and BinaryTree modules share the parent namespace Lab2.

However, you are confused between running the project and executing it in F# Interactive. A common solution is to create a Tree.fsx file and load corresponding modules in it:

#load "Tree.fs"    
open Lab2.BinaryTree

let tree = addList([5; 2; 1; 6; 7], emptyTree);;
let inorderTree = inorder(tree);;
let charTree = addList(['e';'b';'a';'f';'g'], emptyTree);;
let inorderCharTree = inorder(charTree);;
let print = treeToString(charTree);;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top