Posts

Showing posts with the label MSR

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...

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...

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.