Wednesday, May 25, 2011
Create Open XML Spreadsheet in F#
Tuesday, May 24, 2011
Create Open XML Word document in F#
Full name: Snippet.testString
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
Full name: Snippet.printXml
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Full name: Snippet.createBody
val string : 'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
Full name: Microsoft.FSharp.Core.Operators.ignore
Full name: Snippet.createDocument
Full name: Snippet.createWordprocessingDocument
type: string
implements: System.IComparable
implements: System.ICloneable
implements: System.IConvertible
implements: System.IComparable
implements: seq
implements: System.Collections.IEnumerable
implements: System.IEquatable
Full name: Microsoft.FSharp.Core.Operators.using
type: 'a
implements: System.IDisposable
Full name: Snippet.result3
public static void CreateWordprocessingDocument(string filepath)
{
// Create a document by supplying the filepath.
using (WordprocessingDocument wordDocument =
WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text("Create text in body - CreateWordprocessingDocument"));
}
}
Monday, September 29, 2008
F# Winform
When you want to make a F# winform application and you don’t want to open an extra console when you start the application.
How can you do this (F# version 1.9.6.2)?
1. Create a F# application:
[Edit- thanks Brian ]
2. Open the project properties
3. In the application tab change the output type
4. Add reference to the System.Windows.Forms
5. Add your code.
6. Run your app
7. Close.
8. Add your own details.
Monday, September 22, 2008
Creating a parser with F#
I want to write my own html-parser. That is one of the reasons I am learning F#. I have read all the documentation that is available in the three published F# books and tried to build some simple parsers myself and downloaded the examples of Chris Smith.
This resulted in a naïve development approach: start with lexer, than create the parser, compose the AST and finally build your application that consumes the AST.
One of the lessons learned: when you want to create a parser you have to start with analyses of the text type. In case you can step through the text and can determine the action after each token a lexer is what you need. In case the action is determined by server tokens you need a parser.
Example of program that needs a lexer:
- A program that replacing digits by roman numerals.
- A simple encryptor
Example of program that needs a lexer and a parser:
- Xml-parser
- Code
Wednesday, February 6, 2008
F# union types in C#
{
int x = Convert.ToInt32(nudTest.Value);
if(PsA.resultOfVote(x).IsYes())
{
lblResult.Text = "Yes";
}
if(PsA.resultOfVote(x).IsNo())
{
lblResult.Text = "No";
}
if(PsA.resultOfVote(x).IsMaybe())
{
lblResult.Text = PsA.resultOfVote(x).Maybe1.ToString();
}
Monday, February 4, 2008
F# union type
A new thing in F# for a C# programmer is a union type. At first I thought it was an enumeration (enum).
#light
type YesNo =
| Yes
| No
let resutlOfQuestion x =
if x then
Yes
else
No
//val resutlOfQuestion : bool -> YesNo
resutlOfQuestion true;;
//> val it : YesNo = Yes
resutlOfQuestion false;;
//> val it : YesNo = No
But wait, there is more:
| Yes
| No
| Maybe of int
let resultOfVote x =
if x = 0 then
No
elif x >= 100 then
Yes
else
Maybe x
resultOfVote 0;;
//> val it : YesNoMaybe = No
resultOfVote 50;;
//> val it : YesNoMaybe = Maybe 50
resultOfVote 120;;
//> val it : YesNoMaybe = Yes
So a union type is a garbage can, you could put in everything you like (or don’t like).
F# by example
At the moment I am preparing new version of the OoxAcceleratorRss program.
I build previous version with C# and was quite successful and wanted to improve it. I ran into some problems which are html-related and I got stuck. I had to design and build a better html parser. One that is able to correct all kinds of html errors.
I read that one of the features of F# is the lexer and the parser. So I intend to learn F#.