<?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>coder . cl &#187; haskell</title>
	<atom:link href="http://coder.cl/category/programming/haskell/feed/" rel="self" type="application/rss+xml" />
	<link>http://coder.cl</link>
	<description>system programmer &#38; web developer</description>
	<lastBuildDate>Thu, 29 Jul 2010 21:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>i&#8217;m learning haskell</title>
		<link>http://coder.cl/2009/11/im-learning-haskell/</link>
		<comments>http://coder.cl/2009/11/im-learning-haskell/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 00:55:09 +0000</pubDate>
		<dc:creator>Daniel Molina Wegener</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://coder.cl/?p=517</guid>
		<description><![CDATA[My experience with Functional Programming Languages is small. I&#8217;ve been writing small hacks for emacs lisp, such as hooks for certain modes and actions for a while, and sometimes building small tools in SBCL. Someone on Internet has recommended me an interesting book about Haskell called &#34;Real World Haskell&#34;, that may be read on-line. I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>My experience with <i>Functional Programming Languages</i> is small. I&#8217;ve been writing small hacks for emacs lisp, such as <i>hooks</i> for certain modes and actions for a while, and sometimes building small tools in <i>SBCL</i>. Someone on Internet has recommended me an interesting book about Haskell called <i>&quot;Real World Haskell&quot;</i>, that may be read on-line. I&#8217;ve been reading the book in my free time, and playing with the Haskell syntax, building small functions and playing with data type. So the result of those games is a small program that I want to share with you.</p>
<p><span id="more-517"></span></p>
<p>First, I was playing with some <i>business</i> data types, and I&#8217;ve defined some data types for Haskell:</p>
<pre name="code" class="Haskell">
data Role = Undefined
          | Researcher
          | Engineer
          | Scientist
            deriving (Eq, Show)

data Person = Person {
      pid               :: Int
    , name              :: String
    , born              :: Int
    } deriving (Eq, Show)

data Employee = Employee {
      who               :: Person
    , role              :: [ Role ]
    } deriving (Eq, Show)

data Company = Company {
      cName             :: String
    , employee          :: [ Employee ]
    } deriving (Eq, Show)
</pre>
<p>For me, data type signatures matters. I prefer strongly typed languages, instead of the ambiguity of some others. Then I&#8217;ve defined some <i>variable to string</i> conversion functions, so I can display the structure of the <i>Company</i> data type.</p>
<pre name="code" class="Haskell">
asStringPerson :: (Person) -&gt; String
asStringPerson (p) = ( name p )
                     ++ " [" ++ ( show ( born p ) ) ++ "]"

asStringEmployee :: (Employee) -&gt; String
asStringEmployee (e) = "\n" ++ ( asStringPerson ( who e ) )
                       ++ " as " ++ ( show ( role e ) )

asStringEmployeeList :: ( [ Employee ] ) -&gt; String
asStringEmployeeList (el) = do let s :: String
                                   s = ""
                               foldr (\i a -&gt; ( a ++ ( asStringEmployee i ) ) ) s el

asStringCompany :: (Company) -&gt; String
asStringCompany (c) = ( cName c ) ++ " has employeed "
                      ++ ( asStringEmployeeList ( employee c ) )
</pre>
<p>The magic of <i>foldr</i> that turns the <i>el</i> parameter of type <i>[ Employee ]</i> into a string is like the magic on the semantics of <i>(map #&#8217;(lambda x) ())</i> in Lisp. But looking at the documentation of <i>Hakell</i>, it has support for a wide variety of <i>map-work-alike</i> functions <img src='http://coder.cl/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<pre name="code" class="Haskell">
main :: IO ()
main = do let someone1P :: Person
              someone1P = Person {
                            pid = 1
                          , name = "Juan Perez"
                          , born = 1927
                          }
              someone1E :: Employee
              someone1E = Employee {
                            who = someone1P
                          , role = [ Researcher, Scientist ]
                          }
              someone2P :: Person
              someone2P = Person {
                            pid = 2
                          , name = "Perico Palotez"
                          , born = 1929
                          }
              someone2E :: Employee
              someone2E = Employee {
                            who = someone2P
                          , role = [ Engineer, Scientist ]
                          }
              aiCompany :: Company
              aiCompany = Company {
                            cName = "IA Company"
                          , employee = [ someone1E, someone2E ]
                          }
          putStrLn ( asStringCompany aiCompany )
</pre>
<p>The main program instantiates a group of Employee data types and puts them in the <i>aiCompany</i> as <i>Company</i> data type is. If you take a little bit of attention on the code, I&#8217;m worried of some significant details, such as <i>naming</i> and <i>signing</i> data types properly, since I prefer that <i>nomenclature</i> for readability.</p>
<p>I shall continue learning it. I think that the code is elegant, mainly since I can use <i>monadic</i> features, which reduces the lines of code and make the program more legible too. You can download the source code for this toy program <a href="http://coder.cl/uploads/2009/11/first_haskell_program.hs_.gz">here</a>.</p>



share this article at: 


	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F11%2Fim-learning-haskell%2F&amp;title=i%27m%20learning%20haskell&amp;source=coder+.+cl+system+programmer+%26amp%3B+web+developer&amp;summary=My%20experience%20with%20Functional%20Programming%20Languages%20is%20small.%20I%27ve%20been%20writing%20small%20hacks%20for%20emacs%20lisp%2C%20such%20as%20hooks%20for%20certain%20modes%20and%20actions%20for%20a%20while%2C%20and%20sometimes%20building%20small%20tools%20in%20SBCL.%20Someone%20on%20Internet%20has%20recommended%20me%20an" title="LinkedIn"><img src="http://coder.cl/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fcoder.cl%2F2009%2F11%2Fim-learning-haskell%2F&amp;title=i%27m%20learning%20haskell&amp;notes=My%20experience%20with%20Functional%20Programming%20Languages%20is%20small.%20I%27ve%20been%20writing%20small%20hacks%20for%20emacs%20lisp%2C%20such%20as%20hooks%20for%20certain%20modes%20and%20actions%20for%20a%20while%2C%20and%20sometimes%20building%20small%20tools%20in%20SBCL.%20Someone%20on%20Internet%20has%20recommended%20me%20an" title="del.icio.us"><img src="http://coder.cl/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fcoder.cl%2F2009%2F11%2Fim-learning-haskell%2F&amp;title=i%27m%20learning%20haskell&amp;bodytext=My%20experience%20with%20Functional%20Programming%20Languages%20is%20small.%20I%27ve%20been%20writing%20small%20hacks%20for%20emacs%20lisp%2C%20such%20as%20hooks%20for%20certain%20modes%20and%20actions%20for%20a%20while%2C%20and%20sometimes%20building%20small%20tools%20in%20SBCL.%20Someone%20on%20Internet%20has%20recommended%20me%20an" title="Digg"><img src="http://coder.cl/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fcoder.cl%2F2009%2F11%2Fim-learning-haskell%2F&amp;title=i%27m%20learning%20haskell" title="Reddit"><img src="http://coder.cl/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fcoder.cl%2F2009%2F11%2Fim-learning-haskell%2F&amp;t=i%27m%20learning%20haskell" title="Facebook"><img src="http://coder.cl/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=i%27m%20learning%20haskell%20-%20http%3A%2F%2Fcoder.cl%2F2009%2F11%2Fim-learning-haskell%2F" title="Twitter"><img src="http://coder.cl/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fcoder.cl%2F2009%2F11%2Fim-learning-haskell%2F&amp;title=i%27m%20learning%20haskell&amp;annotation=My%20experience%20with%20Functional%20Programming%20Languages%20is%20small.%20I%27ve%20been%20writing%20small%20hacks%20for%20emacs%20lisp%2C%20such%20as%20hooks%20for%20certain%20modes%20and%20actions%20for%20a%20while%2C%20and%20sometimes%20building%20small%20tools%20in%20SBCL.%20Someone%20on%20Internet%20has%20recommended%20me%20an" title="Google Bookmarks"><img src="http://coder.cl/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>


<br/><br/><br/><hr height="1px" width="50%" />
<div style='text-align: center !important;'><b>Copyright © 2009 Daniel Molina Wegener</b><br/><b>Atribución-No Comercial-Sin Derivadas 2.0 Chile</b><br/><a target='_new' rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/cl/"><img alt="Creative Commons License" style="border-width:0" src="/cc88x31.png" /></a></div>
<br/><hr height="1px" width="100%" />
<p><small>© Daniel Molina Wegener for <a href="http://coder.cl">coder . cl</a>, 2009. | <a href="http://coder.cl/2009/11/im-learning-haskell/">Permalink</a> | <a href="http://coder.cl/2009/11/im-learning-haskell/#comments">No comment</a><br/>Post tags: <br/></small></p>
]]></content:encoded>
			<wfw:commentRss>http://coder.cl/2009/11/im-learning-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
