Sunday, December 7, 2008

Mutual Recursion in F#

Mutual recursion is an important concept in functional programming. It is useful if two function need to call each other. In the following example, I am calculating Fibonacci numbers in straight and mutual recursive fashion:
#light
// Straight Version
let rec fibonacci(n) =
if n<1>=0 "))
if n <=2 then 1 else fibonacci(n-1)+fibonacci(n-2)

let n=fibonacci(8)
printfn "%d" n

// mutual recursion version
let rec f(n)= if n=1 then 1 else g(n-1)
and g(n)= if n=1 then 0 else g(n-1)+f(n-1)

let mr_fibonacci(n)=f(n)+g(n)

let mr_d=mr_fibonacci(8)
printfn "%d" mr_d

Sunday, November 30, 2008

F# LegoNxt Robot


I am learning F# since 07/2007 and looked always for an opportunity to put my F# coding skills in to practice. I grabbed my son’s Lego Nxt and wrote a library in F# to communicate with Lego Nxt Brick via Blue Tooth . I have added support for two moving motors (Port B and C) , touch sensor (Port 1) and sonar sensor(Port4). Joe Armstrong (Erlang Programming Language) told in an interview something like this :
In a real world, people also act concurrently from one another and this all works fine and we don’t need to be Siamese twins with conjoined brains(hint :shared memory). I am using Erlang style Message Passing Style using F# MailboxProcessor for collecting data and to control nxt robot.
In source code , I have written an example AutonomousNxtBrick which will drive autonomic and avoid collision.

Enjoy

Thursday, November 13, 2008

F# MailboxProcessor

The Mailboxprocessor class has changed slightly in Version 1.9.6.2
You have to modify the example 13.11 from the Expert F# Book as following to run

#light
// ----------------------------
// Listing 13-11.
/// The internal type of messages for the agent
type internal msg = Increment of int Fetch of AsyncReplyChannel Stop
type CountingAgent() =
let counter = MailboxProcessor.Start(fun inbox ->
// The states of the message-processing state machine...
let rec loop(n) =
async { let! msg = inbox.Receive()
match msg with
Increment m ->
// increment and continue.
return! loop(n+m)
Stop ->
// exit
return ()
Fetch replyChannel ->
// post response to reply channel and continue
do replyChannel.Reply(n)
return! loop(n) }
// The initial state of the message-processing state machine...
loop(0))
member a.Increment(n) = counter.Post(Increment(n))
member a.Stop() = counter.Post(Stop)
member a.Fetch() = counter.PostAndReply(fun replyChannel -> Fetch(replyChannel))
// ----------------------------
let counter = new CountingAgent()
counter.Increment(1)
let v=counter.Fetch()
printfn "%d" v
counter.Increment(2)
let s=counter.Fetch()
printfn "%d" s
counter.Stop()

Thursday, October 30, 2008

Formulation Abstractions with Higher Order Functions

A function is called higher order function if it takes functions as arguments and return a function. Say, you have a family of algorithm and each of them differs from one another in some steps, but there is common basic patter. In object oriented world, you will use typically the Strategy–Pattern to tackle this kind of problem.
Please check
http://www.paul-abraham.com/FSharpCode2SICP.doc

Sunday, October 19, 2008

Continuation Monad

Continuation Monad plays an import role in F# . It powers Workflows. I wrote a sample code in F# and checked monadic Axioms. Please check to get entire series
(
http://www.paul-abraham.com/MonadsInFSharp.doc)

Wednesday, October 8, 2008

Linear Recursion and Linear Iteration (Tail Recursion).

Friends, I am back from Israel and I have attended there a mathematical conference. Now ,I am enjoying reading the wonderful book “Structure and Interpretation of computer Programs”. I have written F# code for Linear Recursion and Linear Iteration (Tail Recursion). Please check http://www.paul-abraham.com/FSharpCode2SICP.doc to get the whole series

Saturday, September 13, 2008

List Monad in F#

Now, it is time look some useful monads than Identity Monad. If you look LINQ query , you can’t miss it recognize the Bind operator(>>=) of a Monad:
public static IEnumerable SelectMany

I am using ResizeArray<_> as List, which is synonym for .Net generic list List<>. I have implemented standard Customer Order scenario to check monads Axioms:
( http://www.paul-abraham.com/MonadsInFSharp.doc)

Wednesday, September 10, 2008

Identity Monad in F#

When I have studied Category Theory at the university , I didn’t know where to use Monads and Kleisli triple.The Whole Gadget was very abstract to me. Since I have encountered with F# Workflows and LINQ , I understand the power and elegance of Monads. In order to learn F#,I have implemented the Identity Monad.
( http://www.paul-abraham.com/MonadsInFSharp.doc)

Wednesday, September 3, 2008

F# Code to Structure and Interpretation of Computer programs

F# Code to Structure and Interpretation of Computer programs.I love and enjoy reading the bookStructure and Interpretation of computer Programs” (http://mitpress.mit.edu/sicp/) but examples of this book are written in Lisp. Right now ,I am learning F# and I think that it is a great opportunity to acquire practice with this great language. Please check http://www.paul-abraham.com/FSharpCode2SICP.doc to get the whole series . I will add F# implementaions day by day.