<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kristian Lunde &#187; SML</title>
	<atom:link href="http://www.klunde.net/tag/sml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.klunde.net</link>
	<description>www.klunde.net</description>
	<lastBuildDate>Wed, 01 Feb 2012 23:57:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Hello Standard ML (of New Jersey)</title>
		<link>http://www.klunde.net/2008/12/17/hello-standard-ml-of-new-jersey/</link>
		<comments>http://www.klunde.net/2008/12/17/hello-standard-ml-of-new-jersey/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 22:52:10 +0000</pubDate>
		<dc:creator>Kristian Lunde</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SML]]></category>
		<category><![CDATA[sml-nj]]></category>

		<guid isPermaLink="false">http://www.klunde.net/?p=203</guid>
		<description><![CDATA[Back when I was at the university we had a course called &#8220;Programming languages&#8221;, 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 [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F12%2F17%2Fhello-standard-ml-of-new-jersey%2F" onclick="urchinTracker('/outgoing/api.tweetmeme.com/share?url=http_3A_2F_2Fwww.klunde.net_2F2008_2F12_2F17_2Fhello-standard-ml-of-new-jersey_2F&amp;referer=');"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.klunde.net%2F2008%2F12%2F17%2Fhello-standard-ml-of-new-jersey%2F&amp;source=kristianlunde&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Back when I was at the university we had a course called &#8220;Programming languages&#8221;, 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: <a href="http://en.wikipedia.org/wiki/Category:Functional_languages" onclick="urchinTracker('/outgoing/en.wikipedia.org/wiki/Category_Functional_languages?referer=');">http://en.wikipedia.org/wiki/Category:Functional_languages</a>). 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.</p>
<h3>Installation</h3>
<p>I am currently using a Mac so installing sml-nj was really simple. I downloaded the mac .dmg package from <a href="http://www.smlnj.org/" onclick="urchinTracker('/outgoing/www.smlnj.org/?referer=');">http://www.smlnj.org/</a> and ran the installer. the system was installed by default at <em>/usr/local/smlnj-110.68</em>. I added the path to my <em>.profile</em> file, which enables me to start sml without being in the sml bin root folder.</p>
<p>When that was done I could start the sml shell (just type <em>sml</em> in your shell):</p>
<div class="geshi no sml">
<ol>
<li class="li1">
<div class="de1">kristian-lundes-macbook-pro:sml kristianlunde$ sml
</div>
</li>
<li class="li1">
<div class="de1">Standard ML of New Jersey v110.68 [built: Thu Sep &nbsp;4 16:23:20 2008]</div>
</li>
</ol>
</div>
<p><em>Example 1: Start standard ML</em></p>
<p>(To exit the sml shell use key combination: CTRL + D).</p>
<h3>First touch &#8211; Hello World</h3>
<p>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.</p>
<div class="geshi no sml">
<ol>
<li class="li1">
<div class="de1">- &quot;Hello World&quot;;
</div>
</li>
<li class="li1">
<div class="de1">val it = &quot;Hello World&quot; : string</div>
</li>
</ol>
</div>
<p><em>Example 2: Print &#8220;Hello World&#8221;</em></p>
<p>The &#8211; 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.</p>
<h3>The first function</h3>
<p>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.</p>
<div class="geshi no sml">
<ol>
<li class="li1">
<div class="de1">1. &#8211; fun hello ():string = &quot;Hello World&quot;;
</div>
</li>
<li class="li1">
<div class="de1">2. val hello = fn : unit -&gt; string
</div>
</li>
<li class="li1">
<div class="de1">3. &#8211; hello();
</div>
</li>
<li class="li1">
<div class="de1">4. val it = &quot;Hello World&quot; : string</div>
</li>
</ol>
</div>
<p><em>Example 3: The first function</em></p>
<p>The function itself is a one liner  seen on line 1 in the example above. The function is created using the <em>fun</em> 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 <em>:string</em> 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. </p>
<p>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).</p>
<p>To execute the function just type the name of the function, see line 3 in the example above, this result in printing out the &#8220;hello world&#8221; text as seen in line 4. Line 4 tell us that the result of the function is &#8220;Hello World&#8221; and the data type is a string.</p>
<h3>Functions with parameters</h3>
<p>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. </p>
<div class="geshi no sml">
<ol>
<li class="li1">
<div class="de1">1. &#8211; fun echo (s:string):string = s;
</div>
</li>
<li class="li1">
<div class="de1">2. val echo = fn : string -&gt; string
</div>
</li>
<li class="li1">
<div class="de1">3. &#8211; echo(&quot;Hello World&quot;);
</div>
</li>
<li class="li1">
<div class="de1">4. val it = &quot;Hello World&quot; : string</div>
</li>
</ol>
</div>
<p><em>Example 4: Function with parameter</em></p>
<p>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 &#8220;Hello World&#8221; and the result is seen in line 4.</p>
<p>To add more than one parameter just add a comma between the parameters. Remember to define a data type on all the parameters.</p>
<p>Example 5 is a function which takes to parameters and sum the two parameters together before returning the result.</p>
<div class="geshi no sml">
<ol>
<li class="li1">
<div class="de1">1. &#8211; fun sum(x:int,y:int):int = x + y;
</div>
</li>
<li class="li1">
<div class="de1">2. val sum = fn : int * int -&gt; int
</div>
</li>
<li class="li1">
<div class="de1">3. &#8211; sum(10,20);
</div>
</li>
<li class="li1">
<div class="de1">4. val it = 30 : int</div>
</li>
</ol>
</div>
<p><em>Example 5: function with multiple parameters</em></p>
<h3>Lambda expressions</h3>
<p>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.</p>
<div class="geshi no sml">
<ol>
<li class="li1">
<div class="de1">fn() =&gt; print &quot;Hello World&quot;;</div>
</li>
</ol>
</div>
<p><em>Example 6: lambda expression.</em></p>
<p>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.</p>
<h3>Load a function from file</h3>
<p>To load sml code from a file just use:</p>
<div class="geshi no sml">
<ol>
<li class="li1">
<div class="de1">- use &quot;../sml/myfile.sml&quot;;</div>
</li>
</ol>
</div>
<p>Where you replace <em>../sml/myfile.sml</em> to point to your file.</p>
<h3>Resources</h3>
<p>This is the best online introductions I have found so far are:<br />
www.pllab.riec.tohoku.ac.jp/smlsharp/smlIntroSlides.pdf<br />
<a href="http://www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf" onclick="urchinTracker('/outgoing/www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf?referer=');">http://www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf</a></p>
<p>A short FAQ:<br />
<a href="http://www.smlnj.org//doc/FAQ/index.html" onclick="urchinTracker('/outgoing/www.smlnj.org//doc/FAQ/index.html?referer=');">http://www.smlnj.org//doc/FAQ/index.html</a></p>
<p>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.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klunde.net/2008/12/17/hello-standard-ml-of-new-jersey/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

