How do  you remove duplicate elements from a list in F#?


The type of the foldBack function in List is defined as follows:

val foldBack : folder:('T -> 'State -> 'State) -> list:'T list -> state:'State -> 'State

The foldBack function takes in two parameters as input of the following type:
a) ('T->'State->'State)
b) 'T list

and gives back as output
'State

It can also be said the foldBack function takes in as input a function of the type 
('T->'State->'State) and returns back a function ('T list->'State->'State) that takes in a 'T list 
and returns back a function ('State->'State) that takes in a 'State and returns back a 'State. 

let f element accum=
    match accum with
    |[]->element::[]
    |head::tail when head<>element ->element::head::tail
    |_::tail ->element::tail

let dupEliminated=[]|>List.foldBack f [1;1;2;2;3;4;4;5;6;7;7;7;8;9]


QuickSort Algorithm in F#

let rec quickSort unsortedList=
    let rec createSubList element tail compare=
        match tail with
        |[]->[]
        |headR::tail when (compare headR  element) -> headR::(createSubList element tail compare) 
        |_::tail -> (createSubList element tail compare)
    
            
    match unsortedList with
    | [] -> []
    | head::tail-> quickSort (createSubList head tail (<=)) @ [head] @ quickSort (createSubList head tail (>=)) 


 Generate Fibonacci Series in F# 

I was intrigued by the Seq.unfold function. The Seq.unfold function is defined as follows:

unfold

val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

The inputs to the unfold method are: 
1) ('State->('T*'State) option)
2) 'State <-- this is the initial state or seed to use. A tuple i.e.  (1,1) is used as the seed to generate the Fib series. 


let h=Seq.unfold (fun x->Some(fst x,(snd x, fst x+snd x))) (1,1)
printfn "%d" (h|>Seq.nth 6) <--- outputs 13

Note: x is of Tuple type i.e. int*int
fst x is of type 'T (where 'T is int ) which is collected in the seq<'T>
The next state that is fed again in the generator is (snd x, fst x+snd x)

 Send Asynchronous Post and Reply Messages over to MailboxProcessor 

type msg = Increment of int | Fetch of int*AsyncReplyChannel | Stop
type Agent<'T>=MailboxProcessor<'T>

let counter =
    new Agent(fun inbox ->
        let rec loop n =
            async {
                   let! msg1 = inbox.Receive()
                   match msg1 with
                   | Increment (x)->printfn "the value is %d" x 
                                    return! loop -5
                   | Stop-> return ()  
                   | Fetch (clientID,channel)->channel.Reply clientID
                                               return! loop -1
                                    
                   }
        loop 0)


counter.Start()
//counter.PostAndReply(fun channel->Fetch channel) |> printfn "the client is saying %d"
seq { for i in 1..100->counter.PostAndAsyncReply(fun channel->Fetch (i,channel)) }
|> Async.Parallel
|> Async.RunSynchronously
|> Array.toList
|> List.iter (printfn "client has value %d from server")

The type for the function PostAndAsyncReply is:

PostAndAsyncReply
member MailboxProcessor.PostAndAsyncReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> Async<'Reply>

a) The buildMessage is a function that takes in a AsyncReplyChannel<'Reply> and gives as an output a 'Msg. 
b) The output from the PostAndAsyncReply is a Async<'Reply>

The AsyncReplyChannel is used by the MailBoxProcessor for replying back to the client. The buildMessage function is executed at somepoint at the MailBoxProcessor end. When this function is executed, an AsyncReplyChannel object is passed as an input by the MailBoxProcessor which is then sent as an output via the 'Msg i.e. (Fetch of int*AsyncReplyChannel). When this 'Msg is collected via the Recieve function i.e. ( Fetch (clientID,channel)), the AsyncReplyChannel is used to echo back the clientID to the client i.e. channel.Reply clientID