<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6693964363562485774</id><updated>2011-07-28T08:50:53.014-07:00</updated><title type='text'>Learn Haskell</title><subtitle type='html'>Learn Haskell in just 5 minutes per day</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://learnhaskell.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://learnhaskell.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Jeremy Shaw</name><uri>http://www.blogger.com/profile/18373967098081701148</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://www.alchymiastudio.com/refs/DSC_5723-small.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6693964363562485774.post-7194821154905174119</id><published>2007-09-18T13:00:00.000-07:00</published><updated>2007-11-13T21:35:38.818-08:00</updated><title type='text'>Lesson 4: Types</title><content type='html'>&lt;h4&gt;Overview&lt;/h4&gt;&lt;br /&gt;In this lesson, we will introduce the type system. In Haskell, every expression can be classified by its &lt;i&gt;type&lt;/i&gt;. We can use GHCi to look at the &lt;i&gt;type signature&lt;/i&gt; of expressions by using the &lt;kbd&gt;:t&lt;/kbd&gt; command:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; :t 'r'&lt;br /&gt;'r' :: Char&lt;br /&gt;Prelude&gt; :t True&lt;br /&gt;True :: Bool&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We see that the &lt;i&gt;character&lt;/i&gt; &lt;code&gt;'r'&lt;/code&gt; has the type &lt;code&gt;Char&lt;/code&gt;, and &lt;i&gt;boolean&lt;/i&gt; value &lt;code&gt;True&lt;/code&gt; has the type &lt;code&gt;Bool&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Functions&lt;/h4&gt;&lt;br /&gt;Types are more exciting once we start looking at functions, so let's look at the type of the &lt;code&gt;not&lt;/code&gt; function:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; :t not&lt;br /&gt;not :: Bool -&gt; Bool&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We can read that line as follows:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;not&lt;/code&gt; is the expression whose type we want to know. In this case the expression is the variable &lt;code&gt;not&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;::&lt;/code&gt; indicates that what follows is a type signature.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;Bool -&amp;gt; Bool&lt;/code&gt; tells us that &lt;code&gt;not&lt;/code&gt; is bound to a &lt;i&gt;pure function&lt;/i&gt; that takes a value of type &lt;code&gt;Bool&lt;/code&gt; and returns a value of type &lt;code&gt;Bool&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;We can easily try out the &lt;code&gt;not&lt;/code&gt; function in GHCi:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; not True&lt;br /&gt;False&lt;br /&gt;Prelude&gt; not False&lt;br /&gt;True&lt;br /&gt;Prelude&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We can also ask GHCi for the type of the expression &lt;code&gt;not True&lt;/code&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; :t not True&lt;br /&gt;not True :: Bool&lt;br /&gt;Prelude&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Note that GHCi does &lt;b&gt;not&lt;/b&gt; run &lt;code&gt;not True&lt;/code&gt; and look at the result. Instead it looks at just the types of &lt;code&gt;not&lt;/code&gt; and &lt;code&gt;True&lt;/code&gt; and calculates what the resulting type &lt;i&gt;must&lt;/i&gt; be.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Type Errors&lt;/h4&gt;&lt;br /&gt;Let's see what happens if we try to call &lt;code&gt;not&lt;/code&gt; on a character. I created a file name &lt;kbd&gt;Test.hs&lt;/kbd&gt; which contains just the following line:&lt;br /&gt;&lt;pre&gt;&lt;span class='definition'&gt;test&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='varid'&gt;not&lt;/span&gt; &lt;span class='chr'&gt;'r'&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;When I try to load it into GHCi, I get the following error:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; :load "/tmp/Test.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /tmp/Test.hs, interpreted )&lt;br /&gt;&lt;br /&gt;/tmp/Test.hs:1:12:&lt;br /&gt;    Couldn't match expected type `Bool' against inferred type `Char'&lt;br /&gt;    In the first argument of `not', namely 'r'&lt;br /&gt;    In the expression: (not 'r')&lt;br /&gt;    In the definition of `test': test = (not 'r')&lt;br /&gt;Failed, modules loaded: none.&lt;br /&gt;Prelude&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This error indicates that the compiler found a problem when it was checking the types. The error message gives us lots of information about exactly where it ran into trouble. In most cases when I get a type error, looking at just the following information is enough to quickly see and fix the problem:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; :load "/tmp/Test.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /tmp/Test.hs, interpreted )&lt;br /&gt;&lt;br /&gt;&lt;b&gt;/tmp/Test.hs&lt;/b&gt;:&lt;b&gt;1&lt;/b&gt;:12:&lt;br /&gt;    Couldn't match expected type &lt;b&gt;`Bool'&lt;/b&gt; against inferred type &lt;b&gt;`Char'&lt;/b&gt;&lt;br /&gt;    In the first argument of `not', namely 'r'&lt;br /&gt;    In the expression: &lt;b&gt;(not 'r')&lt;/b&gt;&lt;br /&gt;    In the definition of `test': test = (not 'r')&lt;br /&gt;Failed, modules loaded: none.&lt;br /&gt;Prelude&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;But sometimes the error is not so obvious and the extra information is useful. Let's look more closely:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;/tmp/Test.hs:1:12:&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This tells us that it ran into a problem in /tmp/Test.hs at line 1, column 12, i.e., when it sees &lt;code&gt;'r'&lt;/code&gt;.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    Couldn't match expected type `Bool' against inferred type `Char'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;GHCi was expecting to find a &lt;code&gt;Bool&lt;/code&gt; value, (e.g., &lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;), but instead it found a &lt;code&gt;Char&lt;/code&gt; value.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    In the first argument of `not', namely 'r'&lt;br /&gt;    In the expression: (not 'r')&lt;br /&gt;    In the definition of `test': test = (not 'r')&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The next three lines give us a contextual description of where the error occurred. Although the first line already told us the exact file, line number, and column, the context information can sometimes help you understand how the compiler sees the code better. For example, you will can see that it implicitly added parentheses in this expression &lt;code&gt;(not 'r')&lt;/code&gt;, and that it thinks (correctly) that &lt;code&gt;'r'&lt;/code&gt; is the first argument to the function &lt;code&gt;not&lt;/code&gt;.&lt;br /&gt;&lt;h4&gt;Creating Your Own Types&lt;/h4&gt;&lt;br /&gt;The &lt;code&gt;Bool&lt;/code&gt; type can be defined like this:&lt;br /&gt;&lt;pre&gt;&lt;span class='keyword'&gt;data&lt;/span&gt; &lt;span class='conid'&gt;Bool&lt;/span&gt; &lt;br /&gt;    &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;False&lt;/span&gt; &lt;br /&gt;    &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;True&lt;/span&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The keyword &lt;code&gt;data&lt;/code&gt; indicates that we are declaring a new &lt;i&gt;algebraic data type&lt;/i&gt;. &lt;code&gt;Bool&lt;/code&gt; is the name of the type, also known as a &lt;i&gt;type constructor&lt;/i&gt;. After the &lt;code&gt;=&lt;/code&gt; we have two &lt;i&gt;data constructors&lt;/i&gt;: &lt;code&gt;False&lt;/code&gt; and &lt;code&gt;True&lt;/code&gt;. The &lt;code&gt;|&lt;/code&gt; is a separator that is required between constructors.&lt;br /&gt;&lt;br /&gt;That is all that is required to define the type. We can use it like this:&lt;br /&gt;&lt;pre&gt;&lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;Main&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class='keyword'&gt;import&lt;/span&gt; &lt;span class='conid'&gt;Prelude&lt;/span&gt; &lt;span class='varid'&gt;hiding&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Bool&lt;/span&gt;&lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='keyglyph'&gt;..&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt; &lt;span class='comment'&gt;-- this hides the predefined version of Bool&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class='keyword'&gt;data&lt;/span&gt; &lt;span class='conid'&gt;Bool&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;False&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;True&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class='definition'&gt;main&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='conid'&gt;True&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;&lt;br /&gt;      &lt;span class='conid'&gt;True&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"True"&lt;/span&gt;&lt;br /&gt;      &lt;span class='conid'&gt;False&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"False"&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We see the &lt;code&gt;case&lt;/code&gt; statement that we introduced last lesson. This time instead of pattern matching on a &lt;code&gt;String&lt;/code&gt; we are matching on a &lt;code&gt;Bool&lt;/code&gt;. &lt;br /&gt;&lt;h4&gt;Cool Stuff We Learned Today&lt;/h4&gt;&lt;br /&gt;Today we saw some really cool stuff! &lt;br /&gt;&lt;h5&gt;Type Inference&lt;/h5&gt;&lt;br /&gt;The first thing we saw is that the compiler is able to infer the types of expressions automatically. This inference is not limited to just predefined values like &lt;code&gt;not&lt;/code&gt; or &lt;code&gt;True&lt;/code&gt;. The compiler was able to infer the types of new expressions that we created, like &lt;code&gt;not True&lt;/code&gt;.&lt;br /&gt;&lt;h5&gt;Static Type Checking&lt;/h5&gt;&lt;br /&gt;In addition to inferring the types of expressions, the compiler also checked that we used expressions in a sensible way at compile time. For example, it noticed that we tried to pass &lt;code&gt;'r'&lt;/code&gt; to the &lt;code&gt;not&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;Static type checking eliminates a whole category of common bugs by preventing you from calling a function with a nonsense argument. It is also useful if you change the arguments that a function takes. The compiler will let you know all the places that need to be updated to reflect the change. This is especially useful if you change a library function that is used by lots of applications. When you later try to rebuild those applications, you won't have to remember if they use any functions that have changed -- the compiler will tell you.&lt;br /&gt;&lt;br /&gt;Due to type inference, we get these benefits for &lt;i&gt;free&lt;/i&gt; -- we don't have to run the code, we don't have to write unit tests to test the code, and we don't have to explicitly declare our types, we just have to compile it, or load it into the interpreter.&lt;br /&gt;&lt;h5&gt;New Types&lt;/h5&gt;&lt;br /&gt;We also learned how easy it is to create new types in Haskell. &lt;br /&gt;&lt;br /&gt;&lt;h5&gt;We Know A Lot About &lt;code&gt;not&lt;/code&gt;&lt;/h5&gt;&lt;br /&gt;We saw earlier that &lt;code&gt;not&lt;/code&gt; is a &lt;i&gt;pure function&lt;/i&gt; with the type signature &lt;code&gt;Bool -&gt; Bool&lt;/code&gt;. It turns out that we know an awful lot about the function just from the type signature.&lt;br /&gt;&lt;br /&gt;We know this function is pure because all functions in Haskell are pure by default. A pure function always produces the same output when given the same input. This is because pure function has no way to remember information from one call to another and it has no way to access external IO resources, such as the disk, keyboard, a random number generator, etc. Therefore, there is no way to return a different result, when presented with the same input. You are already familiar with pure functions from basic arithmetic; functions like &lt;kbd&gt;+&lt;/kbd&gt; and &lt;kbd&gt;-&lt;/kbd&gt; are pure functions. &lt;kbd&gt;1 + 1&lt;/kbd&gt; always equals &lt;kbd&gt;2&lt;/kbd&gt; no matter what calculations you have done in the past.&lt;br /&gt;&lt;br /&gt;We also know that this function takes a &lt;code&gt;Bool&lt;/code&gt; and returns a &lt;code&gt;Bool&lt;/code&gt;. &lt;br /&gt;&lt;br /&gt;Combining purity and the type signature &lt;code&gt;Bool -&amp;gt; Bool&lt;/code&gt;, we can see that &lt;code&gt;not&lt;/code&gt; could only do one of five possibly things:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Always return True no matter what the input is&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Always return False no matter what the input is&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Always return the input value. i.e., for &lt;code&gt;True&lt;/code&gt; return &lt;code&gt;True&lt;/code&gt; and for &lt;code&gt;False&lt;/code&gt; return &lt;code&gt;False&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;For &lt;code&gt;True&lt;/code&gt; return &lt;code&gt;False&lt;/code&gt; and for &lt;code&gt;False&lt;/code&gt; return &lt;code&gt;True&lt;/code&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Never return at all&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;br /&gt;Since we tried out &lt;code&gt;not&lt;/code&gt; interactively, we know it is option 4:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; not True&lt;br /&gt;False&lt;br /&gt;Prelude&gt; not False&lt;br /&gt;True&lt;br /&gt;Prelude&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Later, we will see how purity makes it easy to do automated unit testing.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Additional Notes&lt;/h4&gt;&lt;br /&gt;There a few simple rules you need to know when declaring a new data type.&lt;br /&gt;&lt;h5&gt;&lt;code&gt;data&lt;/code&gt; Declarations Must Be at the Top Level&lt;/h5&gt;&lt;br /&gt;You can not declare new data types inside functions or inside anything else. They must always be declared at what is called the &lt;i&gt;top-level&lt;/i&gt;. &lt;br /&gt;&lt;h5&gt;Case matters&lt;/h5&gt;&lt;br /&gt;Type and data constructors must always start with an uppercase letter, followed by zero or more upper or lower case letters or numbers.&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Exercise&lt;/h4&gt;&lt;br /&gt;Your simple exercise for today is to create your own data type, and use it in a case statement, similar to the Bool example given above. &lt;br /&gt;&lt;br /&gt;&lt;h4&gt;Preview&lt;/h4&gt;&lt;br /&gt;If you experimented with the &lt;code&gt;:t&lt;/code&gt; command, you may have found that some values return some unusual looking results, for example, we see that &lt;code&gt;"hello"&lt;/code&gt; is a list of characters:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; :t "hello"&lt;br /&gt;"hello" :: [Char]&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;We will learn more about lists very soon. The number &lt;code&gt;1&lt;/code&gt; has a really funny looking type:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; :t 1&lt;br /&gt;1 :: (Num t) =&gt; t&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;That is because we want to be able to use &lt;code&gt;1&lt;/code&gt; to represent values  of several different types. For example, an &lt;code&gt;Integer&lt;/code&gt; or a &lt;code&gt;Float&lt;/code&gt;. We will learn more about this type signature when we learn about &lt;i&gt;polymorphism&lt;/i&gt; and &lt;i&gt;type classes&lt;/i&gt;. &lt;br /&gt;&lt;h4&gt;Closing&lt;/h4&gt;&lt;br /&gt;Today's lesson had lots of technical terms in it. Don't worry too much about about the exact meaning of the words, and don't worry about trying to remember them all at once. These same terms will come up again and again in future lessons and I will continue to try to make their meanings obvious from the context. Once you have more experience with Haskell, it will be easier to give concrete definitions of the terms.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6693964363562485774-7194821154905174119?l=learnhaskell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnhaskell.blogspot.com/feeds/7194821154905174119/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6693964363562485774&amp;postID=7194821154905174119' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default/7194821154905174119'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default/7194821154905174119'/><link rel='alternate' type='text/html' href='http://learnhaskell.blogspot.com/2007/09/lesson-4-types.html' title='Lesson 4: Types'/><author><name>Jeremy Shaw</name><uri>http://www.blogger.com/profile/18373967098081701148</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://www.alchymiastudio.com/refs/DSC_5723-small.jpg'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6693964363562485774.post-4080426912305269200</id><published>2007-09-11T10:50:00.000-07:00</published><updated>2007-09-18T10:14:42.264-07:00</updated><title type='text'>Lesson 3: case</title><content type='html'>&lt;h4&gt;Overview&lt;/h4&gt;&lt;br /&gt;Today we will start learning about the &lt;code&gt;case&lt;/code&gt; statement. Here is some code to get us started:&lt;br /&gt;&lt;pre&gt;&lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;Main&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class='definition'&gt;main&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class='keyword'&gt;do&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"Do you like Haskell? [yes/no]"&lt;/span&gt;&lt;br /&gt;       &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;getLine&lt;/span&gt;&lt;br /&gt;       &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"yes"&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"yay!"&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"no"&lt;/span&gt;  &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"I am sorry to hear that :("&lt;/span&gt;&lt;br /&gt;         &lt;span class='keyword'&gt;_&lt;/span&gt;     &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"say what???"&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4&gt;A Closer Look at &lt;code&gt;case&lt;/code&gt;&lt;/h4&gt;&lt;br /&gt;The first line of the &lt;code&gt;case&lt;/code&gt; statement looks like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;       &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You can put any valid Haskell &lt;i&gt;expression&lt;/i&gt; between the keywords &lt;code&gt;case&lt;/code&gt; and &lt;code&gt;of&lt;/code&gt;. In this example we have a very simple expression: the variable &lt;code&gt;answer&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;The next two lines look like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;         &lt;span class='str'&gt;"yes"&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"yay!"&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"no"&lt;/span&gt;  &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"I am sorry to hear that :("&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Notice that they are indented more than the &lt;code&gt;case&lt;/code&gt; line. This is another example of whitespace sensitive layout in Haskell. Like with the &lt;code&gt;do&lt;/code&gt; statement, each &lt;i&gt;alternative&lt;/i&gt; of the case statement will be indented the same amount. If a line is indented more, then it is a continuation of the previous line. If a line is indented less, then the previous line is the last alternative in the &lt;code&gt;case&lt;/code&gt; statement.&lt;br /&gt;&lt;br /&gt;The &lt;code&gt;case&lt;/code&gt; statement will check each alternative, in the order they are listed until it finds a &lt;i&gt;pattern&lt;/i&gt; that matches. Once a match is found, the expression on the right hand side of the &lt;code&gt;-&amp;gt;&lt;/code&gt; is evaluated. After a match is found, no further alternatives are considered.&lt;br /&gt;&lt;h5&gt;The Default Wild Card Alternative&lt;/h5&gt;&lt;br /&gt;The final line is:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;         &lt;span class='keyword'&gt;_&lt;/span&gt;     &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"say what???"&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The underscore is a wild card pattern that will match anything. So this alternative will match when the user enters something besides &lt;kbd&gt;yes&lt;/kbd&gt; or &lt;kbd&gt;no&lt;/kbd&gt;.&lt;br /&gt;&lt;h4&gt;&lt;code&gt;case&lt;/code&gt; Always Matches Exactly One Alternative&lt;/h4&gt;&lt;br /&gt;The &lt;code&gt;case&lt;/code&gt; statement will always evaluate exactly one alternative. Let's see what happens when there is more than one match or no matches at all.&lt;br /&gt;&lt;h5&gt;Overlapping Patterns&lt;/h5&gt;&lt;br /&gt;Let's say we stay up too late hacking Haskell code, and we accidentally put in the &lt;code&gt;"yes"&lt;/code&gt; alternative twice:&lt;br /&gt;&lt;pre&gt;&lt;span class='definition'&gt;main&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class='keyword'&gt;do&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"Do you like Haskell? [yes/no]"&lt;/span&gt;&lt;br /&gt;       &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;getLine&lt;/span&gt;&lt;br /&gt;       &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"yes"&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"yay!"&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"yes"&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"awesome!"&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"no"&lt;/span&gt;  &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"I am sorry to hear that :("&lt;/span&gt;&lt;br /&gt;         &lt;span class='keyword'&gt;_&lt;/span&gt;     &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"say what???"&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;When we load this into GHCi (or compile it), we get a warning:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Prelude&gt; :load "/tmp/Overlap.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /tmp/Overlap.hs, interpreted )&lt;br /&gt;&lt;br /&gt;/tmp/Overlap.hs:6:7:&lt;br /&gt;    Warning: Pattern match(es) are overlapped&lt;br /&gt;      In a case alternative: "yes" -&gt; ...&lt;br /&gt;Ok, modules loaded: Main.&lt;br /&gt;*Main&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If you try running the code, you will see that when you enter &lt;kbd&gt;yes&lt;/kbd&gt; it always prints &lt;kbd&gt;yay!&lt;/kbd&gt; and never prints &lt;kbd&gt;awesome!&lt;/kbd&gt;. Notice that if we put the wild card pattern &lt;i&gt;first&lt;/i&gt;, we will also get an overlapping pattern warning:&lt;br /&gt;&lt;pre&gt;&lt;span class='definition'&gt;main&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class='keyword'&gt;do&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"Do you like Haskell? [yes/no]"&lt;/span&gt;&lt;br /&gt;       &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;getLine&lt;/span&gt;&lt;br /&gt;       &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;&lt;br /&gt;         &lt;span class='keyword'&gt;_&lt;/span&gt;     &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"say what???"&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"yes"&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"yay!"&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"no"&lt;/span&gt;  &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"I am sorry to hear that :("&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;GHCi tells us that &lt;code&gt;"yes"&lt;/code&gt; and &lt;code&gt;"no"&lt;/code&gt; will never be considered, since &lt;code&gt;_&lt;/code&gt; matches everything:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;*Main&gt; :load "/tmp/Overlap.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /tmp/Overlap.hs, interpreted )&lt;br /&gt;&lt;br /&gt;/tmp/Overlap.hs:6:7:&lt;br /&gt;    Warning: Pattern match(es) are overlapped&lt;br /&gt;      In a case alternative:&lt;br /&gt;   "yes" -&gt; ...&lt;br /&gt;   "no" -&gt; ...&lt;br /&gt;Ok, modules loaded: Main.&lt;br /&gt;*Main&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h5&gt;Incomplete Patterns&lt;/h5&gt;&lt;br /&gt;Let's see what happens if we don't provide a default alternative:&lt;br /&gt;&lt;pre&gt;&lt;span class='definition'&gt;main&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class='keyword'&gt;do&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"Do you like Haskell? [yes/no]"&lt;/span&gt;&lt;br /&gt;       &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;getLine&lt;/span&gt;&lt;br /&gt;       &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='varid'&gt;answer&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"yes"&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"yay!"&lt;/span&gt;&lt;br /&gt;         &lt;span class='str'&gt;"no"&lt;/span&gt;  &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"I am sorry to hear that :("&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;When we load this code into GHCi, it loads with out any errors or warnings:&lt;br /&gt;&lt;pre&gt;Prelude&gt; :load "/tmp/Incomplete.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /tmp/Incomplete.hs, interpreted )&lt;br /&gt;Ok, modules loaded: Main.&lt;br /&gt;*Main&gt;&lt;/pre&gt;&lt;br /&gt;But, if we enter a string other than &lt;kbd&gt;yes&lt;/kbd&gt; or &lt;kbd&gt;no&lt;/kbd&gt;, it throws an exception:&lt;br /&gt;&lt;pre&gt;*Main&gt; main&lt;br /&gt;Do you like Haskell? [yes/no]&lt;br /&gt;whee&lt;br /&gt;*** Exception: /tmp/Incomplete.hs:(6,7)-(8,54): Non-exhaustive patterns in case&lt;br /&gt;&lt;br /&gt;*Main&gt;&lt;/pre&gt;&lt;br /&gt;It's nice that the exception tells us which file and line number the non-exhaustive pattern is at, but it would be even nicer if it told us before we tried to run the code. GHC can do this if we enable some extra warnings with the &lt;kbd&gt;-W&lt;/kbd&gt; flag. In GHCi, we can set this flag by typing &lt;kbd&gt;:set -W&lt;/kbd&gt; at the prompt:&lt;br /&gt;&lt;pre&gt;*Main&gt; :set -W&lt;br /&gt;*Main&gt; :load "/tmp/Incomplete.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /tmp/Incomplete.hs, interpreted )&lt;br /&gt;&lt;br /&gt;/tmp/Incomplete.hs:6:7:&lt;br /&gt;    Warning: Pattern match(es) are non-exhaustive&lt;br /&gt;      In a case alternative:&lt;br /&gt;   Patterns not matched:&lt;br /&gt;       []&lt;br /&gt;       (GHC.Base.C# #x) : _ with #x `notElem` ['y', 'n']&lt;br /&gt;       [GHC.Base.C# 'y']&lt;br /&gt;       (GHC.Base.C# 'y') : ((GHC.Base.C# #x) : _) with #x `notElem` ['e']&lt;br /&gt;       ...&lt;br /&gt;Ok, modules loaded: Main.&lt;br /&gt;*Main&gt;&lt;/pre&gt;&lt;br /&gt;Now, GHCi produces a (somewhat bizarre) warning, telling us that we have a non-exhaustive pattern. The last part of the error is not very easy to understand, but if we just look at the first two lines, things make sense:&lt;br /&gt;&lt;pre&gt;/tmp/Incomplete.hs:6:7:&lt;br /&gt;    Warning: Pattern match(es) are non-exhaustive&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This tells us that the &lt;code&gt;case&lt;/code&gt; statement at Line 6, Column 7 in the file Incomplete.hs does not have alternatives for all possible values.&lt;br /&gt;&lt;br /&gt;If you are compiling the code, you can just add the flag &lt;kbd&gt;-W&lt;/kbd&gt; to the command-line:&lt;br /&gt;&lt;pre&gt; $ ghc --make -O2 -W Incomplete.hs -o incomplete&lt;/pre&gt;&lt;br /&gt;You may wonder why incomplete pattern matching is not enabled by default. Consider the following example:&lt;br /&gt;&lt;pre&gt;&lt;span class='definition'&gt;main&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class='keyword'&gt;case&lt;/span&gt; &lt;span class='num'&gt;2&lt;/span&gt; &lt;span class='keyword'&gt;of&lt;/span&gt;&lt;br /&gt;      &lt;span class='num'&gt;2&lt;/span&gt; &lt;span class='keyglyph'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"2"&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;With the extra warnings enabled, this produces the warning:&lt;br /&gt;&lt;pre&gt;*Main&gt; :load "/tmp/Complete.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /tmp/Complete.hs, interpreted )&lt;br /&gt;&lt;br /&gt;/tmp/Complete.hs:2:4:&lt;br /&gt;    Warning: Pattern match(es) are non-exhaustive&lt;br /&gt;      In a case alternative:&lt;br /&gt;   Patterns not matched: #x with #x `notElem` [2#]&lt;br /&gt;Ok, modules loaded: Main.&lt;br /&gt;*Main&gt;&lt;/pre&gt;&lt;br /&gt;The warning says, you only matched on the value &lt;code&gt;2&lt;/code&gt;, but you have not handled all the cases where the value is not equal to &lt;code&gt;2&lt;/code&gt; (e.g. &lt;code&gt;1,3,4,5,6,...&lt;/code&gt;). Obviously &lt;code&gt;2&lt;/code&gt; is the only value that will ever come up, so it does not matter that the other alternatives are not matched.&lt;br /&gt;&lt;br /&gt;In this case, it is rather obvious that the warning can be ignored. A more sophisticated compiler might be able to figure this out as well, and not bother to warn you. In fact, there is a program &lt;a href="http://www-users.cs.york.ac.uk/~ndm/catch/"&gt;&lt;kbd&gt;catch&lt;/kbd&gt;&lt;/a&gt;, by Neil Mitchell, which does just that. I expect &lt;kbd&gt;catch&lt;/kbd&gt; will be integrated in GHC someday.&lt;br /&gt;&lt;h4&gt;Cool Stuff&lt;/h4&gt;&lt;br /&gt;We are not done learning about the &lt;code&gt;case&lt;/code&gt; statement yet, but we have already seen some cool stuff. If you have used other languages such as C, C++, Java, etc, you are probably familiar with a similar construct know as the &lt;code&gt;switch&lt;/code&gt; statement. However, in many languages, &lt;code&gt;switch&lt;/code&gt; only works with a few (numeric) data types. The &lt;code&gt;case&lt;/code&gt; statement in Haskell, however, can be used with (almost) all data types. In C, we would have to use a bunch of &lt;kbd&gt;if-then-else&lt;/kbd&gt; statements like:&lt;br /&gt;&lt;pre&gt;  if (!strcmp(answer,"yes"))&lt;br /&gt;    printf("yay!\n");&lt;br /&gt;  else if (!strcmp(answer,"no"))&lt;br /&gt;    printf("I am sorry to hear that :(\n");&lt;br /&gt;  else&lt;br /&gt;    printf("say what??\n");&lt;/pre&gt;&lt;br /&gt;I think you will agree that the Haskell version looks a lot more elegant and easier to comprehend. At the very least, the Haskell version is easier on the fingers to type.&lt;br /&gt;&lt;br /&gt;Aesthetics aside, Haskell can help us avoid bugs by noticing overlapping patterns or incomplete patterns. A C compiler is not likely to notice if we have overlapping or incomplete patterns in our &lt;code&gt;if-then-else-if...&lt;/code&gt; statement.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Warnings&lt;/h5&gt;&lt;br /&gt;GHC has lots of warnings that you can enable. They are documented &lt;a href="http://www.haskell.org/ghc/docs/latest/html/users_guide/options-sanity.html"&gt;here&lt;/a&gt;.  Some projects, such as &lt;a href="http://xmonad.org/"&gt;&lt;kbd&gt;xmonad&lt;/kbd&gt;&lt;/a&gt; enable all the warnings using the &lt;kbd&gt;-Wall&lt;/kbd&gt; flag, and fix all the warnings before shipping. All the extra warnings can be bothersome when you are developing. But, enabling and fixing the warnings is a good way to clean up your code and perhaps kill a few bugs before a release.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6693964363562485774-4080426912305269200?l=learnhaskell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnhaskell.blogspot.com/feeds/4080426912305269200/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6693964363562485774&amp;postID=4080426912305269200' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default/4080426912305269200'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default/4080426912305269200'/><link rel='alternate' type='text/html' href='http://learnhaskell.blogspot.com/2007/09/lesson-3-case-3.html' title='Lesson 3: &lt;code&gt;case&lt;/code&gt;'/><author><name>Jeremy Shaw</name><uri>http://www.blogger.com/profile/18373967098081701148</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://www.alchymiastudio.com/refs/DSC_5723-small.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6693964363562485774.post-1987953363560788659</id><published>2007-09-07T10:52:00.000-07:00</published><updated>2007-09-17T14:42:02.484-07:00</updated><title type='text'>Lesson 2: Input and Output, Variable Binding, and more</title><content type='html'>&lt;h4&gt;Overview&lt;/h4&gt;&lt;br /&gt;Today we will be examining this simple program:&lt;br /&gt;&lt;pre&gt;&lt;span class='keyword'&gt;module&lt;/span&gt; &lt;span class='conid'&gt;Main&lt;/span&gt; &lt;span class='keyword'&gt;where&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span class='definition'&gt;main&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt;&lt;br /&gt;    &lt;span class='keyword'&gt;do&lt;/span&gt; &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='str'&gt;"What is your name?"&lt;/span&gt;&lt;br /&gt;       &lt;span class='varid'&gt;name&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;getLine&lt;/span&gt;&lt;br /&gt;       &lt;span class='varid'&gt;putStrLn&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='str'&gt;"Hello, "&lt;/span&gt; &lt;span class='varop'&gt;++&lt;/span&gt; &lt;span class='varid'&gt;name&lt;/span&gt; &lt;span class='varop'&gt;++&lt;/span&gt; &lt;span class='str'&gt;". I think you will really like Haskell!"&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Copy this code into a file named &lt;kbd&gt;HelloYou.hs&lt;/kbd&gt;, and then run it in GHCi (&lt;kbd&gt;C-c C-l&lt;/kbd&gt;, and then run the &lt;code&gt;main&lt;/code&gt; function), or compile it an run it (&lt;kbd&gt;M-C ghc --make -O2 HelloYou.hs -o helloYou&lt;/kbd&gt;).&lt;br /&gt;&lt;br /&gt;Files containing Haskell source code will almost always end with the extention &lt;kbd&gt;.hs&lt;/kbd&gt;. You should follow this convention as well, because the compiler expects it.&lt;br /&gt;&lt;h4&gt;&lt;code&gt;do&lt;/code&gt; notation&lt;/h4&gt;&lt;br /&gt;The first new thing we see is the &lt;code&gt;do&lt;/code&gt; keyword. In this context, the &lt;code&gt;do&lt;/code&gt; keyword indicates that we want to perform several IO (input/output) actions in a row. &lt;br /&gt;&lt;br /&gt;Notice how each action is indented the same amount. In Haskell, the layout of the code is significant. The &lt;code&gt;do&lt;/code&gt; statement automatically ends when a line that is indented less is encountered.&lt;br /&gt;&lt;br /&gt;For example, in this code:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;main =&lt;br /&gt;    do putStrLn "What is your name?"&lt;br /&gt;       name &lt;- getLine&lt;br /&gt;       putStrLn ("Hello, " ++ name ++ ". I think you will really like Haskell!")&lt;br /&gt;&lt;br /&gt;cheese = "cheddar"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Because &lt;code&gt;cheese = "cheddar"&lt;/code&gt; starts at the first column, it is not part of the previous &lt;code&gt;do&lt;/code&gt; statement. The blank line before &lt;code&gt;cheese&lt;/code&gt; is ignored.&lt;br /&gt;&lt;br /&gt;If a line is indented more, then it is considered to be a continuation of the previous line. For example, we can reformat our program so that the third action is split across two lines:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;main =&lt;br /&gt;    do putStrLn "What is your name?"&lt;br /&gt;       name &lt;- getLine&lt;br /&gt;       putStrLn ("Hello, " ++ name ++ &lt;br /&gt;                 ". I think you will really like Haskell!")&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The meaning is not changed at all.&lt;br /&gt;&lt;h4&gt;Variable Binding&lt;/h4&gt;&lt;br /&gt;The next new thing we see is this line:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;       &lt;span class='varid'&gt;name&lt;/span&gt; &lt;span class='keyglyph'&gt;&amp;lt;-&lt;/span&gt; &lt;span class='varid'&gt;getLine&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;code&gt;getLine&lt;/code&gt; is an IO action that reads a line of input from stdin. The &lt;code&gt;&amp;lt;-&lt;/code&gt; operator &lt;i&gt;binds&lt;/i&gt; the variable &lt;code&gt;name&lt;/code&gt; to the value read by &lt;code&gt;getLine&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;What Does &lt;i&gt;bind&lt;/i&gt; Mean?&lt;/h5&gt;&lt;br /&gt;You can imagine that the value returned by &lt;code&gt;getLine&lt;/code&gt; is a cardboard box with a &lt;code&gt;String&lt;/code&gt; inside. &lt;i&gt;binding&lt;/i&gt; &lt;code&gt;name&lt;/code&gt; to the value is like putting a label on the cardboard box. This makes it easy to refer to that value later, because we can just use the variable &lt;kbd&gt;name&lt;/kbd&gt;.&lt;br /&gt;&lt;h5&gt;Not Like Variables You Have Seen Before&lt;/h5&gt;&lt;br /&gt;&lt;div class="aside"&gt;&lt;br /&gt;If you have used other programming languages, you are probably familiar with  a different kind of variable. For example, in C, you would declare a variable &lt;code&gt;i&lt;/code&gt; and assign it the value &lt;code&gt;9&lt;/code&gt; like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    int i;&lt;br /&gt;    i = 9;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This called a &lt;i&gt;destructive update&lt;/i&gt; and is different than &lt;i&gt;binding&lt;/i&gt;  a variable. If we think about the box analogy, the statement &lt;code&gt;int i;&lt;/code&gt; creates a new cardboard box with the label &lt;code&gt;i&lt;/code&gt; already attached to it. The statement &lt;code&gt;i = 9;&lt;/code&gt; opens up the box, destroys the current contents, and then puts &lt;code&gt;9&lt;/code&gt; in the box.&lt;br /&gt;&lt;br /&gt;This difference is pretty substantial -- just imagine how you would feel if you were still using the old contents of the box! We will cover this concept more in a few lessons, and see why the difference is so exciting.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;h4&gt;The &lt;code&gt;++&lt;/code&gt; operator&lt;/h4&gt;&lt;br /&gt;The next new thing we see is the &lt;code&gt;++&lt;/code&gt; operator. &lt;code&gt;++&lt;/code&gt; concatenates two lists together and returns a new list. In Haskell, a String is just a list a characters.&lt;br /&gt;&lt;h4&gt;Where did those () come from?&lt;/h4&gt;&lt;br /&gt;In the first lesson, we noted that Haskell does not require you to use parentheses when calling a function; but now we have some parentheses, so what's the deal? The parentheses in Haskell are used to group operations, in the same way you would in math.&lt;br /&gt;&lt;br /&gt;So in this line:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;       putStrLn ("Hello, " ++ name ++ ". I think you will really like Haskell!")&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The parentheses indicated that we want to concatenate the strings first, and  then apply &lt;code&gt;putStrLn&lt;/code&gt; to the new string. If we did not use parentheses, the compiler would add implicit parentheses like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;       (putStrLn "Hello, ") ++ (name ++ ". I think you will really like Haskell!")&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;which does not make any sense; it says we want to print the string &lt;kbd&gt;Hello ,&lt;/kbd&gt; and then append the &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;(name ++ ". I think you will really like Haskell!")&lt;/code&gt;, to the value returned by &lt;code&gt;putStrLn&lt;/code&gt;.&lt;br /&gt;&lt;h4&gt;No Variable Declarations, But Still Safe&lt;/h4&gt;&lt;br /&gt;In our program, we use a variable called &lt;code&gt;name&lt;/code&gt;. You may have noticed that we did not declare that variable before we started using it. That saves us some typing, which is nice, but what happens if we make a typo and spell it &lt;code&gt;nmae&lt;/code&gt;? Let's load this code into GHCi and see:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;module Main where&lt;br /&gt;&lt;br /&gt;main =&lt;br /&gt;    do putStrLn "What is your name?"&lt;br /&gt;       name &lt;- getLine&lt;br /&gt;       putStrLn ("Hello, " ++ nmae ++ ". I think you will really like Haskell!")&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;GHCi reports the following error:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;*Main&gt; :load "/root/n-heptane/docs/haskell-lessons/HelloYou.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /root/n-heptane/docs/haskell-lessons/HelloYou.hs, interpreted )&lt;br /&gt;&lt;br /&gt;/root/n-heptane/docs/haskell-lessons/HelloYou.hs:6:30:&lt;br /&gt;    Not in scope: `nmae'&lt;br /&gt;Failed, modules loaded: none.&lt;br /&gt;Prelude&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Nice! GHC tells us that the identifier `nmae' at line 6, column 30, is not defined. Notice that we have not tried to &lt;i&gt;run&lt;/i&gt; the code yet, the bug was detected at &lt;i&gt;compile&lt;/i&gt; time. So, we get the best of both worlds: we don't have to tell the compiler about our variables before we use them, but the compiler can still tell us if we accidentally use an undefined variable.&lt;br /&gt;&lt;h4&gt;Cool Stuff We Learned Today&lt;/h4&gt;&lt;br /&gt;Today's code example was pretty simple, but, we managed to avoid four extremely common bugs that have been responsible for thousands of security holes and program crashes.&lt;br /&gt;&lt;br /&gt;&lt;h5&gt;Safe from &lt;i&gt;Buffer Overflows&lt;/i&gt;&lt;/h5&gt;&lt;br /&gt;The first two bugs we avoid are buffer overflows. Buffer overflows are an extremely common source of security holes and program crashes. Buffer overflows occur when a string is too big to fit in the space allocated for it, or when some code thinks a string is longer than it really is, and tries to read characters beyond the end of the string.&lt;br /&gt;&lt;br /&gt;Two common places to encounter buffer overflows are when you are reading input and receive more input than you expected, or when you are concatenating strings and don't allocate enough space, or you accidentally copy too much data.&lt;br /&gt;&lt;br /&gt;In our code sample of the day, we read input with &lt;code&gt;getLine&lt;/code&gt; and concatenate &lt;code&gt;String&lt;/code&gt;s with &lt;code&gt;++&lt;/code&gt;. But, we never had to worry about how long the &lt;code&gt;String&lt;/code&gt;s were, it was all handled automatically for us.&lt;br /&gt;&lt;br /&gt;Safety from buffer overflows is nice, but does not really set Haskell apart. Many other languages, such as Java, Python, Perl, Ruby, etc, are also safe from buffer overflows in this way (as far as I known). So let's look at the next two bugs we managed to avoid, which are a bit more interesting.&lt;br /&gt;&lt;h5&gt;Safe from Uninitialized Variables&lt;/h5&gt;&lt;br /&gt;We saw earlier that when we misspelled &lt;code&gt;name&lt;/code&gt; as &lt;code&gt;nmae&lt;/code&gt;, the compiler caught our mistake. It noticed that we were trying to use the variable &lt;code&gt;nmae&lt;/code&gt;, but &lt;code&gt;nmae&lt;/code&gt; had not been bound to anything yet.&lt;br /&gt;&lt;br /&gt;This is really nice! In many languages that do not required you to declare your variables in advance, you would not notice this bug until you ran the program. In some cases the program would die when it tried to use &lt;code&gt;nmae&lt;/code&gt;. In other cases, it would just assume that &lt;code&gt;nmae&lt;/code&gt; was equal to the empty string "", so you might not even notice the problem!&lt;br /&gt;&lt;br /&gt;Even in languages where you have to declare your variables in advance, we are susceptible to a similar bug. Consider the following C code:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    int a;&lt;br /&gt;    int b;&lt;br /&gt;    int c;&lt;br /&gt;&lt;br /&gt;    b = 1;&lt;br /&gt;    c = a + b;&lt;br /&gt;    printf("%d\n", c);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Even though we declared our variables &lt;code&gt;a,b,&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;, I &lt;i&gt;forgot&lt;/i&gt; to assign a value to &lt;code&gt;a&lt;/code&gt;. This means that when try to use &lt;code&gt;a&lt;/code&gt; to calculate &lt;code&gt;c&lt;/code&gt; we have absolutely no idea what will happen. On my system &lt;code&gt;c&lt;/code&gt; was equal to &lt;code&gt;-1077263575&lt;/code&gt; the first time I ran it, and &lt;code&gt;-1080545591&lt;/code&gt; the second time.&lt;br /&gt;&lt;br /&gt;Fortunately, Haskell goes the extra step and requires that all the variables we use are actually bound to a value, so we will not be seeing of those nasty behaviors.&lt;br /&gt;&lt;h5&gt;How It Looks Is Meaningful&lt;/h5&gt;&lt;br /&gt;We saw in this lesson that the formatting of the code is important to the compiler. This is nice because the code looks pretty, and we don't have to type in lots of extra characters like &lt;code&gt;(){};&lt;/code&gt;. But it also helps us avoid bugs. Almost all programmers try to format their code so that you can understand the flow of the code by the way it looks. But, in most languages, the compiler does not care how the code looks. Consider the following C code:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    if (something)&lt;br /&gt;        doSomething();&lt;br /&gt;        doSomethingElse();&lt;br /&gt;&lt;br /&gt;    doSomethingToo();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Even though &lt;code&gt;doSomethingElse()&lt;/code&gt; &lt;i&gt;looks&lt;/i&gt; like it is inside the &lt;code&gt;if&lt;/code&gt; statement, it is not. The compiler reads it like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;    if (something)&lt;br /&gt;        doSomething();&lt;br /&gt;&lt;br /&gt;    doSomethingElse();&lt;br /&gt;    doSomethingToo();&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Ouch! Since Haskell does care about the formatting, you are far less likely to see things differently than the compiler does.&lt;br /&gt;&lt;h4&gt;Summary&lt;/h4&gt;&lt;br /&gt;Well, I think I went over my time again, but there was a lot of new material, even though today's program was only two lines longer than yesterday's. Next time we will learn about the &lt;code&gt;case&lt;/code&gt; statement. This will allow us to do different things depending on what the user enters.&lt;br /&gt;&lt;br /&gt;You don't need to memorize or perfectly understand everything in this lesson. We will be exploring these concepts more in the upcoming lessons, which should help you to remember and understand them.&lt;br /&gt;&lt;h4&gt;emacs corner: &lt;kbd&gt;TAB&lt;/kbd&gt; indenting&lt;/h4&gt;&lt;br /&gt;&lt;div class="emacs"&gt;&lt;br /&gt;In the previous lesson, we saw that using &lt;kbd&gt;emacs&lt;/kbd&gt; makes it easy to load programs into GHCi. You probably also noticed that &lt;kbd&gt;emacs&lt;/kbd&gt; colored the source code for you. In this lesson, we learned that the indentation of each line is significant. &lt;kbd&gt;emacs&lt;/kbd&gt; can help here too. If you copied and pasted the code into &lt;kbd&gt;emacs&lt;/kbd&gt; try typing it in by hand instead. When you need to indent a line, press the &lt;kbd&gt;TAB&lt;/kbd&gt; key a couple times in a row. You will see that &lt;kbd&gt;emacs&lt;/kbd&gt; cycles through the different possibilities. Personally, I find this feature extremely useful.&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6693964363562485774-1987953363560788659?l=learnhaskell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnhaskell.blogspot.com/feeds/1987953363560788659/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6693964363562485774&amp;postID=1987953363560788659' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default/1987953363560788659'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default/1987953363560788659'/><link rel='alternate' type='text/html' href='http://learnhaskell.blogspot.com/2007/09/lesson-2-input-and-output-variable.html' title='Lesson 2: Input and Output, Variable Binding, and more'/><author><name>Jeremy Shaw</name><uri>http://www.blogger.com/profile/18373967098081701148</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://www.alchymiastudio.com/refs/DSC_5723-small.jpg'/></author><thr:total>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6693964363562485774.post-1792742398527071458</id><published>2007-09-06T11:35:00.000-07:00</published><updated>2008-02-05T11:23:32.291-08:00</updated><title type='text'>Lesson 1: Hello, World!</title><content type='html'>&lt;h4&gt;Installing Haskell and supporting tools&lt;/h4&gt;&lt;br /&gt;&lt;h5&gt;Disclaimer&lt;/h5&gt;&lt;br /&gt;In this series I am using the following software packages on a Debian based GNU/Linux distribution:&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="http://www.haskell.org/ghc/"&gt;GHC 6.6.1&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="http://www.gnu.org/software/emacs/"&gt;Emacs 21&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;a href="http://www.iro.umontreal.ca/~monnier/elisp/#haskell-mode"&gt;Haskell Mode 2.3 (for Emacs)&lt;/a&gt;&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;br /&gt;In some of the initial lessons I assume you are as well, but if you are not, you should still be able to follow along. Once we really get into the language, it should not matter much, until we get to advanced lessons that use features only found in recent versions of GHC.&lt;br /&gt;&lt;h5&gt;Installing&lt;/h5&gt;&lt;br /&gt;First run:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; $ sudo apt-get install emacs21 ghc6&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Then check that haskell-mode 2.3 or higher is available:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; $ sudo apt-cache policy haskell-mode&lt;br /&gt;haskell-mode:&lt;br /&gt;  Installed: 2.3-0cnr1&lt;br /&gt;  Candidate: 2.3-0cnr1&lt;br /&gt;  Version table:&lt;br /&gt; *** 2.3-0cnr1 0&lt;br /&gt;        500 http://apt2.freespire.org skipjack-feisty/main Packages&lt;br /&gt;        100 /var/lib/dpkg/status&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If it is, run the following command and skip to the next section:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; $ sudo apt-get install haskell-mode&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;If the version of haskell-mode is less than 2.3, then temporarily add this line to your /etc/apt/sources.list:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;deb http://apt2.freespire.org/CNRUbuntu skipjack-feisty main&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and run:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt; $ sudo apt-get update &lt;br /&gt; $ sudo apt-get install haskell-mode&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;and, finally, remove the skipjack-feisty line from your sources.list.&lt;br /&gt;&lt;h5&gt;Configure Emacs and Haskell mode&lt;/h5&gt;&lt;br /&gt;Add the following lines to your &lt;kbd&gt;~/.emacs&lt;/kbd&gt; file and restart emacs:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;;; Font Locking, Programming Modes, and Compilation settings&lt;br /&gt;;;&lt;br /&gt;&lt;br /&gt;(global-font-lock-mode 1)&lt;br /&gt;;; maximum colors&lt;br /&gt;(setq font-lock-maximum-decoration t)&lt;br /&gt;&lt;br /&gt;;; extra key bindings&lt;br /&gt;(global-set-key "\M-C" 'compile)&lt;br /&gt;(global-set-key "\C-^" 'next-error)&lt;br /&gt;(global-set-key "\C-\M-g" 'goto-line)&lt;br /&gt;&lt;br /&gt;;; use spaces instead of tabs&lt;br /&gt;(setq-default indent-tabs-mode nil)&lt;br /&gt;&lt;br /&gt;;; haskell mode configuration&lt;br /&gt;(setq auto-mode-alist&lt;br /&gt;      (append auto-mode-alist&lt;br /&gt;              '(("\\.[hg]s$"  . haskell-mode)&lt;br /&gt;                ("\\.hic?$"     . haskell-mode)&lt;br /&gt;                ("\\.hsc$"     . haskell-mode)&lt;br /&gt;  ("\\.chs$"    . haskell-mode)&lt;br /&gt;                ("\\.l[hg]s$" . literate-haskell-mode))))&lt;br /&gt;(autoload 'haskell-mode "haskell-mode"&lt;br /&gt;   "Major mode for editing Haskell scripts." t)&lt;br /&gt;(autoload 'literate-haskell-mode "haskell-mode"&lt;br /&gt;   "Major mode for editing literate Haskell scripts." t)&lt;br /&gt;&lt;br /&gt;;adding the following lines according to which modules you want to use:&lt;br /&gt;(require 'inf-haskell)&lt;br /&gt;&lt;br /&gt;(add-hook 'haskell-mode-hook 'turn-on-font-lock)&lt;br /&gt;;(add-hook 'haskell-mode-hook 'turn-off-haskell-decl-scan)&lt;br /&gt;;(add-hook 'haskell-mode-hook 'turn-off-haskell-doc-mode)&lt;br /&gt;(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)&lt;br /&gt;;(add-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)&lt;br /&gt;;(add-hook 'haskell-mode-hook 'turn-on-haskell-hugs)&lt;br /&gt;(add-hook 'haskell-mode-hook 'turn-on-haskell-ghci)&lt;br /&gt;(add-hook 'haskell-mode-hook &lt;br /&gt;   (function&lt;br /&gt;    (lambda ()&lt;br /&gt;      (setq haskell-program-name "ghci")&lt;br /&gt;      (setq haskell-ghci-program-name "ghci6"))))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h4&gt;&lt;kbd&gt;Hello, World&lt;/kbd&gt;&lt;/h4&gt;&lt;br /&gt;Run &lt;kbd&gt;emacs HelloWorld.hs&lt;/kbd&gt;, and enter the following program:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;module Main where&lt;br /&gt;&lt;br /&gt;main = putStrLn "Hello, World!"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To save press &lt;kbd&gt;Control-x&lt;/kbd&gt; followed by &lt;kbd&gt;Control-s&lt;/kbd&gt;. In emacs-speak this is written as &lt;kbd&gt;C-x C-s&lt;/kbd&gt;.&lt;br /&gt;&lt;h5&gt;Loading the code into GHC Interactive&lt;/h5&gt;&lt;br /&gt;Now we can load the code into the GHC intepreter by pressing &lt;kbd&gt;C-c C-l&lt;/kbd&gt;.&lt;br /&gt;This should open another buffer in emacs that looks something like this:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;   ___         ___ _&lt;br /&gt;  / _ \ /\  /\/ __(_)&lt;br /&gt; / /_\// /_/ / /  | |      GHC Interactive, version 6.6.1, for Haskell 98.&lt;br /&gt;/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/&lt;br /&gt;\____/\/ /_/\____/|_|      Type :? for help.&lt;br /&gt;&lt;br /&gt;Loading package base ... linking ... done.&lt;br /&gt;Prelude&gt; :load "/tmp/HelloWorld.hs"&lt;br /&gt;[1 of 1] Compiling Main             ( /tmp/HelloWorld.hs, interpreted )&lt;br /&gt;Ok, modules loaded: Main.&lt;br /&gt;*Main&gt; &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;To switch between the code and ghci buffers press &lt;kbd&gt;C-x o&lt;/kbd&gt;, or use the mouse and click inside the buffer you want active.&lt;br /&gt;&lt;br /&gt;To run the &lt;kbd&gt;main&lt;/kbd&gt; function, just type &lt;kbd&gt;main&lt;/kbd&gt; and hit enter at the &lt;kbd&gt;*Main&amp;gt;&lt;/kbd&gt; prompt:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;*Main&gt; main&lt;br /&gt;Hello, World!&lt;br /&gt;*Main&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h5&gt;Compiling the Code&lt;/h5&gt;&lt;br /&gt;Switch back to the &lt;kbd&gt;HelloWorld.hs&lt;/kbd&gt; buffer and enter &lt;kbd&gt;M-C&lt;/kbd&gt;.   The &lt;kbd&gt;M&lt;/kbd&gt; stands for &lt;kbd&gt;Meta&lt;/kbd&gt;, which is usually the key labelled &lt;kbd&gt;Alt&lt;/kbd&gt;, and the &lt;kbd&gt;C&lt;/kbd&gt; is uppercase. So you will need to press &lt;kbd&gt;Alt-Shift-c&lt;/kbd&gt;. At the bottom of the emacs window it should now say:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Compile command: make -k &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Change it to&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;Compile command: ghc --make -O2 HelloWorld.hs -o helloWorld&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You should now have a &lt;kbd&gt;*compilation*&lt;/kbd&gt; buffer that looks like&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;cd /tmp/&lt;br /&gt;ghc --make -O2 HelloWorld.hs -o helloWorld&lt;br /&gt;[1 of 1] Compiling Main             ( HelloWorld.hs, HelloWorld.o )&lt;br /&gt;Linking helloWorld ...&lt;br /&gt;&lt;br /&gt;Compilation finished at Thu Sep  6 12:33:33&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now open a shell, and run the executable:&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;lain:/tmp $ ./helloWorld &lt;br /&gt;Hello, World!&lt;br /&gt;lain:/tmp $ &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;The parts of the command-line are pretty straight forward:&lt;br /&gt;&lt;dl&gt;&lt;br /&gt;&lt;dt&gt;&lt;kbd&gt;ghc&lt;/kbd&gt;&lt;/dt&gt;&lt;dd&gt;The name of the compiler&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;kbd&gt;--make&lt;/kbd&gt;&lt;/dt&gt;&lt;dd&gt;This tells the compiler that we want to compile a module and all of its dependencies, and produce an executable that we can run.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;kbd&gt;-O2&lt;/kbd&gt;&lt;/dt&gt;&lt;dd&gt;This tells the compiler that we want to enable level 2 optimizations (the highest level available). This should result in an executable that runs faster.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;kbd&gt;HelloWorld.hs&lt;/kbd&gt;&lt;/dt&gt;&lt;dd&gt;This is the name of the file that contains the &lt;kbd&gt;main&lt;/kbd&gt; function.&lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;kbd&gt;-o helloWorld&lt;/kbd&gt;&lt;/dt&gt;&lt;dd&gt;This tells the compiler that the executable should be named &lt;kbd&gt;helloWorld&lt;/kbd&gt;. If you do not specify a name, it will probably default to &lt;kbd&gt;a.out&lt;/kbd&gt;&lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;h5&gt;Parts of the Program&lt;/h5&gt;&lt;br /&gt;The program is pretty short, but let's go over the details anyway.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;module Main where&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This tells the compiler that this file contains the &lt;kbd&gt;Main&lt;/kbd&gt; module. &lt;br /&gt;When your program is run, it always starts at the &lt;kbd&gt;main&lt;/kbd&gt; function in the module &lt;kbd&gt;Main&lt;/kbd&gt;.&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;main = putStrLn "Hello, World"&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This defines a function named &lt;kbd&gt;main&lt;/kbd&gt; that prints &lt;kbd&gt;Hello, World&lt;/kbd&gt; followed by a carriage return to &lt;kbd&gt;stdout&lt;/kbd&gt;. &lt;kbd&gt;putStrLn&lt;/kbd&gt; is a function, and &lt;kbd&gt;"Hello, World"&lt;/kbd&gt; is a &lt;kbd&gt;String&lt;/kbd&gt;. To call a function with an argument, we simply put some whitespace after the function name followed by the argument. &lt;br /&gt;&lt;h4&gt;Cool Stuff We Learned&lt;/h4&gt;&lt;br /&gt;This lesson was a bit long, because we had to install stuff, but we learned some cool stuff:&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Haskell code can be compiled to an executable, or run interactively&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Haskell syntax is very clean. We do not need lots of characters like &lt;kbd&gt;{}();&lt;/kbd&gt; to get stuff done&lt;/li&gt;&lt;br /&gt;&lt;li&gt;We are not &lt;i&gt;required&lt;/i&gt; to declare the types of our functions anywhere&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/6693964363562485774-1792742398527071458?l=learnhaskell.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnhaskell.blogspot.com/feeds/1792742398527071458/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=6693964363562485774&amp;postID=1792742398527071458' title='17 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default/1792742398527071458'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6693964363562485774/posts/default/1792742398527071458'/><link rel='alternate' type='text/html' href='http://learnhaskell.blogspot.com/2007/09/lesson-1-hello-world.html' title='Lesson 1: Hello, World!'/><author><name>Jeremy Shaw</name><uri>http://www.blogger.com/profile/18373967098081701148</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='21' height='32' src='http://www.alchymiastudio.com/refs/DSC_5723-small.jpg'/></author><thr:total>17</thr:total></entry></feed>
