-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRACKET-CHEATSHEET.html
20 lines (20 loc) · 13.2 KB
/
RACKET-CHEATSHEET.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><meta name="viewport" content="width=device-width, initial-scale=0.8"/><title>A Racket Cheatsheet</title><link rel="stylesheet" type="text/css" href="scribble.css" title="default"/><link rel="stylesheet" type="text/css" href="racket.css" title="default"/><link rel="stylesheet" type="text/css" href="manual-style.css" title="default"/><link rel="stylesheet" type="text/css" href="manual-racket.css" title="default"/><script type="text/javascript" src="scribble-common.js"></script><script type="text/javascript" src="manual-racket.js"></script><!--[if IE 6]><style type="text/css">.SIEHidden { overflow: hidden; }</style><![endif]--></head><body id="scribble-racket-lang-org"><div class="tocset"><div class="tocview"><div class="tocviewlist tocviewlisttopspace"><div class="tocviewtitle"><table cellspacing="0" cellpadding="0"><tr><td style="width: 1em;"><a href="javascript:void(0);" title="Expand/Collapse" class="tocviewtoggle" onclick="TocviewToggle(this,"tocview_0");">►</a></td><td></td><td><a href="" class="tocviewselflink" data-pltdoc="x">A Racket Cheatsheet</a></td></tr></table></div><div class="tocviewsublistonly" style="display: none;" id="tocview_0"><table cellspacing="0" cellpadding="0"><tr><td align="right">1 </td><td><a href="#%28part._.The_.Basics%29" class="tocviewlink" data-pltdoc="x">The Basics</a></td></tr></table></div></div></div><div class="tocsub"><table class="tocsublist" cellspacing="0"><tr><td><span class="tocsublinknumber"></span><a href="#%28part._.A_.Racket_.Cheatsheet%29" class="tocsubseclink" data-pltdoc="x">A Racket Cheatsheet</a></td></tr><tr><td><span class="tocsublinknumber">1<tt> </tt></span><a href="#%28part._.The_.Basics%29" class="tocsubseclink" data-pltdoc="x">The Basics</a></td></tr></table></div></div><div class="maincolumn"><div class="main"><div class="versionbox"><span class="versionNoNav">8.1</span></div><h2><a name="(part._.A_.Racket_.Cheatsheet)"></a>A Racket Cheatsheet</h2><p>This is meant to serve two purposes: A <span style="font-weight: bold">quick reference</span> on
racket itself, and an <span style="font-weight: bold">introduction for people with some
programming experience</span>.</p><h3>1<tt> </tt><a name="(part._.The_.Basics)"></a>The Basics</h3><p>For those of you with some programming experience, the neat thing
about Racket is how simple it is. The core language is super small,
and you just need to know a few bits of syntax to get started. In
particular:</p><ul><li><p>Applying functions</p></li><li><p>Naming things (otherwise known as binding variables)</p></li><li><p>Defining functions</p></li><li><p>Conditionals</p></li></ul><p>Let’s talk about each of these with some examples. Along the way,
we’ll learn a bit about some of the functions and data types that
Racket comes with.</p><p><span style="font-weight: bold">Applying functions</span> in Racket is simple, if a little unfamiliar. It
always looks like this:</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">string-append</span><span class="hspace"> </span><span class="RktVal">"Hello"</span><span class="hspace"> </span><span class="RktVal">" World!"</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">"Hello World!"</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">+</span><span class="hspace"> </span><span class="RktVal">3</span><span class="hspace"> </span><span class="RktVal">4</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">7</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">+</span><span class="hspace"> </span><span class="RktVal">1</span><span class="hspace"> </span><span class="RktVal">2</span><span class="hspace"> </span><span class="RktVal">3</span><span class="hspace"> </span><span class="RktVal">8</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">14</span></p></td></tr></table></blockquote><p>As you can see, a function application is a parenthesized list, where
the first thing is the function, and the remaining things are the
argument to that function. This looks pretty weird when doing
arithmetic, but you get used to it.</p><p><span style="font-weight: bold">Naming things</span> is mostly done using the define syntax.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace"> </span><span class="RktSym">seven</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">+</span><span class="hspace"> </span><span class="RktVal">3</span><span class="hspace"> </span><span class="RktVal">4</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">+</span><span class="hspace"> </span><span class="RktSym">seven</span><span class="hspace"> </span><span class="RktSym">seven</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">14</span></p></td></tr></table></blockquote><p><span style="font-weight: bold">Defining functions</span> is also typically done with a variant of the
define syntax.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">double</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">*</span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktVal">2</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">double</span><span class="hspace"> </span><span class="RktVal">10</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">20</span></p></td></tr></table></blockquote><p><span style="font-weight: bold">Conditionals</span> are the last critical language
feature. Conditionals are what let you decide between different
statements. There are two versions of this that come up: if and cond,
but we recommend you mostly stick to cond. Cond lets you define an
arbitrary sequence of alternatives, with a test for each one.</p><p>Here’s a really simple example: a function that constrains a number
within a range.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">clamp</span><span class="hspace"> </span><span class="RktSym">min</span><span class="hspace"> </span><span class="RktSym">max</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">cond</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym">></span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">max</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">max</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktPn">(</span><span class="RktSym"><</span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">min</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktSym">min</span><span class="RktPn">]</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">[</span><span class="RktSym">else</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">]</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">clamp</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktVal">20</span><span class="hspace"> </span><span class="RktVal">0</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">10</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">clamp</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktVal">20</span><span class="hspace"> </span><span class="RktVal">100</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">20</span></p></td></tr><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">clamp</span><span class="hspace"> </span><span class="RktVal">10</span><span class="hspace"> </span><span class="RktVal">20</span><span class="hspace"> </span><span class="RktVal">12</span><span class="RktPn">)</span></td></tr><tr><td><p><span class="RktRes">12</span></p></td></tr></table></blockquote><p>Note that all these forms can be freely intermixed, i.e., you can
define a function inside of a function.</p><blockquote class="SCodeFlow"><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><table cellspacing="0" cellpadding="0" class="RktBlk"><tr><td><span class="stt">> </span><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">scale-vector</span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">y</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">square</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">*</span><span class="hspace"> </span><span class="RktSym">x</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace"> </span><span class="RktSym">mag</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">+</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">square</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">square</span><span class="hspace"> </span><span class="RktSym">y</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">define</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">scale</span><span class="hspace"> </span><span class="RktSym">z</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">/</span><span class="hspace"> </span><span class="RktSym">z</span><span class="hspace"> </span><span class="RktSym">mag</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr><tr><td><span class="hspace"> </span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">values</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">scale</span><span class="hspace"> </span><span class="RktSym">x</span><span class="RktPn">)</span><span class="hspace"> </span><span class="RktPn">(</span><span class="RktSym">scale</span><span class="hspace"> </span><span class="RktSym">y</span><span class="RktPn">)</span><span class="RktPn">)</span><span class="RktPn">)</span></td></tr></table></td></tr></table></blockquote></div></div><div id="contextindicator"> </div></body></html>