Archive for December, 2008
Merry Christmas
I wish you all a very merry christmas
Hello Standard ML (of New Jersey)
Back when I was at the university we had a course called “Programming languages”, there we learned a little something about a lot of known and less known programming languages. One of these languages was Standard ML, and I remember that I was quite fascinated with that language, it was so different from everything else we had learned. Since then the functional languages have become a bit more common, with Erlang, F#, Clojure, Scala and Haskell as some examples (more functional languages can be found here: http://en.wikipedia.org/wiki/Category:Functional_languages). I have not used any functional languages since then and I thought I would just briefly refresh my memory by installing Standard ML and try the most basics of the language.
Installation
I am currently using a Mac so installing sml-nj was really simple. I downloaded the mac .dmg package from http://www.smlnj.org/ and ran the installer. the system was installed by default at /usr/local/smlnj-110.68. I added the path to my .profile file, which enables me to start sml without being in the sml bin root folder.
When that was done I could start the sml shell (just type sml in your shell):
-
kristian-lundes-macbook-pro:sml kristianlunde$ sml
-
Standard ML of New Jersey v110.68 [built: Thu Sep 4 16:23:20 2008]
Example 1: Start standard ML
(To exit the sml shell use key combination: CTRL + D).
First touch – Hello World
I always do a Hello World programming in a new language. Even though I have been playing around with Standard ML before I do not remember a hole lot of it, so I start from scratch.
-
- "Hello World";
-
val it = "Hello World" : string
Example 2: Print “Hello World”
The – is the sml prompt, to end a expression the semi-colon is used. So to write Hello World all you have to do is to apply quotes around the text and end it with ; and hit enter. The result gives us the text and the data type.
The first function
To do a Hello world proved to be quite simple, so lets take it a bit further and write a function which prints the hello world text.
-
1. – fun hello ():string = "Hello World";
-
2. val hello = fn : unit -> string
-
3. – hello();
-
4. val it = "Hello World" : string
Example 3: The first function
The function itself is a one liner seen on line 1 in the example above. The function is created using the fun keyword, the function is named hello and have no parameters which result in the (). The function return a result of the data type string, this is defined with the :string element, The content of the function start after the equal sign, and is just a print of the hello world text. To end the function close semi-colon.
When creating a function which is compiled we get the response seen in line 2. This tell us that hello is a function and that it return a string. I believe the unit text mean that the function does not take any parameters (I might be wrong).
To execute the function just type the name of the function, see line 3 in the example above, this result in printing out the “hello world” text as seen in line 4. Line 4 tell us that the result of the function is “Hello World” and the data type is a string.
Functions with parameters
Now we have created our first function, but a function usually need one or more parameters, so let us have a look at that. The next function is a simple echo function which just print the text to screen.
-
1. – fun echo (s:string):string = s;
-
2. val echo = fn : string -> string
-
3. – echo("Hello World");
-
4. val it = "Hello World" : string
Example 4: Function with parameter
Line 1 in example 4 define the function, it takes one parameter s as a string, the return data type is also a string and it just output the string to the shell. When the function is executed in line 3 the parameter is “Hello World” and the result is seen in line 4.
To add more than one parameter just add a comma between the parameters. Remember to define a data type on all the parameters.
Example 5 is a function which takes to parameters and sum the two parameters together before returning the result.
-
1. – fun sum(x:int,y:int):int = x + y;
-
2. val sum = fn : int * int -> int
-
3. – sum(10,20);
-
4. val it = 30 : int
Example 5: function with multiple parameters
Lambda expressions
Lambda expressions is also known as anonymous functions, and create a function without any name. This is used quite frequently in sml and are written as seen in example 6.
-
fn() => print "Hello World";
Example 6: lambda expression.
The example 6 do not do much except print out the Hello world text when it is called. Naturally a lambda expression must be called as part of another function or code snippet.
Load a function from file
To load sml code from a file just use:
-
- use "../sml/myfile.sml";
Where you replace ../sml/myfile.sml to point to your file.
Resources
This is the best online introductions I have found so far are:
www.pllab.riec.tohoku.ac.jp/smlsharp/smlIntroSlides.pdf
http://www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf
A short FAQ:
http://www.smlnj.org//doc/FAQ/index.html
I really have not showed anything of the fancy stuff you could go about and build with Standard ML, but hopefully I am able to spend some more hours playing around with standard ML and write another blog post.
2.5 applications I really miss in OS X
In a previous post I wrote about my new life running on a MacBook Pro and OSX. It has now been over a month since I switch over to this unix hybrid, and I am quite liking it. It is very stable, I almost never turn off my mac, but I put it to sleep, this is working fine and my last reboot is over two weeks ago. I have also gotten used to some of the new weird keys on the keyboard and the shortcuts, but I am not yet as efficent on a mac as I am/were on ubuntu/windows. During the last few weeks I have discovered that Apple and other in most cases provide me with the applications I need, but not always, and here is the list of applications I really miss:
1. TortoiseSVN
Windows application which integrates itself with Windows Explorer and provide a SVN client. I would say that this is the best graphical SVN client I have ever used. SVNX which I currently use on the mac is not a very good replacement.
2. Kate / Notepad++
Kate is a KDE text editor for unix based systems. Notpad++ is Kates equivalent on Windows. Both editors provide a simple and intuitive user interface, and a lot of syntax highlight files for all the obscure programming languages you can think of.
I know Mac have the TextMate application, but that is third party software and you have to pay €48 or something for a license, and that is probably what I probably will do. The TextMate application is really good and provide most if not all the functionality that Kate and Notepad++ provide.
In my desperation for a good text editor I almost went off and tried to install KDE on Mac, but that was said to be experimental and could break my entire system. So that is a no go for now. The article however was really interesting:
URL: http://www.simplehelp.net/2007/07/22/how-to-install-kde-4-in-os-x/.
I still miss my ubuntu system and will probably go off and install parallels or vmware and ubuntu, just to have it accessible
Adding and editing pages in your drupal module
Whenever you are adding or editing pages to your module, remember to refresh the admin -> site building -> modules or your changes will not take effect.