<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Sententia cdsmithus &#187; Haskell for Kids</title>
	<atom:link href="http://cdsmith.wordpress.com/category/haskell-for-kids/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdsmith.wordpress.com</link>
	<description>software, programming languages, and other ideas</description>
	<lastBuildDate>Sun, 12 May 2013 23:56:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='cdsmith.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Sententia cdsmithus &#187; Haskell for Kids</title>
		<link>http://cdsmith.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://cdsmith.wordpress.com/osd.xml" title="Sententia cdsmithus" />
	<atom:link rel='hub' href='http://cdsmith.wordpress.com/?pushpress=hub'/>
		<item>
		<title>CodeWorld and the Future</title>
		<link>http://cdsmith.wordpress.com/2012/11/18/codeworld-and-the-future/</link>
		<comments>http://cdsmith.wordpress.com/2012/11/18/codeworld-and-the-future/#comments</comments>
		<pubDate>Mon, 19 Nov 2012 05:46:06 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=617</guid>
		<description><![CDATA[I&#8217;ve been thinking a lot the last few weeks about the future of CodeWorld.  I&#8217;m writing this to share my vision of where things are going, and why.  I&#8217;d appreciate feedback from anyone with thoughts on the future, different ideas, and so on. And Overview of the Past For anyone new to the ideas, I [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=617&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been thinking a lot the last few weeks about the future of CodeWorld.  I&#8217;m writing this to share my vision of where things are going, and why.  I&#8217;d appreciate feedback from anyone with thoughts on the future, different ideas, and so on.</p>
<h2>And Overview of the Past</h2>
<p>For anyone new to the ideas, I should review a little about what CodeWorld is.  CodeWorld is the name for the curriculum I built and taught last school year.  It has the goal of teaching abstract and mathematical reasoning skills through computer programming.  Throughout the course of the year, students use a web site to write descriptions of pictures, then animations, and finally video games, and run them in the web browser to immediately see the results.</p>
<p>I taught a pilot program last school year in a neighborhood school, and it was a huge success.  I have high hopes of getting something going again in the future.</p>
<h2>What Happened&#8230;</h2>
<p>Many people have noticed that CodeWorld stopped in its tracks some time during the summer.  What ended up happening was a fairly big life change for me: I&#8217;ve changed jobs (now working for Google on YouTube!), moved (to San Francisco from Colorado), and have been focused on personal changes for a while.  Unfortunately, that meant I had to cancel the plans to teach CodeWorld at <em>three schools</em> in Colorado this school year.  I hope, though, this is only a temporary setback!  I still have high hopes for the future.</p>
<h2>Part 1: The Technology</h2>
<p>In terms of technology, CodeWorld is built on several things:</p>
<ol>
<li>The Haskell programming language.  This is a language that&#8217;s very well suited for CodeWorld because it expresses things in a way that&#8217;s very declarative and consistent with the mental models students should bring into algebra and other abstract mathematics classes.</li>
<li>The Gloss library.  This library, originally developed for introductory college programming classes, is a great fit for many of the same reasons.  It&#8217;s based on Haskell, and again does everything declaratively.  It&#8217;s also very concise, not needing a lot of wordy boilerplate to get something quickly on the screen.</li>
<li>The SafeHaskell feature of GHC.  This allows the server to safely compile and run code written by students without worrying that it might delete critical files and such.</li>
</ol>
<p>In some ways, the set of technology chosen for CodeWorld is perfect.  Specifically, it let me get a web site up and running in about a week of work, right before the start of the class last school year.  That saved a lot of hassle, and made the class a lot more successful.  In other ways, though, each of these choices is lacking in a few ways.</p>
<ol>
<li>Haskell is a great choice of language, but it&#8217;s very difficult to target Haskell to run in web browsers.  For this reason, CodeWorld actually runs students&#8217; programs way over in a data center, far from their computers.  This worked and was easy to do (great!), but it also means programs run very slowly, and the server gets bogged down if it&#8217;s used by too many people at once (not so good).</li>
<li>Gloss is the closest thing we have to a perfect choice.  Even that, though, has its limitations.  Because Gloss has a number of users who want to do more advanced things (like use it to show off parallel processing APIs in Haskell), there are some technical pieces in the back end that are difficult to fit in with a web browser as well.  Also, mainly due to backward compatibility, there are inconsistencies in the API (circle to draw the outline of a circle versus rectangleWire to do the same with a rectangle, for example), inconsistencies with what students will see in math classes (angles are clockwise, for example, in the official implementation), and lost opportunities to make small tweaks that will more clearly emphasize declarative thinking.  We definitely want something <strong>like </strong>Gloss, but there are a few details that it would help to tweak in some incompatible ways.</li>
<li>SafeHaskell turns out to be problematic for a variety of reasons.  Aside from the performance problems from running on the server in the first place, it&#8217;s really only a small part of a solution to the problems of running untrusted code.  For example, it has no protections against using excessive resources and slowing down the server until it&#8217;s unusable.</li>
</ol>
<p>So I&#8217;ve been evaluating a new technical path.  This new approach uses Chris Done&#8217;s language called <a href="http://fay-lang.org/">Fay</a>, which is a subset of Haskell specifically designed to run in web browsers.  It has all of those advantages of Haskell that I mentioned above, but works better in a web browser.  I&#8217;ve been porting the Gloss library over to Fay, but taking liberties to modify some of the inconsistent behaviors and lost opportunities as I go.  SafeHaskell then becomes entirely unnecessary, as the code is no longer running on the server.  It&#8217;s running in the student&#8217;s web browser instead, where JavaScript is already thoroughly sandboxed by companies that put lots of resources into the problem.</p>
<p>This technology migration isn&#8217;t done yet, and there&#8217;s still a lot of little syntax stuff that&#8217;s frustratingly unsupported by Fay&#8230; but I&#8217;m working on it, helping out the Fay project a little but, too, and it&#8217;ll get there!</p>
<h2>Part 2: The Presentation</h2>
<p>In addition to changing technologies, I&#8217;ve been putting a lot of thought into what I want the future of the system to look like.  I am looking at how to make the platform more appealing when I am not personally living right by the school and able to pop over and help with any students who are having problems.</p>
<p>One side of this is definitely a redesign of the web site.  The site needs examples, it needs to look more finished, and it needs documentation!  I&#8217;m working on a mock-up of the future web site, and while my web design skills are definitely lacking, I think I&#8217;m happy with the concept so far.  You can <a href="http://zelda.designacourse.com/codeworld/codeworld.html">take a look</a>, if you want, and let me know what you think.</p>
<p>Another side actually was started over the summer, and that&#8217;s putting together a comprehensive teacher&#8217;s guide, including worksheets, exercises, and organization of the content.  This needs to be more than just a web site; it needs to be an organized approach to presenting the ideas and guiding students through the learning process.  I&#8217;m strongly considering making a somewhat involved video series to present the concepts, as well.</p>
<h2>Part 3: Community</h2>
<p>The third and final part of this is putting the pieces in place for a sustainable community of people working together.  I don&#8217;t have any specific plans here, yet&#8230; but I&#8217;ll just say that I see positive things ahead.  I&#8217;m now working with tens of thousands of brilliant people, many of whom care very much about education, in a company that encourages its employees to experiment with technology in ways that aren&#8217;t focused on just one product.  I&#8217;m also within a short drive of the Khan Academy, and plenty of other groups that care a lot about math education.</p>
<p>Some things I&#8217;d like to try here:</p>
<ol>
<li>Getting together students who build things with CodeWorld to meet and share their creations.</li>
<li>Helping students make the transition from developing things for &#8220;a school project&#8221; to sharing them with friends and others, and possibly even offering their creations in various app stores or markets.  (Student creations in CodeWorld actually tend to be fairly similar to the casual game market for smartphones and such&#8230;)</li>
<li>Creating more resources for teachers who want to incorporate this into their schools.  I&#8217;d love to hold seminars and such for teachers!  Please ask me to do this, once the pieces are in place!</li>
</ol>
<p>So that&#8217;s where CodeWorld is, and where it&#8217;s hopefully going.  I&#8217;m still very excited about the future of this effort, and looking forward to making some more progress here soon.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/617/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/617/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=617&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2012/11/18/codeworld-and-the-future/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Juggling in Haskell and Gloss</title>
		<link>http://cdsmith.wordpress.com/2012/02/14/juggling-in-haskell-and-gloss/</link>
		<comments>http://cdsmith.wordpress.com/2012/02/14/juggling-in-haskell-and-gloss/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 19:23:32 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>
		<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=495</guid>
		<description><![CDATA[Just sharing what we&#8217;ve been talking about in my classes at LSV (Little School on Vermijo) the past few days.  It all started when a math teacher I follow on Google+ posted a link to this video on the mathematics of juggling, by Cornell professor Allen Knutson. So we all watched the video, and talked [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=495&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Just sharing what we&#8217;ve been talking about in my classes at LSV (Little School on Vermijo) the past few days.  It all started when a math teacher I follow on Google+ posted a link to this video on the mathematics of juggling, by Cornell professor Allen Knutson.</p>
<span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='595' height='365' src='http://www.youtube.com/embed/38rf9FLhl-8?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span>
<p>So we all watched the video, and talked about it&#8230; and then about midnight last night, I realized this would make a perfect introduction to Gloss simulations, which we&#8217;re learning about in our Haskell programming class!  Fast forward about 20 minutes of hacking, and we have this short program:</p>
<blockquote>
<pre>import Graphics.Gloss
thePattern = [5,2,5,1,2]
initial g = (-1, 0.0, [], cycle thePattern)
step dt (hand, time, balls, pattern) = (newhand, newtime, newballs, newpattern)
    where (throw, newtime) = properFraction (time + dt)
          newhand    = if throw == 1 then -hand else hand
          thrown     = if throw == 1 then [ (hand, newtime, head pattern) ]
                                     else []
          newpattern = if throw == 1 then tail pattern else pattern
          newballs   = [ (bhand, btime + dt, height)
                       | (bhand, btime, height) &lt;- balls,
                         btime + dt &lt; fromIntegral height ]
                       ++ thrown
draw (hand, time, balls, pattern) = pictures [
    juggler,
    pictures [ ball b | b &lt;- balls ]
    ]
ball (bhand, btime, height) = translate (50*x) (50*y) (circleSolid 10)
    where t = 1 - 2 * (btime / fromIntegral height)
          x = if even height then bhand else bhand * t
          y = if height &lt; 3 then 0 else fromIntegral (height - 1) * (1 - t^2)
juggler = pictures [
    line [(-50, 0), (0, 25), (50, 0)],
    line [(-30, -100), (0, -50), (30, -100)],
    line [( 0, 25), (0, -50)],
    translate 0 50 (circle 25)
    ]</pre>
</blockquote>
<p>Feel free to <a href="http://dac4.designacourse.com:8000/simulateInBrowser?digest=pVrxT4T5JmIWBjSRK3ROfA%3d%3d">check it out</a>.</p>
<p>This isn&#8217;t the best code in the world, because I deliberately write it to avoid as many ideas as I can that we haven&#8217;t talked about in my Haskell class.  Among those, for example, are any kind of user-defined data types, or any structured data other than tuples and lists!  I&#8217;ve also used list comprehensions when maps and filters might be clearer, for the same reason: we&#8217;ve <em>done</em> list comprehensions in class!  Those are coming soon, but for the time being, it&#8217;s not too hard to see what&#8217;s going on here.  A couple notes about the world type:</p>
<ul>
<li>The state of world comprises four pieces of information: which hand will throw next, how much time has passed since the last throw, what balls are in the air, and what pattern is coming up next.  Since we don&#8217;t have user-defined types and it makes the math easy, I&#8217;ve described hands as -1 for left, and +1 for right.</li>
<li>The balls in the air form a list, but each element of that list is another tuple, containing the hand the ball was thrown from, how long it has been in the air, and its height.</li>
</ul>
<p>So like all Gloss simulations, we need to specify three things:</p>
<ol>
<li><em>The initial state.</em>  This is the left hand, no time since the last throw, no balls in the air, and the pattern sitting ahead of us.  The cycle function turns a finite list into an infinite repeating one.</li>
<li><em>The step function.</em>  This is where most of the logic sits&#8230; there are a lot of bits, but it does what you&#8217;d expect!  Every second, a ball is thrown, and the hand switches sides.  We update the balls to add to their time in the air, and keep only the ones that haven&#8217;t landed.  When a ball is thrown, we add it.  And each time we drop a number from the pattern.</li>
<li><em>The draw function.</em>  This draws the juggler and all of the balls.  The balls are drawn with some faux-physicsy thing that, frankly, is just the result of fiddling and isn&#8217;t an accurate physics simulation.  I can get away with that when teaching middle schoolers!  Hey, at least they follow parabolas; just not all with the same acceleration.  In class I just skimmed over the math for where the ball appears, since the class right now is about the state type, and drawing is something they&#8217;ve been doing all year anyway.</li>
</ol>
<p>Definitely a lot of fun.  I encourage you to copy and paste the code above into <a href="http://dac4.designacourse.com:8000/sim">http://dac4.designacourse.com:8000/sim</a> and fiddle with the patterns and see what they look like!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/495/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=495&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2012/02/14/juggling-in-haskell-and-gloss/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell For Kids: Week 8</title>
		<link>http://cdsmith.wordpress.com/2011/10/06/haskell-for-kids-week-8/</link>
		<comments>http://cdsmith.wordpress.com/2011/10/06/haskell-for-kids-week-8/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 20:17:45 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=464</guid>
		<description><![CDATA[Welcome to the Week 8 summary of my Haskell class for kids.  If you&#8217;re just joining us, feel free to browse through weeks 1, 2, 3, 4, 5, 6, and 7. A Brief Review Just to set the stage: we spent quite a few weeks on pictures, and just last week we started on animations. [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=464&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Welcome to the Week 8 summary of my Haskell class for kids.  If you&#8217;re just joining us, feel free to browse through weeks <a href="http://cdsmith.wordpress.com/2011/08/16/haskell-for-kids-week-1/">1</a>, <a href="http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-week-2/">2</a>, <a href="http://cdsmith.wordpress.com/2011/09/03/haskell-for-kids-week-3/">3</a>, <a href="http://cdsmith.wordpress.com/2011/09/10/haskell-for-kids-week-4/">4</a>, <a href="http://cdsmith.wordpress.com/2011/09/14/haskell-for-kids-week-5/">5</a>, <a href="http://cdsmith.wordpress.com/2011/09/27/haskell-for-kids-week-6/">6</a>, and <a href="http://cdsmith.wordpress.com/2011/09/28/haskell-for-kids-week-7/">7</a>.</p>
<h1>A Brief Review</h1>
<p>Just to set the stage: we spent quite a few weeks on pictures, and just last week we started on animations.  We learned:</p>
<ul>
<li>Whereas pictures were just <em>variables</em>, animations are <em>functions</em>.</li>
<li>They have a single <em>parameter</em>, which is how many seconds since the program started running.</li>
<li>The <em>result</em> of the function should be a picture.</li>
</ul>
<p>To define a function, we give the parameter a name on the left side of the equal sign, right after the function name.  We can then use that name after the equal sign, just like if it were a number.  Here&#8217;s an example:</p>
<pre style="padding-left:30px;">animation t = rotate (60*t) (rectangleSolid 40 200)</pre>
<p>The function is called <em>animation</em>.  The parameter is called <em>t</em>.  And after the equal sign, we can use <em>t</em> just like any other number.  For example, we can multiply it by 60, and then rotate the picture by that many degrees.</p>
<p>The result of functions comes from <em>substitution</em>.  So to find out what this animation will look like after 5 seconds, the computer just looks at everything after the equal sign, and replaces any <em>t</em> that it sees with a 5.  So it takes the following steps:</p>
<ol>
<li><em>animation 5</em> &#8211; this is what the computer starts out trying to figure out.</li>
<li><em>rotate (60*5) (rectangleSolid 40 200)</em> &#8211; It replaces any <em>t</em> by 5 after the equal sign.</li>
<li><em>rotate 300 (rectangleSolid 40 200)</em> &#8211; This is just math: the computer calculated 60 times 5, and got 300.</li>
</ol>
<p>So after 5 seconds, the computer will draw the rectangle rotated by 300 degrees.  If you try this out with a few numbers, you&#8217;ll notice that the rectangle turns by more and more degrees over time, making it spin.</p>
<h3>So What&#8217;s the Problem?</h3>
<p>We played around with animations that change lots of things: by <em>t</em> in different places, you can make things spin (using <em>rotate</em>), move (using <em>translate</em>), or get bigger or smaller (using <em>scale</em>, or by using <em>t</em> as the parameter to Gloss functions like <em>circle</em>).  But you might have noticed something disappointing about doing anything except rotation: after a while, the numbers get too big to see!  Things move off the edge of the screen, or get so big you can&#8217;t see them any more.</p>
<p>Because of this, our programs so far haven&#8217;t really done anything except rotate.  To do much more without everything ending up too big or too far off the screen, we&#8217;re going to have to get comfortable with a different kind of function&#8230;</p>
<h1>Functions That Give Back Numbers</h1>
<p>Animations are functions that get a number, and turn it into a picture.  But that&#8217;s not the only kind of function!  We&#8217;ve actually seen quite a few kinds of functions, if you count the ones that Gloss defines for us:</p>
<ul>
<li><em>circle</em> is a function that turns a number into a picture (just like animations, but the number means something different)</li>
<li><em>light</em> is a function that turns a color into a different color.</li>
<li><em>rotate</em> is a function that turns a number <strong>and</strong> a picture into different picture (one that&#8217;s rotated).</li>
<li><em>polygon</em> is a function that turns a <strong>list</strong> of points into a picture.</li>
</ul>
<p>In general, you can think of a function as a machine that turns things into other things:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/10/function.png"><img class="aligncenter size-full wp-image-465" title="function" src="http://cdsmith.files.wordpress.com/2011/10/function.png?w=595" alt=""   /></a>You feed the parameters in, and you get the result out the other side.  Different functions need different types and different numbers of parameters, and they give you different types of results, too.  So when you want to use a function, the first things you should ask are: (1) How many parameters does it need? (2) What types of things does it want for its parameters?  And, (3) What type of thing is it going to give me back when it&#8217;s done?</p>
<p>In the end, you want to create pictures, of course!  So at first you might think that you&#8217;re only interested in functions that give you pictures back.  But that would be short-sighted!  Remember the functions <em>light</em> and <em>dark</em>?  They are functions that expect one parameter &#8211; a color &#8211; and give you back another color, <em>not</em> a picture.  But they&#8217;ve still been very useful for building your pictures, haven&#8217;t they?</p>
<p>The same thing is true with numbers.  Even though you ultimately want to end up with a picture, it&#8217;s very useful to be familiar with some functions that work with numbers, because we <em>use</em> numbers all the time when we&#8217;re making pictures.  So we&#8217;ll spend a lot of this week talking about functions that work with numbers.</p>
<h2>How to Draw a Function on Numbers</h2>
<p>When you have a function that expects a number for its only parameter, and gives you back a number as a result, we have a convenient way to draw it.  It might look weird at first, but believe me, it makes things a lot easier!  So here&#8217;s how to draw a function.  It only works if the function is from numbers to other numbers.</p>
<p>We start out by drawing two lines: one horizontal, and one vertical, like this:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/10/graph.png"><img class="aligncenter size-full wp-image-466" title="graph" src="http://cdsmith.files.wordpress.com/2011/10/graph.png?w=595" alt=""   /></a></p>
<p>I&#8217;ve called the vertical line <em>f</em> and the horizontal line <em>t</em>.  That&#8217;s because normally, the horizontal line will represent the amount of time the program has been running, and we&#8217;ve been calling that <em>t</em>.  The <em>f</em> is just short for &#8220;function&#8221;.</p>
<p>Next, you want to label the tick marks on the lines: for <em>t</em>, just number them 1, 2, 3, 4, and so on.  Zero is where the two lines cross, so the first tick mark after that should be 1.  For the vertical line, how you label it depends on the numbers you&#8217;re working with!  Notice it&#8217;s got both positive and negative numbers, too.</p>
<p>The next step is to draw some points.  Ask yourself, if you give your function is given the number 0, what number does it give back?  Now draw a point <em>directly</em> above or below the point on the horizontal line that means zero (remember, that&#8217;s the point where the lines cross).  How far up or down you draw the line is decided by what the function gives you back.  Now do the same thing for 1, and for 2, and so on&#8230;</p>
<p>Finally, once you&#8217;ve got a bunch of points, connect them with a smooth line.  That line is how you draw that function.</p>
<h3>Example</h3>
<p>Let&#8217;s try an example.  We&#8217;re going to draw the function</p>
<pre style="padding-left:30px;">f t = t/2 - 2</pre>
<p>That means the function is called <em>f</em>, its parameter (the number you give it) is called <em>t</em>, and the number it gives you back is half of <em>t</em>, minus 2.</p>
<p>Let&#8217;s draw the lines like we did before, and label the points counting by one in both ways:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/10/graph1.png"><img class="aligncenter size-full wp-image-467" title="graph1" src="http://cdsmith.files.wordpress.com/2011/10/graph1.png?w=595" alt=""   /></a>The next step is to figure out some values of the function, and draw them as points on the graph:</p>
<pre style="padding-left:30px;"><strong>f t = t/2 - 2</strong></pre>
<pre style="padding-left:30px;">f  0 =  0/2 - 2 = 0.0 - 2 = -2.0
f  1 =  1/2 - 2 = 0.5 - 2 = -1.5
f  2 =  2/2 - 2 = 1.0 - 2 = -1.0
f  3 =  3/2 - 2 = 1.5 - 2 = -0.5
f  4 =  4/2 - 2 = 2.0 - 2 =  0.0
f  5 =  5/2 - 2 = 2.5 - 2 =  0.5
f  6 =  6/2 - 2 = 3.0 - 2 =  1.0
f  7 =  7/2 - 2 = 3.5 - 2 =  1.5
f  8 =  8/2 - 2 = 4.0 - 2 =  2.0
f  9 =  9/2 - 2 = 4.5 - 2 =  2.5
f 10 = 10/2 - 2 = 5.0 - 2 =  3.0
f 11 = 11/2 - 2 = 5.5 - 2 =  3.5
f 12 = 12/2 - 2 = 6.0 - 2 =  4.0
f 13 = 13/2 - 2 = 6.5 - 2 =  4.5</pre>
<p>Here&#8217;s the graph with the line that connects those points:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/10/graph2.png"><img class="aligncenter size-full wp-image-468" title="graph2" src="http://cdsmith.files.wordpress.com/2011/10/graph2.png?w=595" alt=""   /></a></p>
<h3>How to Read the Drawing</h3>
<p>Looking at the graph, you can get a good feeling for what that function does.  Imagine you&#8217;re always moving to bigger and bigger numbers of seconds since the program started (because you are!).  Then this function will keep getting bigger and bigger forever.  You can also see that it will keep getting bigger at the same speed.  And you can see that it starts out being -2, and then after 4 seconds it&#8217;s 0, and it keeps going up from there.</p>
<p>So what does that mean?</p>
<ul>
<li>If you say <em>rotate (t/2-2)</em>, that means start out rotated a little bit clockwise, and then turn counter-clockwise at a constant speed forever.</li>
<li>If you say <em>translate (t/2-2) 0</em>, that means start out a little to the left, then move right at a constant speed forever.</li>
<li>and so on&#8230;</li>
</ul>
<h2>Practice Drawing Functions on Numbers</h2>
<p>In class on Tuesday, we did this worksheet together, where we practiced drawing functions that use numbers.</p>
<p style="padding-left:30px;"><a href="http://cdsmith.files.wordpress.com/2011/10/graphs_worksheet.pdf">graphs_worksheet</a></p>
<h2>Some Useful Functions on Numbers</h2>
<p>You might notice at the end of the worksheet a few functions that we haven&#8217;t used yet that are useful:</p>
<ul>
<li><em>min</em> and <em>max</em>.  These functions actually take two numbers as parameters, and they return one of the two numbers you give them.  So <em>min</em> gives you the smaller of the two numbers, and <em>max</em> gives you the larger of the two numbers.</li>
<li><em>sin</em>.  This function, which is pronounced like &#8220;sign&#8221;, is useful for making things move back and forth.  It is way more interesting than just that, but we&#8217;ll save the more interesting stuff for high school trigonometry classes.  All we want it for is to make things move back and forth.  Keep in mind that <em>sin</em> itself only goes back and forth between -1 and 1, so you probably want to multiple the answer by something bigger!</li>
</ul>
<p>Want to see what the sin function looks like?  Here you go:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/10/sine.png"><img class="aligncenter size-full wp-image-471" title="sine" src="http://cdsmith.files.wordpress.com/2011/10/sine.png?w=595" alt=""   /></a></p>
<p>See how graphs of functions help you understand what they do?</p>
<h1>Your Task</h1>
<p>Because the Little School is off next week for a inter-term break, it will be two weeks until my next summary!  Never fear, though, I&#8217;ve got something for you to work on.</p>
<p>Your task is to make an animation of an amusement park ride.  Try to get several kinds of movement in there: rotating in circles, moving back and forth, and anything else you can do!  Those of us in my class are going to make these:</p>
<ul>
<li>Skyler: Roller coaster</li>
<li>Grant: Bungee Jump Ride</li>
<li>Marcello: Ferris Wheel</li>
<li>Sophia: Tilt-a-Whirl</li>
<li>Sue: Swinging Pirate Ship</li>
<li>Me: Kids Jumping Rope (the example from week 7, but I might tweak it a little)</li>
</ul>
<p>We&#8217;re planning to put them together into a gigantic amusement park picture when we&#8217;re done.  If you have friends doing this at the same time, you can, too!  (Do you know how?  It&#8217;s just like top-down design: get everyone to send you their programs, but call them something other than <em>animation</em>, then scale them, move them to different places, and combine them using <em>pictures</em>.)</p>
<p>Good luck!  And as always, use the comments to ask for help if you&#8217;re stuck.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/464/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/464/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=464&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2011/10/06/haskell-for-kids-week-8/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/10/function.png" medium="image">
			<media:title type="html">function</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/10/graph.png" medium="image">
			<media:title type="html">graph</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/10/graph1.png" medium="image">
			<media:title type="html">graph1</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/10/graph2.png" medium="image">
			<media:title type="html">graph2</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/10/sine.png" medium="image">
			<media:title type="html">sine</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell for Kids: Week 7</title>
		<link>http://cdsmith.wordpress.com/2011/09/28/haskell-for-kids-week-7/</link>
		<comments>http://cdsmith.wordpress.com/2011/09/28/haskell-for-kids-week-7/#comments</comments>
		<pubDate>Thu, 29 Sep 2011 05:18:18 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=455</guid>
		<description><![CDATA[Where We Are This is the summary of week 7 of my Haskell class for children, aimed at ages 11 through 13 or so.  You can go back and review the previous weeks to catch up. Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 We&#8217;ve now spent six weeks learning some [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=455&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h1>Where We Are</h1>
<p>This is the summary of week 7 of my Haskell class for children, aimed at ages 11 through 13 or so.  You can go back and review the previous weeks to catch up.</p>
<ul>
<li><a href="http://cdsmith.wordpress.com/2011/08/16/haskell-for-kids-week-1/">Week 1</a></li>
<li><a href="http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-week-2/">Week 2</a></li>
<li><a href="http://cdsmith.wordpress.com/2011/09/03/haskell-for-kids-week-3/">Week 3</a></li>
<li><a href="http://cdsmith.wordpress.com/2011/09/10/haskell-for-kids-week-4/">Week 4</a></li>
<li><a href="http://cdsmith.wordpress.com/2011/09/14/haskell-for-kids-week-5/">Week 5</a></li>
<li><a href="http://cdsmith.wordpress.com/2011/09/27/haskell-for-kids-week-6/">Week 6</a></li>
</ul>
<p>We&#8217;ve now spent six weeks learning some of the Haskell programming language, and making various pictures, from simple stuff to some pretty complicated drawings.  Now it&#8217;s time for the next big step: animations!</p>
<h1>Using gloss-web for Animations</h1>
<p>We&#8217;ve done all of our programming using the gloss-web web site at <a href="http://dac4.designacourse.com:8000">http://dac4.designacourse.com:8000</a>, where you can program and look at the result in a web browser.  So far, everything we&#8217;ve done has been in the &#8220;Picture&#8221; portion of the web site, which you get to by clicking the picture button:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture.png"><img class="aligncenter size-full wp-image-456" title="picture" src="http://cdsmith.files.wordpress.com/2011/09/picture.png?w=595" alt=""   /></a></p>
<p>Now we&#8217;re ready to move on to the next type of program.  So to get started, click the &#8220;Animate&#8221; button instead.</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/animate.png"><img class="aligncenter size-full wp-image-457" title="animate" src="http://cdsmith.files.wordpress.com/2011/09/animate.png?w=595" alt=""   /></a></p>
<p>This page will look very nearly the same, but don&#8217;t be fooled!  The web site now expects from you a different kind of program: one that describes a picture that moves over time.</p>
<p>You&#8217;ll remember that the picture mode expected you write a definition for a variable called <em>picture</em>.  Everything else in your program was just there to help you define what <em>picture</em> meant, and when you finished, the program would run by looking up the definition of <em>picture</em>, and drawing it.  Your other definitions were useful only because they let you use those new words to define <em>picture</em> more easily.</p>
<p>The same thing is going on here, but there are two changes:</p>
<ol>
<li>Instead of <em>picture</em>, you&#8217;re defining a word called <em>animation</em>.</li>
<li>Instead of being a description of a picture, <em>animation</em> is a <strong>function</strong>.  Like all functions, it has a parameter, which is the amount of time that&#8217;s passed.</li>
</ol>
<p><em>Warning:</em> I&#8217;ve said from the beginning not to use Internet Explorer for the programming web site.  It&#8217;s probably worked so far if you didn&#8217;t listen to me.  But now it will <em>not</em> work any more, so you&#8217;ll need to use something else: Firefox, Chrome, Safari, Opera&#8230; take your pick, but Internet Explorer won&#8217;t work.</p>
<p>Let&#8217;s jump in with some examples.</p>
<h2>Example 1: Rotating a square</h2>
<p>The first example program we&#8217;ll write is the one that is given to you by the web site:</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">animation t = rotate (60 * t) (rectangleSolid 100 100)</pre>
<p>Here&#8217;s how to think about what that means.Think of <em>t</em> as standing for the number of seconds it&#8217;s been since you clicked the <strong>Run</strong> button.</p>
<p>At the very beginning of your animation, it&#8217;s been 0 seconds since you pressed <strong>Run</strong>.  So the program is saying the same thing as: <em>rotate (60*0) (rectangleSolid 100 100)</em>.  Remember that <em>*</em> means multiplication.  Of course, 60 times 0 is 0, so this is just <em>rotate 0 (rectangleSolid 100 100)</em>.  It draws a rectangle that hasn&#8217;t been rotated at all.</p>
<p>But then some time passes.  After half of a second, now <em>t</em> is 0.5.  Now the picture your program is drawing is <em>rotate (60*0.5) (rectangleSolid 100 100)</em>.  What is 60 times one half?  It&#8217;s 30.  So now the picture is <em>rotate 30 (rectangleSolid 100 100)</em>, and it will draw a rectangle rotated by 30 degrees.  This will continue, too.  After a full second, <em>t</em> is 1, and 60 times 1 is 60, so the rectangle will be rotated by 60 degrees.  After 2 seconds, it will be rotated by 60 times 2, or 120 degrees.  As <em>t</em> gets bigger, the rectangle will be rotated more.</p>
<p>Okay, here&#8217;s a little quiz: how long will it take for the rectangle to turn all the way around, a full 360 degrees?  That shouldn&#8217;t be too hard: you want to find the number that will give 360 when it&#8217;s multiplied by 60.  That&#8217;s how many <em>seconds</em> it will take.</p>
<p>What about after that?  Can you rotate something <em>more</em> than 360 degrees?  Sure, you can&#8230; but you can&#8217;t tell that you did it!  Rotating 360 degrees looks just like leaving it alone.  So, for example, when it&#8217;s rotated 390 degrees, that&#8217;s the same as just rotating 30.  (If you were thinking about this, you might have noticed that you actually can&#8217;t even tell if a square has been rotated 90 degrees!  That&#8217;s because of the symmetry of the square.  But you can&#8217;t tell if <em>any</em> shape has been rotated 360 degrees, no matter if it has symmetry or not.)</p>
<p>Try this example on the web site, and make sure it looks the way you expected.</p>
<h2>Example 2: Changing the speed</h2>
<p>Let&#8217;s make a small modification to the example earlier.  We&#8217;ll make the square rotate faster:</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">animation t = rotate (100 * t) (rectangleWire 100 100)</pre>
<p>Just like before, <em>t</em> stands for the number of seconds since you clicked Run.  At the very beginning, <em>t</em> is zero, and zero times 100 is still zero.  So it starts in the same place.  But now look what happens: after half a second, it&#8217;s rotated by 50 degrees.  After a full second, it&#8217;s rotated by 100 degrees.  It will only take about three and a half seconds to make a full turn.</p>
<h2>Example 3: Starting at a different angle</h2>
<p>Now let&#8217;s try to change the angle it starts at, so that it started standing on one point like a diamond.</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">animation t = rotate (100 * t + 45) (rectangleWire 100 100)</pre>
<p>Okay, what&#8217;s that when <em>t</em> is zero?  It&#8217;s <em>rotate 45 (rectangleWire 100 100)</em>.  So the square will start out turned on one point.  It will still rotate at the same speed as before, though: 100 degress every second.  Try that and see what it looks like.</p>
<h2>Example 4: Moving</h2>
<p>Now let&#8217;s try moving something instead of rotating it.</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">animation t = translate (50 * t) 0 (circle 25)</pre>
<p>Take a moment and try to guess what that will look like.</p>
<p>To figure it out, let&#8217;s look at the picture we&#8217;ll get at different times.  When the animation first starts, it&#8217;s been 0 seconds, and <em>t</em> is 0.  Then this is <em>translate 0 0 (circle 25)</em>.  That won&#8217;t move the circle at all, so it will still be in the middle of the box.  But now when <em>t</em> is 1 (after the animation has been going for a full second), it will become <em>translate 50 0 (circle 25)</em>, so the circle will be a bit off to the right.  In fact, the circle keeps moving right until it runs right off the screen (and even afterward, but then you can&#8217;t see it any more).</p>
<p>Try it!</p>
<h2>Example 5: Moving faster</h2>
<p>Can you guess how to make the circle move faster?  If you guessed that we want to multiply <em>t</em> by a higher number, you&#8217;re right!</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">animation t = translate (100 * t) 0 (circle 25)</pre>
<p>The circle will still start in the middle. (Why?)  But now after one second, it will have moved twice as far.  It&#8217;s moving faster.</p>
<p>We won&#8217;t do it as an example, but what if you wanted the circle to start on the left side of the screen?  Hint: it works a lot like example 3!</p>
<h2>Example 6: Changing the size</h2>
<p>What if you want something to get larger as time goes on?  You can do that, too.  Here&#8217;s an example:</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">animation t = circle (20 * t)</pre>
<p>Once again, <em>t</em> stands for the number of seconds since you pressed Run.  At the very beginning, your picture will be <em>circle (20 * 0)</em>, which is the same thing as c<em>ircle 0</em>.  A circle with a radius of 0 is too small to see, so you won&#8217;t see anything.  But after half a second, it becomes <em>circle (20 * 0.5)</em>, which is <em>circle 10</em>, and you will see a circle with a radius of 10.  The circle will keep getting larger: after ten seconds, it will be <em>circle 200</em>.  Eventually, if you&#8217;re patient enough, it will grow too big to even show up on the screen.</p>
<p>Try that, and see if it does what you expected.</p>
<p>(You could have also written that using scale: <em>animation t = scale t t (circle 20)</em>.  Do you see why those two programs should do the same thing?)</p>
<h2>Example 7: Rotating around another point</h2>
<p>Sometimes we might want something to rotate, but around the middle of the whole picture (or part of a picture), and not just around its own middle.  The trick is to move it (with <em>translate</em>), and then rotate the picture that you get as a result of that.</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">animation t = rotate (60 * t) (translate 100 0 (circleSolid 25))</pre>
<p>See if you can work out what that will look like at different times, and then try it out and check yourself.</p>
<h2>Animations and List Comprehensions and Functions</h2>
<p>You might have noticed that a lot of the things we&#8217;re doing here seem very similar to what we did with list comprehensions.  You&#8217;d be right!</p>
<p>Both times, we had <strong>variables</strong>, and we got different pictures because those variables got different values.  The difference is that with list comprehensions, we put all of those different pictures together at the same time.  Now we&#8217;re using different pictures at different times.  But the idea is the same: you use variables to represent numbers that you don&#8217;t know yet, and then you can build versions of your picture where those variables mean something different.</p>
<p>We&#8217;ve actually used variables like that three times:</p>
<ul>
<li>When we defined <strong>functions</strong> (like <em>awesome</em>), the parameters got names, and you used those names in the function.</li>
<li>In list comprehensions, you used the funny backward arrow: &lt;-, to pick a name and arrange for it to get lots of different values.</li>
<li>Now, in animations, you&#8217;re again defining a function: a special one, where the parameter is the time.</li>
</ul>
<p>This is something that will keep happening.  Variables are very important, so you should get used to seeing expressions that have variables in them, and doing something called &#8220;substitution&#8221;.  That just means answering the question &#8220;If <em>t</em> is 5, what does this work out to?&#8221;</p>
<h1>Top Down Design With Animations</h1>
<p>With pictures, we worked on organizing our program by top down design: we started by saying what <em>picture</em> meant, but we sometimes used other words the computer doesn&#8217;t know yet.  Then we went back in and filled in what those words meant.  We just kept this up until the program was done.  The nice thing about top-down design was this: you didn&#8217;t have to think about your <em>entire</em> program at once.  You could just think about one piece at a time.</p>
<p>We&#8217;d like to keep doing top-down design with animations, too.  There&#8217;s one thing we have to figure out, though, and that&#8217;s what to do with <em>t</em>.  Here&#8217;s what we&#8217;ll do:</p>
<ul>
<li>When we&#8217;re defining a word, if its a piece of the picture that, just looking at that one piece, will have some movement, then we&#8217;ll make it a function, taking a parameter called <em>t</em>.</li>
<li>When we&#8217;re using it later on, we&#8217;ll pass <em>t</em> for that parameter.</li>
</ul>
<p>It&#8217;s important to keep these consistent: if you define a function with a parameter, then you also have to give it that parameter when you <em>use</em> the function.  If you define a variable without a parameter, then you aren&#8217;t allowed to give it a parameter when you use it.</p>
<p>Let&#8217;s look at an example: here&#8217;s a program to draw a clock:</p>
<pre style="padding-left:30px;">import Graphics.Gloss

animation t = clock t

clock t = pictures [
    backPlate,
    minuteHand t,
    secondHand t
    ]

backPlate = color (dark white) (circleSolid 250)

minuteHand t = rotate (-0.1 * t) (line [(0,0), (0,250)])

secondHand t = rotate (-6 * t) (line [(0,0), (0,250)])</pre>
<p>We&#8217;ll break it down piece by piece:</p>
<ol>
<li>First, we defined the <em>animation</em> function.  When you&#8217;re working in animation mode, you always have to define this, and it always has the parameter <em>t</em>, that means how many seconds since it started.  We say that the animation is a clock.  The clock will have moving parts, so we pass along <em>t</em>.</li>
<li>Since we gave the time, <em>t</em>, to <em>clock</em> earlier as a parameter, our definition of <em>clock</em> has to say it has a parameter, too.  A clock consists of three parts: a back plate, a minute hand, and a second hand.  The back plate does <strong>not</strong> move, so we don&#8217;t pass along <em>t</em>.  But the minute hand and second hand do move, so we&#8217;ll be passing the time along to those.</li>
<li>Next we define <em>backPlate</em>.  Remember we didn&#8217;t give it a <em>t</em> parameter because it won&#8217;t be moving, so this is just a variable.</li>
<li>Now we define <em>minuteHand</em>.  We did give it a parameter, so now we have to say it gets a parameter.  And just like in the earlier examples, we can use that parameter <em>t</em> to make it rotate.  We want the minute hand to rotate 360 degrees every <em>hour</em>, and an hour is 3600 seconds.  So that means we want it to move only a tenth of a degree every second.  Also, we&#8217;d like it to move clockwise, so that number has to be negative.  (Remember, positive rotations are counter-clockwise.)</li>
<li>Finally, we define <em>secondHand</em>.  It needs a parameter, too, so we give it the <em>t</em> parameter.  The definition looks the same, but now we want it to rotate 360 degrees every minute (60 seconds).  That works out to 6 degrees every second, and it&#8217;s still negative so that the rotation is clockwise.</li>
</ol>
<p>Hopefully, that doesn&#8217;t look too bad!  It&#8217;s just the same kind of top-down design we&#8217;ve been doing, except with some time thrown in to keep things moving.  You have to ask yourself whether each part has movement in it or not, and you have to consistent about the answer.</p>
<h2>This week&#8217;s assignment</h2>
<p>The assignment we&#8217;re working on this week is to make a working model of the solar system.  That is, put the sun in the middle, and the planets around it, and have them rotate around the sun.  A few notes:</p>
<ul>
<li>It doesn&#8217;t need to be to scale.  In fact, the planets are <em>so</em> far apart, and <em>so</em> small compared to the distance between them, that if you try to draw everything to scale, you won&#8217;t even be able to see it!  So just pick what looks good.</li>
<li>If you are ambitious, try to add at least one moon, and give Saturn its rings.The hint here is that moons and rings aren&#8217;t too hard, as long as you&#8217;re using top-down design.  For example, you should have something called <em>saturn</em>, and instead of making it a circle, make it a pair of circles with one of them stretched a bit.
<p>Now suppose you&#8217;re adding a moon to earth, you probably want a function called something like <em>earthSystem</em> that represents the earth and its moon together.  Since the moon is moving around the earth, it should be a <strong>function</strong> and have a parameter called <em>t</em>.  When you&#8217;re defining <em>earthSystem</em>, you&#8217;ll probably want even more variables called <em>earth</em> and <em>moon</em>.  These will probably just be colored circles.</li>
<li>Our class here in Colorado Springs actually all decided to make up their own solar system with different planet names.  That&#8217;s fine, too.  In fact, the computer doesn&#8217;t care what you name things.</li>
</ul>
<p>Good luck!  And feel free to ask in the comments for help.</p>
<h1>Just for fun&#8230;</h1>
<p>If you&#8217;re bored, try this animation.  It&#8217;s a preview of what&#8217;s coming in the next couple weeks.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

animation t = pictures [
    jumprope t,
    translate (-210) 0 stickFigure,
    translate ( 210) 0 stickFigure,
    jumper t
    ]

jumprope t = scale 1.75 (1.1 * sin t) rope

rope = line [ (x, 100 * cos (0.015 * x)) | x &lt;- [-100, -75 .. 100 ] ]

stickFigure = pictures [
    line [ (-35, -100), (0, -25) ],
    line [ ( 35, -100), (0, -25) ],
    line [ (  0,  -25), (0,  50) ],
    line [ (-35,    0), (0,  40) ],
    line [ ( 35,    0), (0,  40) ],
    translate 0 75 (circle 25)
    ]

jumper t = translate 0 (max 0 (-50 * sin t)) stickFigure</pre>
<p>That&#8217;s all for this week.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/455/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/455/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=455&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2011/09/28/haskell-for-kids-week-7/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture.png" medium="image">
			<media:title type="html">picture</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/animate.png" medium="image">
			<media:title type="html">animate</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell For Kids: Week 6</title>
		<link>http://cdsmith.wordpress.com/2011/09/27/haskell-for-kids-week-6/</link>
		<comments>http://cdsmith.wordpress.com/2011/09/27/haskell-for-kids-week-6/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 21:12:14 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=437</guid>
		<description><![CDATA[(This is from last week.  Yes, I&#8217;m really late in posting this&#8230; sorry!  This week&#8217;s summary will be up in a couple days.) Practice Time Welcome to the week 6 summary of Haskell for Kids, my middle school level (that is, ages 12 &#8211; 13 ish) programming class using Haskell and Gloss at the Little [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=437&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>(This is from last week.  Yes, I&#8217;m really late in posting this&#8230; sorry!  This week&#8217;s summary will be up in a couple days.)</p>
<h1>Practice Time</h1>
<p>Welcome to the week 6 summary of Haskell for Kids, my middle school level (that is, ages 12 &#8211; 13 ish) programming class using Haskell and Gloss at the Little School on Vermijo.  You can go back and review weeks <a href="http://cdsmith.wordpress.com/2011/08/16/haskell-for-kids-week-1/">1</a>, <a href="http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-week-2/">2</a>, <a href="http://cdsmith.wordpress.com/2011/09/03/haskell-for-kids-week-3/">3</a>, <a href="http://cdsmith.wordpress.com/2011/09/10/haskell-for-kids-week-4/">4</a>, and <a href="http://cdsmith.wordpress.com/2011/09/14/haskell-for-kids-week-5/">5</a> if you like.</p>
<p>This week was time for practice.  Instead of introducing any new ideas, I&#8217;ll just post the practice activity we did together in class on Tuesday.  The way we practiced was very simple: I drew pictures, and we tried to write descriptions of those pictures.  Rather than waste time, let&#8217;s just jump right into it.  It might help to download (and maybe print out) the PDF file from the end of week 5 (follow the link earlier) for a reminder of how to write list comprehensions.</p>
<p>I&#8217;m not including answers, because it&#8217;s too easy to just read ahead!  If you can&#8217;t figure one of them out, feel free to ask for help in the comments.</p>
<h3>Practice 1</h3>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture1.png"><img class="aligncenter size-medium wp-image-443" title="picture1" src="http://cdsmith.files.wordpress.com/2011/09/picture1.png?w=297&#038;h=300" alt="" width="297" height="300" /></a></p>
<p><strong>Hints:</strong> None!  Hopefully this one is easy.</p>
<h3>Practice 2</h3>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture2.png"><img class="aligncenter size-medium wp-image-444" title="picture2" src="http://cdsmith.files.wordpress.com/2011/09/picture2.png?w=298&#038;h=300" alt="" width="298" height="300" /></a></p>
<p><strong>Hints:</strong> The parameter to <em>circle</em> is a number.  Use the variable from the list comprehension for that.</p>
<h3>Practice 3</h3>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture3.png"><img class="aligncenter size-medium wp-image-445" title="picture3" src="http://cdsmith.files.wordpress.com/2011/09/picture3.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p><strong>Hints:</strong> This one should also be easy.  It&#8217;s just a stepping stone to the next practice.</p>
<h3>Practice 4</h3>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture4.png"><img class="aligncenter size-medium wp-image-446" title="picture4" src="http://cdsmith.files.wordpress.com/2011/09/picture4.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p><strong>Hint:</strong> The list comprehension variable tells you how far to translate.</p>
<h3>Practice 5</h3>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture5.png"><img class="aligncenter size-medium wp-image-447" title="picture5" src="http://cdsmith.files.wordpress.com/2011/09/picture5.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p><strong>Hint:</strong> Don&#8217;t do this with <em>rotate</em>.  You can change just one thing from practice 4 and make this work.  Remember that you can use the list comprehension variable more than once.</p>
<h3>Practice 6</h3>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture6.png"><img class="aligncenter size-medium wp-image-448" title="picture6" src="http://cdsmith.files.wordpress.com/2011/09/picture6.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p><strong>Hint:</strong> Remember that if you translate something first and then rotate it, it will be rotated around the middle of the picture.</p>
<h3>Practice 7</h3>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture7.png"><img class="aligncenter size-medium wp-image-449" title="picture7" src="http://cdsmith.files.wordpress.com/2011/09/picture7.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p><strong>Hint:</strong> Here you&#8217;ll want to use two variables in your list comprehension.  One of the examples from the reference page shows you how to do this.</p>
<h3>Practice 8</h3>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/picture8.png"><img class="aligncenter size-medium wp-image-450" title="picture8" src="http://cdsmith.files.wordpress.com/2011/09/picture8.png?w=298&#038;h=300" alt="" width="298" height="300" /></a></p>
<p><strong>Hint:</strong> Again you want two variables, but this time one of them will be used in <em>translate</em> and the other one in <em>rotate</em>.</p>
<h1>Projects</h1>
<p>In addition to this, we just worked on our list comprehension projects.  If you remember from last week, the project was to pick something repetitive and draw it.  I suggested a U.S. flag if you&#8217;re in the U.S. and don&#8217;t have another idea.  Some of the other projects kids from our class came up with were an iPhone with a grid of icons, a keyboard (the musical type!), and a galaxy with spiral arms and a background of stars.  So be creative!</p>
<p>Oh, and I have to show off Sophia&#8217;s keyboard, because she worked really, really hard on it, and I&#8217;m very impressed.  It&#8217;s a great use of list comprehensions, too.  Here goes&#8230;</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/keyboard.png"><img class="aligncenter size-full wp-image-452" title="keyboard" src="http://cdsmith.files.wordpress.com/2011/09/keyboard.png?w=595" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/437/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/437/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=437&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2011/09/27/haskell-for-kids-week-6/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture1.png?w=297" medium="image">
			<media:title type="html">picture1</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture2.png?w=298" medium="image">
			<media:title type="html">picture2</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture3.png?w=300" medium="image">
			<media:title type="html">picture3</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture4.png?w=300" medium="image">
			<media:title type="html">picture4</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture5.png?w=300" medium="image">
			<media:title type="html">picture5</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture6.png?w=300" medium="image">
			<media:title type="html">picture6</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture7.png?w=300" medium="image">
			<media:title type="html">picture7</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/picture8.png?w=298" medium="image">
			<media:title type="html">picture8</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/keyboard.png" medium="image">
			<media:title type="html">keyboard</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell for Kids: Week 5</title>
		<link>http://cdsmith.wordpress.com/2011/09/14/haskell-for-kids-week-5/</link>
		<comments>http://cdsmith.wordpress.com/2011/09/14/haskell-for-kids-week-5/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 05:02:46 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=426</guid>
		<description><![CDATA[Functions and Variables! Welcome to the week 5 summary of my Haskell class for kids.  You can go back and read the previous weeks summaries, if you like: First Week: The Basics Second Week: Organization Third Week: Playing Computer Fourth Week: Top Down Design Our theme this week will be functions and variables. Section 1: [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=426&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h1>Functions and Variables!</h1>
<p>Welcome to the week 5 summary of my Haskell class for kids.  You can go back and read the previous weeks summaries, if you like:</p>
<ol>
<li><a href="http://cdsmith.wordpress.com/2011/08/16/haskell-for-kids-week-1/">First Week: The Basics</a></li>
<li><a href="http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-week-2/">Second Week: Organization</a></li>
<li><a href="http://cdsmith.wordpress.com/2011/09/03/haskell-for-kids-week-3/">Third Week: Playing Computer</a></li>
<li><a href="http://cdsmith.wordpress.com/2011/09/10/haskell-for-kids-week-4/">Fourth Week: Top Down Design</a></li>
</ol>
<p>Our theme this week will be functions and variables.</p>
<h2>Section 1: How to Draw an Awesome Elephant</h2>
<p>We started with a simple example.  We already know how to draw a star:</p>
<pre style="padding-left:30px;">star = polygon [
    ( -75, -100),
    (   0,   87),
    (  75, -100),
    (-100,   25),
    ( 100,   25),
    ( -75, -100)
    ]</pre>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/star.png"><img class="aligncenter size-medium wp-image-427" title="star" src="http://cdsmith.files.wordpress.com/2011/09/star.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>and Sophia has figured out how to draw an elephant (this is one of her early versions, actually, but I picked it because it&#8217;s shorter; you don&#8217;t need to understand it):</p>
<pre style="padding-left:30px;">elephant = pictures [
      rotate (-20) (scale 3 2 (translate 30 40 (circleSolid 25))),
      translate 150 (-20) (rectangleSolid 40 80),
      translate (-10) 40 (scale 1.5 1 (circleSolid 80)),
      translate 50 (-50)(rectangleSolid 40 70),
      translate (-60) (-50) (rectangleSolid 40 70),
      translate (-140) 50 (rotate 100 (rectangleSolid 10 40))
      ]</pre>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/elephant.png"><img class="aligncenter size-medium wp-image-428" title="elephant" src="http://cdsmith.files.wordpress.com/2011/09/elephant.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>But now we want to draw a really awesome star.  Being &#8220;awesome&#8221;, of course, means being twice as large, and the color chartreuse.</p>
<pre style="padding-left:30px;">awesomeStar     = scale 2 2 (color chartreuse star)</pre>
<pre style="padding-left:30px;">awesomeElephant = scale 2 2 (color chartreuse elephant)</pre>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/astar.png"><img class="aligncenter size-medium wp-image-429" title="astar" src="http://cdsmith.files.wordpress.com/2011/09/astar.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>and</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/aelephant.png"><img class="aligncenter size-medium wp-image-430" title="aelephant" src="http://cdsmith.files.wordpress.com/2011/09/aelephant.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>But, by now, you can probably guess that I can draw an awesome version of <em>any</em> picture.  There are awesome spaceships, awesome shoes, awesome bicycles&#8230; give me any picture at all, and I can tell you how to draw an awesome one: you draw it twice as big, and make it chartreuse.  It will get tiresome to keep repeating ourselves, but luckily, Haskell lets us say this just once:</p>
<pre style="padding-left:30px;">awesome x = scale 2 2 (color chartreuse x)</pre>
<p>Now we don&#8217;t need to define a bunch of variables like <em>awesomeStar</em> and <em>awesomeElephant</em>.  Instead, we can just write <em>awesome star</em>, with a space in between the two!</p>
<p>We&#8217;ve seen things like this before: awesome is called a <strong>function</strong>, and the thing that we&#8217;re making awesome is called its <strong>argument</strong>.  But up until now, we&#8217;d only used functions that other people gave us&#8230; like <em>rotate</em> and <em>translate</em> in the Gloss library.  Now, we know how to write definitions for our own functions.</p>
<p>To sum up, we can write a function because:</p>
<ol>
<li>Given <em>any</em> thing, we know how to make it awesome.</li>
<li>It follows a formula: <em>scale 2 2 (color chartreuse ____________)</em> where you just fill in the blank.</li>
</ol>
<p>If both of those are true, you can write a function definition.  It looks like like the variable definitions we&#8217;ve written before, except that it has <strong>parameters</strong>.  A parameter is any word that comes before the equal sign (=), <strong>except for</strong> the first one.  In the function we wrote a minute ago, we called the parameter <em>x</em>.  Parameters are just placeholders, so when you use the function, you&#8217;ll give it an argument, that argument gets stuck in where ever that variable name pops up.</p>
<p>We also talked about what it means to be cute, and came up with the following:</p>
<pre style="padding-left:30px;">cute x = scale 0.5 0.5 (color purple x)</pre>
<p>Let&#8217;s check our assumptions: given any shape you choose to define, we can draw a cute one.  It follows a definite formula (make it half the size, and color it purple).  And so we wrote a definition where the function is called <em>cute</em>, and the parameter is called <em>x</em>.  If you wanted to draw a cute elephant, you could define <em>picture</em> like this: <em>picture = cute elephant</em>.  There, the function is <em>cute</em> and the argument is <em>elephant</em>, so to figure out what that means you just replace <em>x</em> with <em>elephant</em> every time it shows up in the function definition for <em>cute</em>!</p>
<h2>Section 2: In which elephants can move their tails&#8230;</h2>
<p>Let&#8217;s look at another function.  We know how to draw an elephant&#8230; but there&#8217;s one problem.  No matter where we draw an elephant, its tail is always sticking out at exactly the same angle.  Suppose we want to be able to draw an elephant with its tail sticking in whatever direction we like.</p>
<p>It turns out that almost all of drawing an elephant will remain the same, but there&#8217;s one <em>rotate</em> where we&#8217;ll want to change the angle.  So once again, we have:</p>
<ol>
<li>For any angle, we can draw an elephant with its tail at that angle.</li>
<li>How we draw an elephant follows the same formula each time, except for a different number in that one place.</li>
</ol>
<p>Sounds like a job for a function!</p>
<pre style="padding-left:30px;">elephant angle = pictures [
      rotate (-20) (scale 3 2 (translate 30 40 (circleSolid 25))),
      translate 150 (-20) (rectangleSolid 40 80),
      translate (-10) 40 (scale 1.5 1 (circleSolid 80)),
      translate 50 (-50)(rectangleSolid 40 70),
      translate (-60) (-50) (rectangleSolid 40 70),
      translate (-125) 50 (rotate angle (translate 0 20 (rectangleSolid 10 40)))
      ]</pre>
<p>See how that worked?  Most of the elephant stayed the same, but the rotate in the last line uses the parameter, <em>angle</em>.  So whatever number we give it decides with way the tail points.  It could be straight up&#8230;</p>
<pre style="padding-left:30px;">picture = elephant 0</pre>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/elephant0.png"><img class="aligncenter size-medium wp-image-431" title="elephant0" src="http://cdsmith.files.wordpress.com/2011/09/elephant0.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>or it could be mostly down&#8230;</p>
<pre style="padding-left:30px;">picture = elephant 160</pre>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/elephant160.png"><img class="aligncenter size-medium wp-image-432" title="elephant160" src="http://cdsmith.files.wordpress.com/2011/09/elephant160.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>So once again, we&#8217;re able to use functions: this time to say how to draw an elephant, but leave until later the question of which direction its tail should be pointing.</p>
<h2>Section 3: Lists, Lists Everywhere</h2>
<p>The interesting thing about functions was that they have <strong>parameters</strong>, which are special variables that can have different values depending on how you use the function.  There&#8217;s another way that something similar can happen, and it&#8217;s in lists.  So we&#8217;ll finish off by looking at some more kinds of lists.</p>
<p>The lists we&#8217;ve used so far have all looked something like this:</p>
<pre style="padding-left:30px;">[ 1, 2, 3, 4, 5 ]</pre>
<p>In other words, a list has always had an opening bracket, then some elements (that is, things in the list) separated by commas, and finally a closing bracket.  The things in the list haven&#8217;t been numbers&#8230; we&#8217;ve worked with lists of pictures, and lists of points!  You can have lists of whatever you want, but the form is the same: you still have the brackets and the commas.  We can call these &#8220;simple lists&#8221;, because, they are pretty easy to use.</p>
<p>A second kind of list that will come in handy, that we haven&#8217;t used before, is a range.  That looks like this:</p>
<pre style="padding-left:30px;">[ 1 .. 10 ]</pre>
<p>It always has an opening bracket, a first element, two dots in a row, and then a final element, and a closing bracket.  This kind of list counts for you.  The things inside (for now, at least) always have to be numbers so that you can count&#8230; and the computer will fill in the middle of the list for you.  In this case, the list we defined is the same as [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].  The range is just a shorthand.</p>
<p>There&#8217;s one more simple trick we can play, too: we can give the first two elements, and the computer will continue the pattern:</p>
<pre style="padding-left:30px;">[ 2, 4 .. 10 ]</pre>
<p>This look almost like a range, except that we&#8217;ve given the first <em>two</em> elements, instead of the just the first one.  The computer will notice that we skipped forward by two, and will keep doing that, so that list is the same as [2, 4, 6, 8, 10]!</p>
<p>Finally, we get to the most interesting kind of list of all: called a <strong>list comprehension</strong>.  Here&#8217;s an example:</p>
<pre style="padding-left:30px;"><strong>[</strong> x + 7 <strong>|</strong> x <strong>&lt;-</strong> [1 .. 5] <strong>]</strong></pre>
<p>This has quite a few parts, so let&#8217;s go through them carefully:</p>
<ul>
<li>It starts with an opening bracket, just like all lists.</li>
<li>Next is an expression.  Notice that the expression has a variable <em>x</em> that we haven&#8217;t mentioned yet.  That&#8217;s okay, it&#8217;s coming later.</li>
<li>In the middle, there&#8217;s a vertical bar (shift + backslash).  That separates the expression from the next part.</li>
<li>Now we have a sort of definition of <em>x</em>, except we say that <em>x</em> is <em>any</em> thing from another list!  We do that with a backwards arrow, which is typed as a less-than sign, followed by a dash.</li>
<li>Finally, there&#8217;s a closing bracket.</li>
</ul>
<p>What this means is: give me a list of all values of (<em>x</em>+7), where <em>x</em> is any of the numbers in that list.  So this is equivalent to [1+7, 2+7, 3+7, 4+7, 5+7]&#8230; and of course that is the same as [8, 9, 10, 11, 12].</p>
<p>This last kind of list is very, very interesting; but maybe right now it just looks very complicated and you can&#8217;t see why.  To see how it&#8217;s interesting, we&#8217;ll start writing some lists of pictures, instead of just numbers.</p>
<h2>Section 4: Combining List Comprehensions and Pictures</h2>
<p>The last thing we did this week is combine pictures and list comprehensions to make some interesting pictures.  I&#8217;ll give you a bunch of examples, but not the pictures they draw.  Your task is to <em>first</em> try to guess what the picture will look like, and <em>then</em> try them out using the <a href="http://dac4.designacourse.com:8000/draw">gloss programming web site</a> and check your guesses.</p>
<p>I&#8217;d very strongly encourage you to take this seriously&#8230; use graph paper, work it out, and do your best to figure out what is going to be drawn <em>before</em> you run the program and check.</p>
<p>First example:</p>
<pre style="padding-left:30px;">picture = pictures [
    circle x
        | x &lt;- [ 10, 20 .. 100 ]
    ]</pre>
<p>Second example:</p>
<pre style="padding-left:30px;">picture = pictures [
    translate x 0 (circle 20)
        | x &lt;- [ 0, 10 .. 100 ]
    ]</pre>
<p>Third example</p>
<pre style="padding-left:30px;">picture = pictures [
    translate x 0 (circle x)
        | x &lt;- [ 10, 20 .. 100 ]
    ]</pre>
<p>Fourth example:</p>
<pre style="padding-left:30px;">picture = pictures [
    rotate x (rectangleWire 300 300)
        | x &lt;- [ 0, 10 .. 90 ]
    ]</pre>
<p>Fifth example:</p>
<pre style="padding-left:30px;">picture = pictures [
    translate x 0 (rotate x (rectangleWire 300 300))
        | x &lt;- [ -45, -35 .. 45 ]
    ]</pre>
<p>We can also put several different variables on the right side of the vertical bar, separated by commas.  In that case, we&#8217;ll get something in the list for each possible <em>combination</em> of those variables!  We can use that, too&#8230;</p>
<p>Sixth example:</p>
<pre style="padding-left:30px;">picture = pictures [
    translate x y (circle 10)
        | x &lt;- [ -200, -100 .. 200 ],
          y &lt;- [ -200, -100 .. 200 ]
    ]</pre>
<p>Seventh example:</p>
<pre style="padding-left:30px;">picture = pictures [
    rotate angle (translate x 0 (circle 10))
        | x     &lt;- [50, 100 .. 200 ],
          angle &lt;- [ 0,  45 .. 360 ]
    ]</pre>
<p>Eighth example:</p>
<pre style="padding-left:30px;">picture = pictures [
    rotate angle (translate (5*x) 0 (circle x))
        | x     &lt;- [10, 20 ..  40 ],
          angle &lt;- [ 0, 45 .. 360 ]
    ]</pre>
<p>As you can see, you can use list comprehensions to make lots of really cool pictures where it would be really tedious to write out each shape the way we&#8217;ve been doing before.</p>
<h2>Your Assignment</h2>
<p>Your assignment this week is to pick a picture with a lot of repetition that you can draw with list comprehensions, and build it.  If you are in the United States and don&#8217;t have another idea, a really good one is the American flag!</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/flag.png"><img class="aligncenter size-medium wp-image-433" title="flag" src="http://cdsmith.files.wordpress.com/2011/09/flag.png?w=298&#038;h=300" alt="" width="298" height="300" /></a></p>
<p>Hints:</p>
<ul>
<li>There&#8217;s a program to draw a star at the beginning of this summary.  You can copy the star part from that if you like.</li>
<li>You&#8217;ll probably have to draw the stars in two groups: first, there&#8217;s a 5-row, 6-column bigger grid, and then there&#8217;s a 4-row, 5-column smaller grid inside of that.</li>
</ul>
<p>You don&#8217;t have to draw a flag, though!  Especially if you&#8217;re not in the United States, your flag probably doesn&#8217;t repeat as much as ours, so you&#8217;ll want to pick something different.  Just pick something you can draw by using list comprehensions to make lists of pictures.</p>
<p>Have fun!</p>
<h2>A Reference</h2>
<p>One last thing&#8230; I&#8217;ve put together a reference card about the recent things we&#8217;ve done: lines, polygons, and types of lists.  This might be a good thing to keep around.  Click below to grab a copy.</p>
<ul>
<li><a href="http://cdsmith.files.wordpress.com/2011/09/polygon-line-and-list-reference.pdf">Polygon Line and List Reference</a></li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/426/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/426/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=426&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2011/09/14/haskell-for-kids-week-5/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/star.png?w=300" medium="image">
			<media:title type="html">star</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/elephant.png?w=300" medium="image">
			<media:title type="html">elephant</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/astar.png?w=300" medium="image">
			<media:title type="html">astar</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/aelephant.png?w=300" medium="image">
			<media:title type="html">aelephant</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/elephant0.png?w=300" medium="image">
			<media:title type="html">elephant0</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/elephant160.png?w=300" medium="image">
			<media:title type="html">elephant160</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/flag.png?w=298" medium="image">
			<media:title type="html">flag</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell For Kids: Week 4</title>
		<link>http://cdsmith.wordpress.com/2011/09/10/haskell-for-kids-week-4/</link>
		<comments>http://cdsmith.wordpress.com/2011/09/10/haskell-for-kids-week-4/#comments</comments>
		<pubDate>Sun, 11 Sep 2011 03:27:21 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=411</guid>
		<description><![CDATA[Welcome back to our weekly report on the Haskell for Kids class!  We did three big things this week, so let&#8217;s just jump in! New Student! First of all, we gained a student in my class, so we all welcomed Skyler!  We&#8217;re three weeks in, but this gave us a great chance to go back [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=411&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h1><span style="font-size:13px;font-weight:normal;">Welcome back to our weekly report on the Haskell for Kids class!  We did three big things this week, so let&#8217;s just jump in!</span></h1>
<h1>New Student!</h1>
<p>First of all, we gained a student in my class, so we all welcomed Skyler!  We&#8217;re three weeks in, but this gave us a great chance to go back over some of the basic ideas we&#8217;ve already seen.  These include:</p>
<ol>
<li>The first week, we talked about what computer programming is, what a compiler is, and what a library does for you.  We also introduced the basics of programming using the Haskell programming language and the Gloss library, and we played around.  So we reviewed the basics of that.  Here&#8217;s a link to the <a href="http://cdsmith.wordpress.com/2011/08/16/haskell-for-kids-week-1/">Week 1 Summary</a>.</li>
<li>The second week, we talked about the basic structure of a program, including what an <em>import</em> statement means, how to write definitions of variables, and the idea of expressions.  We also looked at how useful it is to stay organized and make sure everything in your program is where it belongs.  Here&#8217;s a link to the <a href="http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-week-2/">Week 2 Summary</a>.</li>
<li>Finally, the third week, we talked about cartesian (x,y) coordinates and lines and polygons, and we practiced &#8220;playing computer&#8221; and predicting what the computer is going to draw when it sees programs we made up.  Here&#8217;s a link to the <a href="http://cdsmith.wordpress.com/2011/09/03/haskell-for-kids-week-3/">Week 3 Summary</a>.</li>
</ol>
<p>Skyler joining gave us a chance to go back and review that, and practice explaining it to someone new.  I think most people learned something new of their own, too.</p>
<h1>Practicing Top-Down Design</h1>
<p>Another thing we did this week was practice some of the ideas we&#8217;ve touched on briefly in the past:</p>
<ul>
<li>Top-down design</li>
<li>Organizing and naming things</li>
<li>Writing stubs</li>
</ul>
<p>We did this by working through an example in class, in great detail.  I&#8217;ll try to reproduce that here.  You can follow along, as always, at <a href="http://dac4.designacourse.com:8000/">http://dac4.designacourse.com:8000/</a>  Remember, the point of this isn&#8217;t just to skip ahead and look at the final result: it&#8217;s to follow through the steps of how we got there.  We used techniques that you can use to try to write your own programs, too!</p>
<p>It does get repetitive, though, so at each step (except the last), I&#8217;ve bolded the parts of the program that are new, so you can generally ignore anything that&#8217;s not in bold because you&#8217;ve seen it before.</p>
<h3>The Goal</h3>
<p>Our goal will be to draw a dinner plate.  Here&#8217;s a whiteboard drawing of our goal:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/dinner.jpg"><img class="aligncenter size-medium wp-image-414" title="dinner" src="http://cdsmith.files.wordpress.com/2011/09/dinner.jpg?w=300&#038;h=225" alt="" width="300" height="225" /></a></p>
<p>We&#8217;ll start out by writing a basic program to draw an empty picture.  We may not have used it before, but <em>blank</em> is defined by Gloss to be an empty picture.</p>
<pre style="padding-left:30px;"><strong>import Graphics.Gloss</strong>

<strong>picture = blank</strong></pre>
<p>If we run this program, the result looks rather boring&#8230;</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/blank.jpg"><img class="aligncenter size-medium wp-image-415" title="blank" src="http://cdsmith.files.wordpress.com/2011/09/blank.jpg?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>So far, so&#8230; boring.  But there&#8217;s something worth pointing out.  Defining picture to be <em>blank</em> was called a &#8220;stub&#8221;.  We just stuck something in to make the program complete, knowing it was obviously not enough.  That&#8217;s okay, so long as we remember the places we&#8217;ve used stubs so we can go back and fill them in later.</p>
<p>The first thing we&#8217;ll do is declare that our picture is a <em>dinner</em>.  That looks something like this.  We&#8217;ll also start adding comments (remember, those of bits of the program that are ignored by the computer) to remind ourselves where we&#8217;ve got stubs.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

<strong>picture = dinner</strong>
<strong>dinner = blank -- stub </strong></pre>
<p>This looks the same as before; we&#8217;ve just said that the <em>picture</em> is a <em>dinner</em>&#8230; and that a <em>dinner</em> is blank.  It might seem pointless, but don&#8217;t underestimate the chance to name things!  This will come in handy later.</p>
<p>Let&#8217;s fill in the stub for <em>dinner</em> with the major parts of the dinner we&#8217;d like to draw.  If you look at the plan, you can see it&#8217;s roughly divided into a plate, some food, a knife, and a fork.  I&#8217;ll throw in a table, too, which is just the background for the entire image.  Here&#8217;s our attempt to expand on the program:</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = dinner

<strong>dinner = pictures [</strong>
<strong> table,</strong>
<strong> plate,</strong>
<strong> food,</strong>
<strong> fork,</strong>
<strong> knife</strong>
<strong> ]</strong>

<strong>table = blank -- stub</strong>
<strong>plate = blank -- stub</strong>
<strong>food = blank -- stub</strong>
<strong>fork = blank -- stub</strong>
<strong>knife = blank -- stub</strong></pre>
<p>This might not look impressive when it&#8217;s done, but we&#8217;re working on understanding how to break down the picture into pieces.  Let&#8217;s pick out a few easy pieces to draw, now.  First, the table is just a 500 by 500 rectangle (in other words, the entire screen), colored a good table color, like brown.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = dinner

dinner = pictures [
    table,
    plate,
    food,
    fork,
    knife
    ]

<strong>table = color brown (rectangleSolid 500 500)</strong>
plate = blank  -- stub
food  = blank  -- stub
fork  = blank  -- stub
knife = blank  -- stub</pre>
<p>Let&#8217;s compile and run that&#8230;</p>
<pre style="padding-left:30px;">Not in scope: `brown'</pre>
<p>Oops!  We used the color <em>brown</em>, but that&#8217;s not defined by Gloss.  We can define it, though.  Brown is, generally speaking, a dark orange, so let&#8217;s say so:</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = dinner

dinner = pictures [
    table,
    plate,
    food,
    fork,
    knife
    ]

table = color brown (rectangleSolid 500 500)
plate = blank  -- stub
food  = blank  -- stub
fork  = blank  -- stub
knife = blank  -- stub

<strong>brown = dark orange</strong></pre>
<p>Now we&#8217;ll try again.</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/dinner1.jpg"><img class="aligncenter size-medium wp-image-416" title="dinner1" src="http://cdsmith.files.wordpress.com/2011/09/dinner1.jpg?w=300&#038;h=298" alt="" width="300" height="298" /></a></p>
<p>Looks good!  Time to add the plate.  We want to draw a rim on the plate, so it will consist of two circles &#8212; one on top of the other, and a little smaller.  We&#8217;ll also want two new colors: <em>gray</em> and <em>lightGray.  </em>This shouldn&#8217;t be difficult, so we&#8217;ll write it.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = dinner

dinner = pictures [
    table,
    plate,
    food,
    fork,
    knife
    ]

table = color brown (rectangleSolid 500 500)

<strong>plate = pictures [</strong>
<strong> color gray (circleSolid 175),</strong>
<strong> color lightGray (circleSolid 150)</strong>
<strong> ]</strong>

food  = blank  -- stub
fork  = blank  -- stub
knife = blank  -- stub

brown     = dark orange
<strong>lightGray = dark white</strong>
<strong>gray = dark lightGray</strong></pre>
<p>I did something a little tricky with the colors.  I could have defined <em>gray</em> to be <em>dark (dark white)</em>, but it was easier to use the other color, <em>lightGray</em>, that I&#8217;d already defined.  Either one would mean the same thing.  Can you see why?</p>
<p>We&#8217;ll now build and run this program, and check the result.</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/dinner2.jpg"><img class="aligncenter size-medium wp-image-417" title="dinner2" src="http://cdsmith.files.wordpress.com/2011/09/dinner2.jpg?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>As you can see, this is coming along fine.  Time to do something about that silverware that&#8217;s stubbed out.  What do we know about it?  Let&#8217;s make a list:</p>
<ul>
<li>We want a fork and a knife, both.</li>
<li>We want them to be in the middle vertically, but on different sides, about 200 pixels over.</li>
<li>They will be much taller than they are wide&#8230; maybe 300 pixels tall, but only 50 wide.</li>
</ul>
<p>We&#8217;ll stop there, rather than trying to do everything at once.  Remember, the idea is to work in small steps, filling in a few details at a time.  So for now, we&#8217;ll ignore the unique shapes of forks and knives, and just make them rectangles.  Because where we put the fork and knife isn&#8217;t really a part of what it <em>means</em> to be a fork or knife, those translations should go in <em>dinner</em>, not <em>fork</em> or <em>knife</em>.  The result looks like this.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = dinner

dinner = pictures [
    table,
    plate,
    food,
    <strong>translate ( 200) 0 fork,</strong>
<strong> translate (-200) 0 knife</strong>
    ]

table = color brown (rectangleSolid 500 500)

plate = pictures [
    color gray      (circleSolid 175),
    color lightGray (circleSolid 150)
    ]

food  = blank                 -- stub
<strong>fork = rectangleWire 50 300 -- stub</strong>
<strong>knife = rectangleWire 50 300 -- stub</strong>

brown     = dark orange
lightGray = dark white
gray      = dark lightGray</pre>
<p>Notice that I&#8217;ve stilled marked <em>fork</em> and <em>knife</em> as stubs.  They are!  That&#8217;s certainly not what they look like.  But it just helped to draw an outline of the region of the plate where they belong.  Let&#8217;s compile and run this, and see what happens.</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/dinner3.jpg"><img class="aligncenter size-medium wp-image-418" title="dinner3" src="http://cdsmith.files.wordpress.com/2011/09/dinner3.jpg?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>It&#8217;s about time we start filling in the food.  We&#8217;ll start out by breaking it down into the three main pieces, and then again stub them out with outlines rather than get into the details of what each piece looks like.  Each piece of food is translated and rotated a little.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = dinner

dinner = pictures [
    table,
    plate,
    food,
    translate ( 200) 0 fork,
    translate (-200) 0 knife
    ]

table = color brown (rectangleSolid 500 500)

plate = pictures [
    color gray      (circleSolid 175),
    color lightGray (circleSolid 150)
    ]

<strong>food = pictures [</strong>
<strong> translate (-50) ( 50) (rotate ( 45) carrot),</strong>
<strong> translate (-20) (-40) (rotate ( 20) broccoli),</strong>
<strong> translate ( 60) (-30) (rotate (-10) broccoli)</strong>
<strong> ]</strong>

<strong>carrot = rectangleWire 40 80 -- stub</strong>
<strong>broccoli = rectangleWire 80 80 -- stub</strong>
fork     = rectangleWire 50 300  -- stub
knife    = rectangleWire 50 300  -- stub

brown     = dark orange
lightGray = dark white
gray      = dark lightGray</pre>
<p>Don&#8217;t worry if you couldn&#8217;t figure out all those numbers right off!  When we wrote this in class, we had to guess and then tweak them until the rectangles ended up in about the right places.  Being able to do that before we worry about the details of what broccoli is shaped like is one of the reasons we like stubs like this!  (Also, don&#8217;t worry if you didn&#8217;t know how to spell broccoli.  I had to look it up, too.)</p>
<p>Let&#8217;s run this and see what it looks like.</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/dinner4.jpg"><img class="aligncenter size-medium wp-image-419" title="dinner4" src="http://cdsmith.files.wordpress.com/2011/09/dinner4.jpg?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>We&#8217;re now going to play our next trick.  We&#8217;d like to focus on how to draw a carrot.  But since the carrot is translated and rotated, it&#8217;s hard for us to focus on just that piece.  Remember when we wrote <em>picture</em> = <em>dinner</em> earlier?  I promised that would come in handy!  We&#8217;re going to, just for now, change that to say <em>picture</em> = <em>carrot</em>.  Our program now looks like this:</p>
<pre style="padding-left:30px;">import Graphics.Gloss

<strong>picture = carrot</strong>

dinner = pictures [
    table,
    plate,
    food,
    translate ( 200) 0 fork,
    translate (-200) 0 knife
    ]

table = color brown (rectangleSolid 500 500)

plate = pictures [
    color gray      (circleSolid 175),
    color lightGray (circleSolid 150)
    ]

food = pictures [
    translate (-50) ( 50) (rotate ( 45) carrot),
    translate (-20) (-40) (rotate ( 20) broccoli),
    translate ( 60) (-30) (rotate (-10) broccoli)
    ]

carrot   = rectangleWire 40  80  -- stub
broccoli = rectangleWire 80  80  -- stub
fork     = rectangleWire 50 300  -- stub
knife    = rectangleWire 50 300  -- stub

brown     = dark orange
lightGray = dark white
gray      = dark lightGray</pre>
<p>When we run this, we&#8217;ll have told the computer all about dinners and forks and knives and everything&#8230; but in the end, only asked it to draw a carrot.  That&#8217;s fine; the computer is happy to ignore our definitions of variables that we don&#8217;t really use!  The result looks like this.</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/dinner5.jpg"><img class="aligncenter size-medium wp-image-420" title="dinner5" src="http://cdsmith.files.wordpress.com/2011/09/dinner5.jpg?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>So we&#8217;ve gotten rid of everything except the carrot, which of course looks like just a rectangle for now.  Having escaped from the distractions of forks and broccoli, it shouldn&#8217;t be too hard to tell what a carrot is!  It&#8217;s just a polygon colored orange.  After some experimentation, you might end up with this:</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = carrot

dinner = pictures [
    table,
    plate,
    food,
    translate ( 200) 0 fork,
    translate (-200) 0 knife
    ]

table = color brown (rectangleSolid 500 500)

plate = pictures [
    color gray      (circleSolid 175),
    color lightGray (circleSolid 150)
    ]

food = pictures [
    translate (-50) ( 50) (rotate ( 45) carrot),
    translate (-20) (-40) (rotate ( 20) broccoli),
    translate ( 60) (-30) (rotate (-10) broccoli)
    ]

<strong>carrot = color orange (polygon [</strong>
<strong> (-5, -40), (-20, 40), (20, 40), (5, -40)</strong>
<strong> ])</strong>

broccoli = rectangleWire 80  80  -- stub
fork     = rectangleWire 50 300  -- stub
knife    = rectangleWire 50 300  -- stub

brown     = dark orange
lightGray = dark white
gray      = dark lightGray</pre>
<p>That looks like this:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/dinner6.jpg"><img class="aligncenter size-medium wp-image-421" title="dinner6" src="http://cdsmith.files.wordpress.com/2011/09/dinner6.jpg?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>Now, having gotten the look of a carrot under control, let&#8217;s change the definition of <em>picture</em>, so that the entire dinner will be drawn again.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

<strong>picture = dinner</strong>

dinner = pictures [
    table,
    plate,
    food,
    translate ( 200) 0 fork,
    translate (-200) 0 knife
    ]

table = color brown (rectangleSolid 500 500)

plate = pictures [
    color gray      (circleSolid 175),
    color lightGray (circleSolid 150)
    ]

food = pictures [
    translate (-50) ( 50) (rotate ( 45) carrot),
    translate (-20) (-40) (rotate ( 20) broccoli),
    translate ( 60) (-30) (rotate (-10) broccoli)
    ]

carrot = color orange (polygon [
    (-5, -40), (-20, 40), (20, 40), (5, -40)
    ])

broccoli = rectangleWire 80  80  -- stub
fork     = rectangleWire 50 300  -- stub
knife    = rectangleWire 50 300  -- stub

brown     = dark orange
lightGray = dark white
gray      = dark lightGray</pre>
<p>Hopefully, you can see how this will end up.  One by one, for each of the remaining pieces of the program, we&#8217;ll do three things:</p>
<ol>
<li>Change the definition of <em>picture</em> to focus on that piece.</li>
<li>Write a program to properly draw that piece.</li>
<li>Change the definition of <em>picture</em> again to draw the entire dinner.</li>
</ol>
<p>In the interest of space, I&#8217;m skipping the remaining steps, but here&#8217;s the program we ended up with:</p>
<pre style="padding-left:30px;">{-
    Picture of a dinner plate

    This program draws a dinner plate, with several food items and silverware.
-}

import Graphics.Gloss

picture = dinner

dinner = pictures [
    table,
    plate,
    food,

    -- Silverware:
    translate ( 200) 0 fork,
    translate (-200) 0 knife
    ]

table = color brown (rectangleSolid 500 500)

plate = pictures [
    color gray      (circleSolid 175),            -- rim
    color lightGray (circleSolid 150)             -- center
    ]

food = pictures [
    translate (-50) ( 50) (rotate   45  carrot),
    translate (-20) (-40) (rotate   20  broccoli),
    translate ( 60) (-30) (rotate (-10) broccoli)
    ]

carrot = color orange (polygon [
    (-5, -40), (-20, 40), (20, 40), (5, -40)
    ])

broccoli = color (dark green) (pictures [
    translate (  0) (-15) (rectangleSolid 30 50), -- base
    translate (-15) (  0) (circleSolid 25),       -- flower
    translate ( 15) (  0) (circleSolid 25),       -- flower
    translate (  0) ( 15) (circleSolid 25)        -- flower
    ])

fork  = color lightGray (pictures [
    rectangleSolid 10 250,                        -- handle
    translate (  0) ( 80) (rectangleSolid 40 10), -- base
    translate (-15) (100) (rectangleSolid 10 45), -- left prong
    translate ( 15) (100) (rectangleSolid 10 45)  -- right prong
    ])

knife = color lightGray (pictures [
    translate 0 (-25) (rectangleSolid 30 200),    -- handle
    polygon [ (-15,  75),
              ( -5, 105),
              ( 15, 125),
              ( 15,  75) ]                        -- blade
    ])

{-
    Some colors that are used in the picture:
-}
brown     = dark orange
lightGray = dark white
gray      = dark lightGray</pre>
<p>(We also added some comments to explain a little more and divide up the program into sections.  And here is the final picture.</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/09/dinner7.jpg"><img class="aligncenter size-medium wp-image-422" title="dinner7" src="http://cdsmith.files.wordpress.com/2011/09/dinner7.jpg?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<p>Voila!  We have finished.  The important thing to learn is how we were able to draw the whole picture by first just sketching out the major parts, and then filling in the details bit by bit.  That&#8217;s the idea of <em>top down design</em>, which is a very good way to go about writing computer programs.</p>
<h1>Oh no!  Exam Time!</h1>
<h1><span style="font-size:13px;font-weight:normal;">We&#8217;ve been working on and talking about pictures for a while, and some of us are getting antsy to move on to something else.  Before we can, though, we need to be sure we all understand the ideas we&#8217;ve learned in writing our picture programs.  </span><span style="font-size:13px;font-weight:normal;">So here it is: time to demonstrate a complete understanding of writing programs to draw pictures.  Time for our first exam!</span></h1>
<h1><span style="font-size:13px;font-weight:normal;">Here&#8217;s the &#8220;exam&#8221; I assigned to the students in my class this week.  It&#8217;s due in class next week.  Don&#8217;t worry too much about it; the point is more to go back and review before we move on&#8230; we&#8217;re not failing anyone!</span></h1>
<h3>Instructions</h3>
<p>This is what they are working on from now until next week, as a final bit of practice with pictures before we move on to animations.</p>
<ol>
<li>Pick one of the following four pictures.<a href="http://cdsmith.files.wordpress.com/2011/09/pictureexamoptions.jpg"><img class="aligncenter size-full wp-image-412" title="PictureExamOptions" src="http://cdsmith.files.wordpress.com/2011/09/pictureexamoptions.jpg?w=595&#038;h=455" alt="" width="595" height="455" /></a></li>
<li>Write a program that draws a simplification of the picture on that page.  Write the program using <strong><em>top-down design</em></strong>, in the way we talked about earlier.</li>
<li>As a rule of thumb, divide the overall picture into at least three pieces, and divide each of those into about two to four pieces each.  Part of the assignment is to pick out how to divide the picture into pieces, which details are important and which you can leave out.  <em><strong>Don&#8217;t</strong></em> try to draw everything in the picture.</li>
<li>Use each of the following at least once in your picture:</li>
</ol>
<ul>
<ul>
<li>A circle</li>
<li>A rectangle</li>
<li>A line or polygon (one or the other is fine)</li>
<li>Translate, rotate, and scale</li>
<li>Colors (pick whichever seem best)</li>
<li>Comments where appropriate to explain what you&#8217;re doing.</li>
</ul>
</ul>
<p><em><span style="text-decoration:underline;"><strong>Remember:</strong></span></em> The goal isn&#8217;t just to get the computer to draw your picture.  It&#8217;s also to write a program that is easy to read and understand, looks clean and organized, and explains your picture well.  Be a good communicator.</p>
<h1>About These Blog Posts</h1>
<p>Okay, so it&#8217;s become clear that I am sometimes going to need the weekend to work on writing down what we do in class at length.  So, from now on, I&#8217;m going to stop calling this &#8220;late&#8221; and just say that I&#8217;ll plan on getting the blog post done by the end of the weekend.  I&#8217;m just changing the definition of &#8220;late&#8221; so that I&#8217;m not late any more.  Very clever, isn&#8217;t it?</p>
<p>Until next week, have fun, and feel free to ask questions in the blog comments.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/411/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/411/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=411&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2011/09/10/haskell-for-kids-week-4/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/dinner.jpg?w=300" medium="image">
			<media:title type="html">dinner</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/blank.jpg?w=300" medium="image">
			<media:title type="html">blank</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/dinner1.jpg?w=300" medium="image">
			<media:title type="html">dinner1</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/dinner2.jpg?w=300" medium="image">
			<media:title type="html">dinner2</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/dinner3.jpg?w=300" medium="image">
			<media:title type="html">dinner3</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/dinner4.jpg?w=300" medium="image">
			<media:title type="html">dinner4</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/dinner5.jpg?w=300" medium="image">
			<media:title type="html">dinner5</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/dinner6.jpg?w=300" medium="image">
			<media:title type="html">dinner6</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/dinner7.jpg?w=300" medium="image">
			<media:title type="html">dinner7</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/09/pictureexamoptions.jpg" medium="image">
			<media:title type="html">PictureExamOptions</media:title>
		</media:content>
	</item>
		<item>
		<title>Games: Now Available in Gloss-Web</title>
		<link>http://cdsmith.wordpress.com/2011/09/05/games-now-available-in-gloss-web/</link>
		<comments>http://cdsmith.wordpress.com/2011/09/05/games-now-available-in-gloss-web/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 23:31:10 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=409</guid>
		<description><![CDATA[At long last, I can announce the game interface in gloss-web.  This is still a good ways off in my class, but those of you who know what you&#8217;re doing can play around with it anyway.  As always, you can visit http://dac4.designacourse.com:8000 If you&#8217;ve used the site before and don&#8217;t see a game button, you [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=409&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h2><span style="font-size:13px;font-weight:normal;">At long last, I can announce the game interface in gloss-web.  This is still a good ways off in my class, but those of you who know what you&#8217;re doing can play around with it anyway.  As always, you can visit</span></h2>
<p style="padding-left:30px;"><a href="http://dac4.designacourse.com:8000">http://dac4.designacourse.com:8000</a></p>
<p>If you&#8217;ve used the site before and don&#8217;t see a game button, you may need to hit the refresh button.</p>
<h2>How they work:</h2>
<p>Games are similar to simulations, except that the time-step function has one fewer parameter, and there&#8217;s an additional event handling function.  So you&#8217;ll need to write the following:</p>
<pre style="padding-left:30px;">initial :: StdGen -&gt; a</pre>
<p>Just as with simulations, you can pick any data type you like (and probably write your own) to represent the state of the world.  You then define <em>initial</em> to be a function that gets a random number generator, and returns the starting state.  If you need random numbers, you can store the generator in your state type.  The type variable <em>a</em> needs to be the same as <em>a</em> in all the definitions that follow.</p>
<pre style="padding-left:30px;">event :: Event -&gt; a -&gt; a</pre>
<p>Here&#8217;s how you respond to events: write a function with the event and the previous state of the world, that returns a new state of the world as its result.  That means anything you want to remember about the events that have happened should be part of your world type.</p>
<pre style="padding-left:30px;">step :: Float -&gt; a -&gt; a</pre>
<p>Here&#8217;s how you respond to the passage of time.  Much like the <em>step</em> function of the simulation interface, the time parameter is the amount of time that&#8217;s elapsed since the last step.  Unlike simulations, there is now ViewPort passed in.  That&#8217;s because in the OpenGL implementation, simulations automatically provided controls to zoom and such&#8230; but games don&#8217;t.  So you just get the elapsed time.  (Of course, gloss-web doesn&#8217;t provide those automatic transformations anyway.)</p>
<pre style="padding-left:30px;">draw :: a -&gt; Picture</pre>
<p>Finally, of course you need to draw the world.  This is where you say what it looks like.</p>
<h2>An Example</h2>
<p>The default program is a fairly simple one that draws a ball where ever you place the mouse; not much of a game, really!  Never fear, here&#8217;s something a little more involved&#8230; and actually pretty fun!  My best score so far is 31.  Can you do better?</p>
<pre style="padding-left:30px;">import Graphics.Gloss
import Graphics.Gloss.Interface.Game

data World = World {
    player :: Point,
    target :: Point,
    goal   :: Point,
    robot  :: Point,
    speed  :: Float,
    score  :: Integer
    }

initial _ = World (0,0) (0,0) (200,200) (-200,-200) 40 0

event (EventMotion (x,y)) world = world { target = (x,y) }
event _                   world = world

distance (x0, y0) (x1, y1) = sqrt ((x1 - x0)^2 + (y1 - y0)^2)

moveToward (x0,y0) (x1,y1) dist = (x0 + dx, y0 + dy)
    where totalDist             = distance (x0,y0) (x1,y1)
          dx | dist &lt; totalDist = (x1 - x0) * dist / totalDist
             | otherwise        = (x1 - x0)
          dy | dist &lt; totalDist = (y1 - y0) * dist / totalDist
             | otherwise        = (y1 - y0)

collisions world | distance (player world) (goal  world) &lt; 40 = foundGoal
                 | distance (player world) (robot world) &lt; 40 = initial
                 | otherwise                                  = world
  where foundGoal = let (x,y) = player world
                    in  world { goal = (-x, -y), score = score world + 1 }

step time world = collisions newWorld
  where
    newWorld = world {
        player = moveToward (player world) (target world) (time * 60),
        robot  = moveToward (robot world)  (player world) (time * speed world),
        speed  = speed world + (0.1 * time)
        }

draw world = pictures [
    color blue  (figure (player world)),
    color red   (figure (robot  world)),
    color green (figure (goal   world)),
    translate (-225) (-225) (scale 0.2 0.2 (text (show (score world))))
    ]

figure (x,y) = translate x y (circleSolid 20)</pre>
<p>Have fun!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/409/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=409&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2011/09/05/games-now-available-in-gloss-web/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell For Kids: Week 3</title>
		<link>http://cdsmith.wordpress.com/2011/09/03/haskell-for-kids-week-3/</link>
		<comments>http://cdsmith.wordpress.com/2011/09/03/haskell-for-kids-week-3/#comments</comments>
		<pubDate>Sat, 03 Sep 2011 17:57:06 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Gloss]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Haskell for Kids]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=402</guid>
		<description><![CDATA[Where We Are We&#8217;ve gotten a lot done in just two weeks.  To sum up, we&#8217;ve done all of these: Learned how computer programming works: all about compilers, programming languages, and libraries, and how they work together. Wrote computer programs to describe pictures.  We described simple pictures like circles and rectangles.  We modified other pictures [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=402&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h1>Where We Are</h1>
<p>We&#8217;ve gotten a lot done in just two weeks.  To sum up, we&#8217;ve done all of these:</p>
<ul>
<li>Learned how computer programming works: all about compilers, programming languages, and libraries, and how they work together.</li>
<li>Wrote computer programs to describe pictures.  We described simple pictures like circles and rectangles.  We modified other pictures by stretching, rotating, and moving them around.  And most importantly, we combined smaller pictures into bigger and more complicated ones.</li>
<li>Used variables to give names to things in our program, like the individual parts of our pictures&#8230; like <em>sun</em>, <em>elephant</em>, and so on.</li>
<li>Saw for ourselves the importance of staying organized, giving things good names, keeping track of related ideas, and using <em>comments</em> (sections of your program that the compiler ignores) to make notes in your program.</li>
</ul>
<p>If you like, you can review those ideas in <a href="http://cdsmith.wordpress.com/2011/08/16/haskell-for-kids-week-1/">Week 1</a> and <a href="http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-week-2/">Week 2</a>.  Go on, we&#8217;ll wait for you.</p>
<h1>Lines and Polygons</h1>
<p>So far, we&#8217;ve been spending a lot of time drawing with circles and rectangles.  That&#8217;s been fun, but it&#8217;s also limiting.  What if you wanted to draw a star?  Maybe you could rig something up by stretching or rotating rectangles in the right ways, but it sure wouldn&#8217;t be easy!  We spent our second class last week talking about how to do something more flexible: lines and polygons.</p>
<h3>Cartesian Coordinates</h3>
<p>To understand lines and polygons, we&#8217;ll need a way to talk about specific places on the drawing.  We do that using something called <em>cartesian coordinates</em>.  The idea is to talk about a point by giving two numbers:</p>
<ol>
<li>How far from the middle of the painting, horizontally.  We call this the <em>x</em> coordinate.</li>
<li>Haw far from the middle of the painting, vertically.  We call this the <em>y</em> coordinate.</li>
</ol>
<p>Here&#8217;s a picture that might help (taken from Berkeley Science, <a href="http://www.berkeleyscience.com/relativity.htm">here</a>):</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/08/coordinates.jpg"><img class="aligncenter size-full wp-image-405" title="coordinates" src="http://cdsmith.files.wordpress.com/2011/08/coordinates.jpg?w=595" alt=""   /></a></p>
<p>We write a point as (<em>x</em>,<em>y</em>) where <em>x</em> and <em>y</em> are the coordinates as above: <em>x</em> goes right or left, and <em>y</em> goes up or down.  The point in the very middle of the picture is called (0,0) because it&#8217;s immediately in the center <em>both</em> horizontally and vertically.  Other points can be found by following the <em>x</em> line (or &#8220;axis&#8221;) to the first coordinate, then moving up or down alongside the <em>y</em> line (the <em>y</em> &#8221;axis&#8221;) to the second coordinate.  Notice that we use negative numbers to mean &#8220;left&#8221; or &#8220;down&#8221;.</p>
<p><strong><em>Exercise</em></strong>: Take a few minutes to pick some more points on the diagram, and see if you can write down their coordinates &#8212; that is, write them as a pair of numbers in parentheses just like the points already labeled.</p>
<p>If you remember, we said earlier that the drawing area on the <a href="http://dac4.designacourse.com:8000/">gloss-web web site</a> is 500 by 500 in size.  That means the center is still (0,0), but the numbers in your coordinates can range from -250 to 250.  (Why?  Because 250 is half of 500, and half of the size is on each side of the center.)  It might help at this point to get out some graph paper, and draw yourself a square, and see if you can find some points&#8230; like (100,100) or (-200, 50).  As a special bonus, you can also hover your mouse over the drawing area when you run your programs using gloss-web, and you&#8217;ll see a tool tip with the coordinates of the place your mouse pointer is.</p>
<h3>Drawing Lines</h3>
<p>Let&#8217;s use those coordinates to draw things.  Try out this program:</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">picture = line [ (-100, -100), (-200, 50) ]</pre>
<p>Can you guess at what the result will be?  Now run it, and see for yourself.  It drew a line from one point to the other.  You can also draw longer (jagged) lines by listing even more points.</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">picture = line [
    (-200, -200),
    ( 200, -200),
    ( 200,  200),
    (-200,  200),
    (-200, -200)
    ]</pre>
<p>Try to see where those points will end up on the drawing screen, and then what the result will be.  Now run it.  Did it do what you expected?</p>
<h3>Drawing Polygons</h3>
<p>Not only can you draw lines, but you can draw filled-in shapes as well.  These are called polygons, which are just a fancy word for shapes with straight edges as sides.  We can turn the previous example into a filled in shape:</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;">picture = polygon [
    (-200, -200),
    ( 200, -200),
    ( 200,  200),
    (-200,  200)
    ]</pre>
<p>Notice that I didn&#8217;t need to repeat the last point like I did for the line.  That&#8217;s because shapes have to be closed off, so Gloss assumes that we meant to add a line back to where we started.  Of course, rectangles aren&#8217;t very exciting, so here&#8217;s a program that Ms. Sue wrote!</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = stars

stars = pictures [
    translate ( 100) ( 100) (color blue   star),
    translate (-100) (-100) (color yellow star),
    translate (   0) (   0) (color red    star)
    ]

star = polygon [
    (  0,  50),
    ( 10,  20),
    ( 40,  20),
    ( 20,   0),
    ( 30, -30),
    (  0, -10),
    (-30, -30),
    (-20,   0),
    (-40,  20),
    (-10,  20),
    (  0,  50)
    ]</pre>
<p>Can you guess what that does?  Here&#8217;s the result.</p>
<h1><a href="http://cdsmith.files.wordpress.com/2011/08/stars.png"><img class="aligncenter size-medium wp-image-406" title="stars" src="http://cdsmith.files.wordpress.com/2011/08/stars.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></h1>
<h1>Thinking Like a Computer</h1>
<p>That&#8217;s what we did last week.  This week, we&#8217;re talking about how to think about computer programs from the computer&#8217;s perspective.  We&#8217;re going back over everything we&#8217;ve learned, but this time we&#8217;re pretending to be a computer, reading the programs and trying to interpret them.  Here&#8217;s what we&#8217;ve talked about so far:</p>
<h3>Modules</h3>
<p>When we write our computer programs in the web browser, we type everything into a text box and choose Run.  The whole thing we type into our text box is called a <em>module</em>.  So far, you can think of module as a word that means the entire program.  (We talked a little bit about how programs can have more than one module, too.  In fact, most programs in the world do!  But it will be a while before we&#8217;ll really need to separate our programs into more than one module.)</p>
<p>Modules have two kinds of things in them that we&#8217;ve seen so far:</p>
<ul>
<li><strong>Imports</strong>.  These always have to be at the beginning of the module, and they say what other modules (normally from libraries) we&#8217;re using.  For example, we always start our programs by importing Graphics.Gloss, because that gives us all the Gloss stuff, like circles and colors and so on.  We normally only import one library, but if you needed to, you could import more than thing.</li>
<li><strong>Definitions</strong>.  A definition tells the computer what a word (called a <em>variable</em>) means.  For example, if you look at the star program earlier, there are three definitions: for the words &#8220;picture&#8221;, &#8220;stars&#8221;, and &#8220;star&#8221;.</li>
</ul>
<p>An import <em>always</em> starts with the special word &#8220;import&#8221;.  A definition for a variable <em>always</em> has the name of the variable you&#8217;re defining, an equal sign, and the thing you&#8217;re defining it to mean.  Sometimes in the past, we&#8217;ve seen people try to add other stuff to a module that are not imports or definitions.  For example, maybe you&#8217;ve tried to write something like this:</p>
<pre style="padding-left:30px;">import Graphics.Gloss</pre>
<pre style="padding-left:30px;"><span style="color:#ff0000;">circle 80</span></pre>
<p>If you&#8217;ve tried that, you know it doesn&#8217;t work!  You can&#8217;t throw in a description of the circle just anywhere in your program.  The error you&#8217;d get for that would say &#8220;Naked expression at top level&#8221;.  When you see that, it means you forgot to say what variable you are defining!</p>
<h3>How To Be A Compiler</h3>
<p>Here&#8217;s what the compiler does: it reads your program, and remembers what words you&#8217;ve defined.  Then it know a specific word it wants to look up the meaning of.  For us, that specific word is &#8220;picture&#8221;.  So it consults your program, and looks up the meaning of &#8220;picture&#8221;.  Then, if there are any words that occur in <em>there</em> that the compiler doesn&#8217;t know, it tries to look those up, too.  As long as there are words it doesn&#8217;t know, it keeps looking them up!  Basically, when you write a computer program, you&#8217;re giving the compiler a dictionary to look up words in.</p>
<p>With that in mind, think about the next program.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = color myFavoriteColor (circle 100)

myFavoriteColor = blue

stars = pictures [
    translate ( 100) ( 100) (color blue   star),
    translate (-100) (-100) (color yellow star),
    translate (   0) (   0) (color red    star)
    ]

star = polygon [
    (  0,  50),
    ( 10,  20),
    ( 40,  20),
    ( 20,   0),
    ( 30, -30),
    (  0, -10),
    (-30, -30),
    (-20,   0),
    (-40,  20),
    (-10,  20),
    (  0,  50)
    ]</pre>
<p>If you are a compiler and see this program, what do you do?  Here&#8217;s what:</p>
<ul>
<li>You see the import, and make a note that this program uses the Gloss library.  You can then go see what&#8217;s in the Gloss library, and you&#8217;ll see that there are definitions for things like &#8220;circle&#8221; and &#8220;rectangleSolid&#8221; and &#8220;polygon&#8221; and &#8220;green&#8221;.  You now know what these words mean.</li>
<li>You then see the definitions in this module.  There are three of them: &#8220;picture&#8221;, &#8220;stars&#8221;, and &#8220;star&#8221;.</li>
<li>Since your goal in life is to go find out what &#8220;picture&#8221; means and draw it, you read the definition, and try to understand it.</li>
<li>Oops, you don&#8217;t know what &#8220;myFavoriteColor&#8221; means!  Not a problem; you go look that up, and find out that it means blue.</li>
<li>Now you know everything you need about what &#8220;picture&#8221; means, and you&#8217;re done.  You go and draw a blue circle.</li>
</ul>
<p>You might wonder, what about &#8220;stars&#8221; and &#8220;star&#8221;?  Well, you know what they mean&#8230; but they never came up.  Just because a word is in the dictionary doesn&#8217;t mean you necessarily need it.  Remember, the compiler only uses the parts of your program that it needs to draw the thing you&#8217;ve called &#8220;picture&#8221;.  If you want to see a shape, you need to make it part of the definition of picture (or of some other word that happens in picture, and so on).</p>
<h3>Expressions (and we don&#8217;t mean smiles!)</h3>
<p>We can talk a little bit more about the things that come after the equal signs in definitions.  These are called <em>expressions</em>, and there are several forms of them we&#8217;ve used.  You have seen all of these before, but it will help to review them.</p>
<ul>
<li><strong>Variables</strong>.  A variable name (something that you&#8217;ve defined, or something that comes from a library) can be an expression.  To find out what it means, you&#8217;d look up the definition.  Examples of this are <em>picture</em> or <em>green</em>.</li>
<li><strong>Functions (with arguments).</strong>  Another kind of expression we use a lot is a function, along with its <em>arguments</em> (also sometimes called <em>parameters</em>). Examples of this are <em>circle 100</em>, or <em>translate 50 50 star</em>.  Remember that each function expects a certain number of parameters, and you have to give it that many!</li>
<li><strong>Lists</strong>.  A list is a bunch of things in square brackets, separated by commas.  We&#8217;ve seen them in two different places now: the parameter to <em>pictures</em> is a list of other pictures.  The parameter to <em>line</em> or <em>polygon</em> is a list of points.</li>
<li><strong>Points.</strong>  A point is a pair of coordinates in parentheses, with a comma between them.  You need them in <em>line</em> and <em>polygon</em>, to go in the list of points you give it.</li>
<li><strong>Numbers</strong>.  A number is written as just itself, like <em>5</em> or <em>1.75</em>.</li>
<li><strong>Strings</strong>.  Remember with <em>text</em>, where we put things in quotes after it?  That&#8217;s a form of expression too: a <em>string</em>.</li>
</ul>
<p>You might have noticed that not all the forms of expressions give you pictures!  Some of them give you numbers, or points, or lists.  It&#8217;s up to you to make sure that you&#8217;re putting the right type of expression in the right places.  If you try to define <em>picture</em> to be the number 7, for example, it won&#8217;t work!  That&#8217;s because 7 isn&#8217;t a picture&#8230; it&#8217;s just a number.</p>
<p>Now, here&#8217;s where things get interesting: Some of those kinds of expressions have smaller parts&#8230; and those smaller parts are expressions too!  So we build up expressions out of other, smaller expressions.</p>
<p>How about an example?  Let&#8217;s look at Ms. Sue&#8217;s stars program from earlier.  Here it is again, so you don&#8217;t have to scroll so far.</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = stars

stars = pictures [
    translate ( 100) ( 100) (color blue   star),
    translate (-100) (-100) (color yellow star),
    translate (   0) (   0) (color red    star)
    ]

star = polygon [
    (  0,  50),
    ( 10,  20),
    ( 40,  20),
    ( 20,   0),
    ( 30, -30),
    (  0, -10),
    (-30, -30),
    (-20,   0),
    (-40,  20),
    (-10,  20),
    (  0,  50)
    ]</pre>
<p>There are three definitions: <em>picture</em>, <em>stars</em>, and <em>star</em>.</p>
<p>The <em>picture</em> definition is easy.  We say that <em>picture</em> means the expression with just one thing in it: the variable <em>stars</em>.  So a <em>picture</em> is the same thing as a <em>stars</em>.  You can define synonyms like this all over the place!  Marcello noticed that you could write:</p>
<pre style="padding-left:30px;">move = translate</pre>
<p>And then you don&#8217;t have to remember what translate means any more.  (I think you should remember what translate means, though.  Defining a new word for it might help you in the short term to write your program, but it won&#8217;t help at all when you need to read someone else&#8217;s program!)</p>
<p>Let&#8217;s look at <em>star</em>, which is a little bit longer.  The expression used for <em>star</em> is a function with arguments: the function is <em>polygon</em>, and there is one argument.  Remember, that argument is an expression of its own!  In particular, it&#8217;s a list, which means it&#8217;s a bunch of things separated by commas, surrounded by square brackets.  We can go deeper, though: each of the things in the list is yet another smaller expression: in this case, a point.  And finally, each of the coordinates in a point is yet <em>another</em> expression, which is a number.  So we have expressions within expressions within expressions within expressions!  That&#8217;s normally how things go: you do a lot of building up expressions in computer programming.</p>
<p>I&#8217;ve been saving the most complicated one for last.  Let&#8217;s take a look at <em>stars</em>.  This time, I&#8217;ll describe the expression using a bulleted list, and I think you&#8217;ll see why.</p>
<ul>
<li>At the top level, <em>stars</em> is defined to mean a function with arguments&#8230;</li>
<ul>
<li>The function is <em>pictures</em>, which is a variable.  So it has no smaller pieces.</li>
<li>The argument is a list &#8212; a bunch of things in square brackets, separated by commas&#8230;</li>
<ul>
<li>The first thing in the list is a function with arguments&#8230;</li>
<ul>
<li>The function is <em>translate</em>.  That&#8217;s just a variable; it has no smaller parts.</li>
<li>The first argument is 100.  It&#8217;s a number, and has no smaller parts.</li>
<li>The second argument is also 100, and has no smaller parts.</li>
<li>The third argument (which means the picture to draw) is another function with arguments.</li>
<ul>
<li>The function is <em>color</em>, a variable.</li>
<li>The first argument is <em>blue</em>, which is a variable.</li>
<li>The second argument is <em>star,</em> which is also a variable.</li>
</ul>
</ul>
<li>The second thing in the list is a function with arguments&#8230;</li>
<ul>
<li>The function is <em>translate</em>, a variable.</li>
<li>The first argument is -100, a number.</li>
<li>The second argument is -100, a number.</li>
<li>The third argument is another function with arguments.</li>
<ul>
<li>The function is <em>color</em>, a variable.</li>
<li>The first argument is <em>yellow</em>, a variable.</li>
<li>The second argument is <em>star</em>, a variable.</li>
</ul>
</ul>
<li>The third thing in the list is yet another function with arguments.</li>
<ul>
<li>The function is <em>translate</em>, a variable.</li>
<li>The first argument is 0, a number.</li>
<li>The second argument is 0, a number.</li>
<li>The third argument is another function with arguments.</li>
<ul>
<li>The function is <em>color</em>, a variable.</li>
<li>The first argument is <em>red</em>, a variable.</li>
<li>The second argument is <em>star</em>, a variable.</li>
</ul>
</ul>
</ul>
</ul>
</ul>
<p>That was long!  But can you look over it, and see how we can break down the expression into pieces, and each of their pieces is another expression?</p>
<h1>Understanding Programs</h1>
<p>The last thing we did this week, and also your homework, was to try to predict the results of some programs.  Here&#8217;s how this works.</p>
<ol>
<li>You&#8217;ll need some graph paper &#8212; you know, the kind with little squares all over it.</li>
<li>Draw a square on your graph paper&#8230; maybe make it 10 boxes to a side.</li>
<li>Draw a dot right exactly in the middle of your square.  This is the point (0,0).</li>
</ol>
<p>If you made your square 10 boxes to a side, then each box is a distance of 50.  See if you can find some points in your square.</p>
<ul>
<li>Find the point (250, 250).</li>
<ul>
<li>(You should have come up with the top right corner of the box.)</li>
</ul>
<li>Find the point (0, -250).</li>
<ul>
<li>(You should come up with the point in the middle of the bottom edge.)</li>
</ul>
</ul>
<p>Now your goal will be to read some programs, and try to draw what they would draw.  You can check your answers using the programming environment.  <em>Be careful, though.</em>  If you submit one of these programs, your computer will forget the program you&#8217;ve been working on, so be sure to save a copy of that somewhere first!</p>
<h3>Exercise 1: Draw the following.</h3>
<pre style="padding-left:30px;">import Graphics.Gloss
picture = rotate 45 (rectangleWire 150 150)</pre>
<h3>Exercise 2: Draw the following.</h3>
<pre style="padding-left:30px;">import Graphics.Gloss
picture = rotate 135 (pictures [
    circle 50,
    rectangleWire 10 100
    ])</pre>
<h3>Exercise 3: Draw the following.</h3>
<pre style="padding-left:30px;">import Graphics.Gloss
picture = translate 50 0 (rotate 45 triangle)
triangle = line [ (0,0), (100,200), (-100,200), (0,0) ]</pre>
<h3>Exercise 4: Draw the following.</h3>
<pre style="padding-left:30px;">import Graphics.Gloss
picture = rotate 90 (translate 100 0 (circle 100))</pre>
<p>After you&#8217;ve tried those, try making up some expressions of your own, and predict what they are going to do.  We&#8217;re spending some time on this, because before we can move on to animations, it&#8217;s important to see what your programs mean!</p>
<p>That&#8217;s all until next week.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/402/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=402&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2011/09/03/haskell-for-kids-week-3/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/08/coordinates.jpg" medium="image">
			<media:title type="html">coordinates</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/08/stars.png?w=300" medium="image">
			<media:title type="html">stars</media:title>
		</media:content>
	</item>
		<item>
		<title>Haskell For Kids: My Project!</title>
		<link>http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-my-project/</link>
		<comments>http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-my-project/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 02:49:58 +0000</pubDate>
		<dc:creator>cdsmith</dc:creator>
				<category><![CDATA[Haskell for Kids]]></category>

		<guid isPermaLink="false">http://cdsmith.wordpress.com/?p=364</guid>
		<description><![CDATA[Oops!  I almost forgot to do my own homework from my Haskell for Kids class! Here&#8217;s the program I wrote (intentionally using only the features we&#8217;d already talked about in class.) import Graphics.Gloss picture = pictures [ color lightGray (rectangleSolid 500 500), translate 0 (-75) (color darkGray mouse), translate 90 (150) thoughtBubble ] darkGray = [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=364&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Oops!  I almost forgot to do my own homework from my <a href="http://cdsmith.wordpress.com/2011/08/16/haskell-for-kids-week-1/">Haskell for Kids class</a>!</p>
<p>Here&#8217;s the program I wrote (intentionally using only the features we&#8217;d already talked about in class.)</p>
<pre style="padding-left:30px;">import Graphics.Gloss

picture = pictures [
    color lightGray (rectangleSolid 500 500),
    translate  0 (-75) (color darkGray mouse),
    translate 90 (150) thoughtBubble
    ]

darkGray = light black
lightGray = dark white

thoughtBubble = pictures [
                            (scale 100 50 filledCircle),
    translate ( -85) ( -15) (scale 0.4 0.4 (text "I wonder...")),
    translate ( -85) ( -65) (scale 20 20 filledCircle),
    translate (-120) (-110) (scale 15 15 filledCircle)
    ]

filledCircle = pictures [
    color white (circleSolid 1),
    color black (circle 1)
    ]

mouse = pictures [
    scale 2   1 (circleSolid 50),
    translate (-100) ( 40) mouseHead,
    translate ( 120) (-20) mouseTail,
    translate ( -40) (-55) mouseLegs,
    translate (  20) (-55) mouseLegs
    ]

mouseHead = pictures [
    translate 20 35 (color rose (circleSolid 20)),
    translate 20 35 (thickCircle 5 40),
    rotate (-10) (scale 1.5 1 (circleSolid 40))
    ]

mouseTail = (rotate (-15) (rectangleSolid 100 5))

mouseLegs = pictures [
    rectangleSolid 5 35,
    translate 20 0 (rotate 10 (rectangleSolid 5 35))
    ]</pre>
<p>And the result:</p>
<p><a href="http://cdsmith.files.wordpress.com/2011/08/mouse.png"><img class="aligncenter size-medium wp-image-366" title="mouse" src="http://cdsmith.files.wordpress.com/2011/08/mouse.png?w=300&#038;h=300" alt="" width="300" height="300" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/cdsmith.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/cdsmith.wordpress.com/364/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=cdsmith.wordpress.com&#038;blog=1158345&#038;post=364&#038;subd=cdsmith&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://cdsmith.wordpress.com/2011/08/23/haskell-for-kids-my-project/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1e7f8859add1bf7ddd9b5fe9000b6a68?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">cdsmith</media:title>
		</media:content>

		<media:content url="http://cdsmith.files.wordpress.com/2011/08/mouse.png?w=300" medium="image">
			<media:title type="html">mouse</media:title>
		</media:content>
	</item>
	</channel>
</rss>
