Thanks both of you. Now the picture is a little bit clearer.

By on 3/26/2009 2:41 PM ()

1.
There is no printInt function that I know of that prints only integers. You'll have to define them yourself.
If you want to generate a string containing your integer value, you can use the sprintf/sprintfn functions. These functions 'print' to a string value.

Example:

let addToS S intValue = sprintf "%s%i" S intValue

would work like:

> addToS "Answer to everything --> " 42;;
val it : string = "Answer to everything --> 42"

The great thing about the sprintf functions is that it's very easy to generate formatted strings without having to mess around with string concatenations yourself.

2.
The problem here is that standard print functions are a bit weird. They don't </i>return<i> stuff, they </i>do<i> stuff. The signature of the printi function is (int -> unit). This means that it takes an int and returns the unit value, while printing something to the console (a side effect of the printi function). In this case of functions that have side effects and return (), you should use Seq.iter instead of Seq.map. So {1..10} |> Seq.iter printi will print the numbers in your console application. I don't know exactly why FSI and the compiled application behave differently when using Seq.map. I'm rather new to this F# game myself as well.

By on 3/26/2009 8:20 AM ()

2: The reason if you execute it line by line in FSI that it sorta works is this:

> seq {1 .. 10} |> Seq.map printi;;
1
2
3
4
5
val it : seq<unit> = seq [null; null; null; null; ...]

That's returning a value (unit seq). Since FSI wants to eval the value and say what "it" is, it has to partially enumerate the sequence. So, it's a pure side effect of FSI showing you the result of each expression you type in. Remember that Seq functions are lazy evaluated in most cases, map included. You want Seq.iter to force it to iterate through them.

By on 3/26/2009 9:15 AM ()
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