OCaml Stack and Queue

This is a quick post to show how easy (and beautiful) it is to implement a simple (not the best though) and elegant FIFOs and LIFOs in OCaml.

For the Stack (LIFO – Last In First Out), the source code is available in the core language in lib/stack.ml, and it uses Lists to save elements inside the Stack, the implementation is so easy :

  1. module Stack = struct
  2.     type 'a t = { mutable c : 'a list }
  3.     exception Empty
  4.  
  5.     let create () = { c = [] }
  6.     let clear s = s.c <- []
  7.     let copy s = { c = s.c }
  8.     let push x s = s.c <- x :: s.c
  9.  
  10.     let pop s =
  11.       match s.c with
  12.         hd::tl -> s.c <- tl; hd
  13.       | []     -> raise Empty
  14.  
  15.     let top s =
  16.       match s.c with
  17.         hd::_ -> hd
  18.       | []     -> raise Empty
  19.  
  20.     let is_empty s = (s.c = [])
  21.     let length s = List.length s.c
  22.     let iter f s = List.iter f s.c
  23. end;;

For the Queue (FIFO), here is an elegant implementation using references on Lists:

  1. module Queue = struct
  2.   type 'a t = ('a list * 'a list) ref
  3.   exception Empty
  4.   let create () = ref ([], [])
  5.   
  6.   let add x queue =
  7.     let front, back = !queue in
  8.         queue := (x::front, back)
  9.   
  10.   let rec take queue =
  11.     match !queue with
  12.         (front, x :: back) ->
  13.                     queue := (front, back);
  14.                     x
  15.         | ([], []) ->    
  16.                     raise Empty
  17.         | (front, []) ->
  18.                     queue := ([], List.rev front);
  19.                     take queue
  20. end;;

The Queue is composed of 2 Lists holding the front elements (used for adding new elements to the Queue) and a back list holding elements when we call take (or dequeue).

The add function is simple, it just take an elements and put it at the beginning of the front list queue := (x::front, back)

The take function is the one that does all the work, it checks if the Queue is not empty (an empty queue is a queue that both front and back lists are empty), then it checks if the back list has at least one element, if this is the case, the element is deleted from the queue and then returned.

Otherwise the back list is empty, so we take elements from the front list (the one used to store elements when we call add), reverse them, and store them in the back list, then the function calls itself recursively to extract the new available elements.

And in case you didn’t notice, both the Stack and Queue are generics by nature, they accept any type for their elements.

Posted in , |

Learning OCaml, Questions From Stackoverflow.com

The most effective way to learn something new is by asking questions, and it’s the same for programming languages.

Below is a collection of question about the OCaml functional language from stackoverflow.com and it’s supposed to help you get your mind to work “OCaml”.

Learning OCaml (Fast)

Suppose I had to learn OCaml in a hurry. What would you suggest I read and/or do?

Know of an OCAML IDE?

Know of an OCAML/CAML IDE? Especially one that runs on Linux?

F# and OCaml

I hear that F# is derived from OCaml. How true is this statement? That is to say, are the resources available for learning OCaml useful to someone who wants to learn F#?…

What is the OCaml idiom equivalent to Python’s range function?

I want to create a list of integers from 1 to n. I can do this in Python using range(1, n+1), and in Haskell using: take n (iterate (1+) 1).

Why isn’t OCaml more popular?

On the other hand, ML languages are functional, garbage collected languages, and OCaml even has an object model, yet they have a reputation for being as fast as C. ML languages have the abstraction anyone could ask for to write high-level, concise code, yet it retains the speed necessary for writing high-performance applications…

In Functional Programming, what is a functor?

What are the differences between SML and Ocaml?

Why are functions in Ocaml/F# not recursive by default?

{X with value} in ocaml

Ocaml: Error - this expression has type x but is used with type x

Does != have meaning in OCaml?

OCaml: How does List.fold_left work?

Linked List Ocaml

Good projects to learn OCaml and F# 

OCaml resources?

For more resources check out the OCaml questions on stackoverflow, and don’t hesitate to leave a question there if you have any, it is a very active community.


Take advantage of the new Windows 7 Taskbar #2 [Showcase : Bloginto7]

This is the part 2 of how to build a simple feed reader following the style of the Gmail Notifier Plus which uses basically the Windows 7 taskbar features.42001378_thumb
Part 1 was about the XAML part of the application, this part will show how to read the feeds asynchronously, how to change the ProgressBar and have an auto update features to read feeds.
First we need a prototype class to hold the feeds :

  1. namespace BlogInto_win7
  2. {
  3.     public class Feed
  4.     {
  5.         public int Id { get; set; }
  6.         public string Title { get; set; }
  7.         public string Link { get; set; }
  8.         public string PubDate { get; set; }
  9.         public string Author { get; set; }
  10.         public string Description { get; set; }
  11.         public string Votes { get; set; }
  12.     }
  13. }
Then we will need the following public variables to hold the feeds data, the current feeds shown and a timer for auto update.

Posted in , , , , , , , , , |

BlogInto, The First Google Chrome Extension For Bloginy

BlogInto has been around for a while as a standard Windows application or a Windows 7 specific version, today it is going public as a Google Chrome extension too.

It is so basic for now and it is the result of less than 2 hours playing around with the extensions’ APIs and specifications, if you want to play around with them too, make sure you take a look at the post about developing Google Chrome extensions I posted yesterday.

You can install it directly from here : https://chrome.google.com/extensions/detail/jppmcmbnmodlmgbfdddmeopgagancoak and of course the source code is public, you can view it using the Chrome developer tools –this is how to do it : [link]-.

Sans titre

Well,I hope you like it, and I would be happy to hear your feedback about any issues and problems with the extension, also any suggestions about features to include are welcome too.

2


Develop Google Chrome Extensions, SVN ‘em All Now

The buzz these days is all about Google Chrome extensions, you can check the directory of the available extensions here https://chrome.google.com/extensions/, you can even publish you own extensions on the same website.
If you want to develop for Google Chrome extensions, you will be so pleased if you already know JavaScript, the extensions are some sort of JavaScript pages (frames) that interact with a variety of APIs.
The Google Extensions website for developers on Google Code -http://code.google.com/chrome/extensions/getstarted.html- is really amazing (as all the other products explained by Google) and it’s too detailed and simple to follow and start getting your hands on the Chrome extensions.
After a while you will need to try out the examples they provide, and the best way is to checkout their ready-to-run samples using you favorite SVN tool, here I use TortoiseSVN for Windows and it’s working pretty cool.
In your favorite directory, right click in a free space and choose SVN Checkout.


Run you own web server using PHP / ASP.NET on IIS7 [Part #2 – Install Aquia-Drupal and Wordpress on IIS]

After part #1 about how to make your webserver available publically on the internet, I will discuss how to install php on IIS7 and how to run some known web applications on the same server.
There are 2 ways to install php on IIS, the first and the simplest one is to use the Microsoft’s Web Platform installer, you get the latest stable version of php and fastcgi configured, if you don’t know already, the Web Platform offers dozen of tools for developers, from Visual Studio Express, to database servers, IIS, php, webapps and more…
You can download the web Platform Installer from here 

The second method to install php  detailed here is to download php separately, then add a new script mapping and associate the extension you want to the php handler.

At the end, create a test page on the C:\inetpub\wwwroot directory –test.php for example- and copy this function inside, you have to add the current user to the owners of the directory in order to change or create files in it.

<?php  phpinfo();  ?>


Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.