Have you tried replacing the call to printfn with a call to Console.WriteLine? It could be that the printfn is doing something strange when writting to the console and so the output is not being captured by the standard .NET features.

Hope that helps,
Rob

By on 12/30/2007 11:04 PM ()

Thanks for answering Rob, by the way I got your book, I am enjoying it very much.

I tried your suggestion, it didn't resolve the issue, but it opened some interesting insights. Here's what I changed:

let answer = numbersToText()

System.Console.WriteLine("The Answer: {0}",answer)

And got the following output:

The Answer: numtotext+answer@55

By on 12/31/2007 6:57 AM ()

Nate, I think you've got some typos in your code that are thowing things off.

I what you meant to write was:

1
2
3
4
let numbersToText d =
     printfn "Answer: %s" (numToText d);

numbersToText 24; // Prints "Answer: Twenty-Four"

By placing the () after the numbersToText name, you declared it a "unit" type (function that doesn't return a value). I think what you wanted was to declare a "int -> unit" -- a function that takes an int (in this case, d) and doesn't return anything.

If you want to think of it this way, (numToText d) simply calls the numToText function with d as its argument, which returns a string. This string is then printed to the console with printfn, using the specified format.

To call the numbersToText function, you just place an argument (in this case, 24) after the name.

Hope that helps! :)

By on 12/31/2007 8:27 AM ()

Thanks Matt - that explaination helps.

If I remove the unit declaration numbersToText() and the () from the function call, when I compile I get the following:

warning: Main module of program is empty: nothing will happen when it is run.

However, I still get the output in Nunit and if I put the code block into fsi, everything works as well

What do I need to do in order for my function to be called when it's run from the exe?

By on 12/31/2007 10:58 AM ()

I just tested it with this code and compiled an EXE and didn't run into any problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#light
module numtotext

let LessThanAThousand t =
     t <= 999

let rec numToText n =
     match n with
     | 1 ->  "One"
     | 2 ->  "Two"
     | 3 ->  "Three"
     | 4 ->  "Four"
     | 5 ->  "Five"
     | 6 ->  "Six"
     | 7 ->  "Seven"
     | 8 ->  "Eight"
     | 9 ->  "Nine"
     | 10 ->  "Ten"
     | 11 ->  "Eleven"
     | 12 ->  "Twelve"
     | 13 ->  "Thirteen"
     | 15 ->  "Fifteen"
     | 20 ->  "Twenty"
     | 30 ->  "Thirty"
     | 40 ->  "Fourty"
     | 50 ->  "Fifty"
     | 60 ->  "Sixty"
     | 70 ->  "Seventy"
     | 80 ->  "Eighty"
     | 90 ->  "Ninety"
     | n when n <= 19 ->  numToText (n % 10) ^"teen"
     | n when n <= 99 -> numToText (n / 10 * 10) ^"-" ^numToText (n % 10)
     | n when LessThanAThousand n && (n % 100) = 0 -> numToText (n / 100) ^" Hundred"
     | n when LessThanAThousand n -> numToText (n / 100 * 100) ^" "^numToText (n % 100)
     | n when (n % 1000) = 0 -> numToText (n / 1000) ^" Thousand"
     | n when n <= 99999 -> numToText (n / 1000 * 1000) ^" " ^numToText (n % 1000)
     | n -> "Unknown"
     
let numbersToText d =
  printfn "Answer: %s" (numToText d);

numbersToText 24;

That just runs the numbersToText function on "24" when the program is run.

Out of curiosity, how are you interfacing this with NUnit? Where are you placing your [TestFixture] and [Test] attributes?

By on 12/31/2007 11:23 AM ()

Out of my own curiosity, I tried to run a test in NUnit. Here's the reference I was looking at: [link:weblogs.asp.net]

I changed the build to produce a DLL instead of an EXE, though that's not necessary. Then I copied nunit.framework.dll into the project directory, and added "-r nunit.framework.dll" to the options.

After the "module" declaration, I added "open NUnit.Framework;" to get access to the NUnit stuff.

Then I added this class at the end of the file:

1
2
3
4
5
6
7
8
9
10
11
12
13
[<TestFixture>]
type NumbersToTextTest = class
    new() = {}
    
    [<Test>]
    member x.NumToTextTest() =
      printfn "Beginning Test1";
      let input = 24;
      let output = numToText input;
      printfn "Input: %d, Output: %s" input output;
      Assert.AreEqual("Twenty-Four", output);
      printfn "End of Test1";
end

Then I compiled it and opened it up in the NUnit console, and the tests showed up and ran perfectly.

Note that no output is produced by the Assert function. Any text that you want to see in the console (whether it's from using printfn or Console.WriteLine, etc) you have to explicitly write code for.

From my tests, output from Console.WriteLine and printfn (and variants) work exactly the same.

Hope that helps!
-Matt

By on 12/31/2007 11:49 AM ()

Funny enough, I was writing a separate post on a sample of writing F# code using TDD techinques and NUnit, but the session timed out and I lost it all. So, I'm working on re-writing that.

I also get my console outs from within the NUnit console, but I'm more curious on when I run my app from the compiled exe, I get no output.

Thanks for all your input, it's much appreciated!

By on 12/31/2007 12:03 PM ()

As usual, I overlooked on thing in your examples, the ";" at the end. I updated my code for a call to numbersToText 4; and the exe output "four". Now all I need to figure out is how to get the command parm passed into the function I'll be all set!

By on 12/31/2007 12:26 PM ()

Awesome! Glad it worked.

As for the command-line arguments: [link:cs.hubfs.net]

By on 12/31/2007 12:32 PM ()

Just an fyi - here's the last bit. Thanks again!

let number = int_of_string Sys.argv.[1]

numbersToText number;

By on 12/31/2007 2:20 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper