Posts

Showing posts from 2008

Adventures in F# - A port of the simplest Herbivore

Following the past of my previous post, this time I present a barebones stripped-down version of the Herbivore that ships with the Terrarium SDK. The code is will equip you with a compilable herbivore, which however does nothing but sit in your terrarium. Just like in the previous post, I did not strip out some of the inline comments, which may be helpful to some of us. So here comes the code ... namespace CBHerbivore #light #r "E:\\Development\\my fsharp\\Terrarium\\Terrarium\\OrganismBase.dll" open System;; open System.Drawing;; open System.Collections;; open System.IO;; open OrganismBase;; //Sample Herbivore //The following Assembly attributes must be applied to each //Organism Assembly [<assembly: OrganismClass("CBHerbivore.CBHerbivore")>] //The class that derives from Animal [<assembly: AuthorInformation("Christian Bitter", "x@y.z")>] //It's an herbivore [<CarnivoreAttribute(false)>] //This value must be between ...

Adventures in F# - A Plant for Terrarium 2

Recently, my beloved Terrarium-game has reappeared from the neatherworlds of software. As some of you may know, Terrarium's intend was to showcase the back then rather new and exciting .NET platform version 1.x (not that it is not still exciting). Although from a gamer's perspective, the graphics weren't all to great, from a technology standpoint, it featured great things, such as Webservices, Reflection, etc. At some point Terrarium ceased to exist, which was a shame, since the community was quite active developing plants, herbivores and carnivores for the virtual ecosystem that Terrarium simulates. I for myself, developed some simple creatures too (a simple decision-based/state-based agent), as part of a project I did during university for my AI course. More advanced (in comparison to my creature) could be imported showcasing great AI stuff such as A*, neural networks, planning, etc. However, in an community effort, Terrarium was relaunched lately on codeplex and will hop...

Adventures in F# - F# and ADO.NET

Today's post is rather simplistic in what it does, but it illustrates some key language elements of F#, such as exception handling.  The task to accomplish is quite it easy, nameley, to connect to a SQL Server 2005 database called "TestDB" on my local machine, using ADO.NET to pull out some rows of the "Person" table having the following schema: Person (   PersonID: int ;   FirstName nvarchar (50);   LastName nvarchar (50);   Street nvarchar (50);   City nvarchar (50);   ZIP int ) The code we use in F# is a rather straightforward conversion of some typical C# code, and as such is probably not the best way to do things, but it gets the task done, and may illustate the point. #light We use the lightweight syntax option. open System.Data; open System; open System.Data.SqlClient; As a next step, we have to open the correct namespaces. let ADONetTest =    let conStringBuilder = new SqlConnectionStringBuilder()   conStringBuilder.IntegratedSecurity   conStringB...

Small one - SourceSafe via command line

Yes, I haven't been blogging for quite a bit and this small post is not going to change that but at least it is a little somethin'. I've been digging into F# and have had some great learning experience but this is nothing to really present now. Today I show a little snippet that let's you load some project ("XYZ") from SourceSafe into a specific folder (&ltTarget Directory&gt). This may come in handy in some sort of automatic build invironment. At first you need to define, i.e. set as environment variables: 1. SourceSafe User Account, supplied with sufficient privileges to actually access the project you want to retrieve 2. The account's password 3. The path to the SourceSafe DB's SourceSafe configuration file (srcsafe.ini) Next you ask SourceSafe to change the working directory for the project, you are going to load. You do this via "ss Workfold". The last step is to load/get the actual project into the working directory. SET SSUSER=...

Aventures in F# - Fibs

Today's post is about the famous Fibbonaci sequence: 0, 1, 1, 2, 3, 5, 8, ... I will present a recursive and an iterative F# approach to generating the sequence. The Fibbonaci number n = Fib(n) is generated as follows: Fib(n) = Fib(n-2) + Fib(n-1) where Fib(0) = 0 and Fib(1) = 1 The condition Fib(0) and Fib(1) are the anchor to end the recursion and otherwise it is dug into the recursion. Consequently, ... 1. Recursive Approach let rec FibRec n = if n = 0 then 0 else if n = 1 then 1 else (FibRec (n-2) + FibRec(n-1)) ;; As you can see the function is rather straightforward and follows the definition closely. However, its recursive nature make it rather unsuiting for large values of n - you will notice that for large values of n the recursion slows the process down. 2. Iterative approach let FibIt n = if n = 0 then 0 else if n = 1 then 1 else let fibA = Array.create (n+1) 0 fibA.[0] for i = 2 to n do fibA.[i] As you can see the iterative approach employs Fib(0) and Fib...

Aventures in F# - words is all you need

F# let's you specify the same thing in different styles - depending on your likings (clarity, purity, style, compactness, problem-domain-specific) you my choose the appropriate expression. For example: Assume we have a list of numbers and want to transform them in a certain way, e.g. we want to square them. 1. generate a list of numbers let numbers = [ for i in 1..10 -> i];; Now we have different options to reach our goal, i.e. transforming the elements of the list to be equal to their respective square, i.e. i -> i * i The first way to do this is to generate the set with squared numbers. let sqnumbers = [ for i in 1..10 -> i * i];; While it solves the problem, it was not exactly what I was looking for. So we define a function that takes an integer list as input and generates the appropriate output list by taking each element squaring it and appending it to the output list. Let sqfunc be this function. One way of calling sqfunc is: sqfunc numbers;; But we could also us...

Adventures in F#

Adventure 1: Reversing an integer list let rec ReverseList (l: int list ) =   match l with  ¦ H::T -> (ReverseList T) @ [H]  ¦ [] -> [] The code is quite simple. It does the following: define a recoursive function ( rec keyword) on integer list argument use pattern matching ( match with ) on the argument l to decompose l into a head and a tail. The head is the head (first) element of l and tail is the remainder of l. use this knowledge to call itself recoursively It is checked whether l consists of a head H and a tail T, if yes, then ReverseList calls itself with T (the list consisting of the tail of l, i.e. l without the head element) and concatenates ( @ operator) the result with the list consisting of the headelement ([H]) if the input argument l is the empty list ([]), then ReverseList exits by returning the empty list.