-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
242 lines (204 loc) · 12.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<html>
<head>
<!-- Bower Components -->
<script src="bower_components/react/react-with-addons.js"></script>
<script src="bower_components/lodash/dist/lodash.js"></script>
<script src="bower_components/node-uuid/uuid.js"></script>
<!-- Line Components -->
<script src="components/Mixins.js"></script>
<script src="components/LineComponent.js"></script>
<script src="components/ApplicationComponent.js"></script>
<script src="components/FunctionNameComponent.js"></script>
<script src="components/ListComponent.js"></script>
<script src="components/IntComponent.js"></script>
<script src="components/LinesComponent.js"></script>
<script src="components/NodeComponent.js"></script>
<script src="components/FunctionEditorComponent.js"></script>
<script src="components/ProgramComponent.js"></script>
<!-- AST Operations -->
<script src="ast_transformations.js"></script>
<script src="initial_ast.js"></script>
<!-- Parser -->
<script src="haskell-parser.js"></script>
<!-- Functions -->
<script src="functions/cons.js"></script>
<script src="functions/plus.js"></script>
<script src="functions/minus.js"></script>
<!-- AST Node Types -->
<script src="astNodeType/int.js"></script>
<script src="astNodeType/list.js"></script>
<script src="astNodeType/application.js"></script>
<script src="astNodeType/functionName.js"></script>
<!-- Styles -->
<link rel="stylesheet" href="app.css">
</head>
<body>
<title>λ Lessons</title>
<div id="top-section" style="width:75%">
<div id="title">
<h1 style="font-size:4em; margin-bottom:0px;">λ Lessons</h1>
<h2 style="font-size:1.7em; color: gray; margin-top:0px;">Pattern matching, first-class functions, and abstracting over recursion in Haskell</h2>
</div>
<div id="introText" style="max-width:1000px;">
<div id="transformTheWayYouThink" style="margin-bottom:15px; font-size:20px;">
This is a short, interactive lesson that teaches core functional programming concepts. It was designed to transform the way you think about performing operations on lists of things, by <strong>showing you how functions are executed.</strong>
</div>
<div id="exploreFunctions" style="margin-bottom:15px; font-size:20px;">
You can explore the way <code class="mapInLesson">map</code> and <code class="foldInLesson">fold</code> (<code class="foldrInLesson">foldr</code> and <code class="foldlInLesson">foldl</code>) are defined and computed. Feel free to <strong>re-define</strong> any of the functions used in this document in the <span class="function-editor-title" style="">Function Editor</span>.
</div>
<div id="haskellSubset" style="margin-bottom:15px; font-size:20px;">
This document implements a small, dynamically-typed, subset of Haskell that includes integers, lists, functions, pattern matching and recursion.
</div>
<div id="creatorsToolsAndSource" style="margin-bottom:15px; font-size:15px; color:gray">
Built by <a href="http://janpaulposma.nl/">Jan Paul Posma</a> & <a href="http://stevekrouse.com">Steve Krouse</a> at YC Hacks '14 with <a href="http://facebook.github.io/react/">React.js</a> & <a href="http://pegjs.majda.cz/">PEG.js</a>. Inspired by <a href="http://worrydream.com">Bret Victor</a> & <a href="http://byorgey.wordpress.com/">Brent Yorgey</a>. Check out the <a href="https://github.com/stevekrouse/hs.js">source</a>.
</div>
</div>
<hr>
</div>
<br>
<div id="function-editor"></div>
<div style="max-width:75%" id="allLessons">
<div id="mapLesson">
<div style="margin-bottom:5px"><code class="mapInLesson" style="font-size:1.8em; font-weight:bold; margin-bottom:10px">map</code></div>
<div>
<code class="mapInLesson">map</code> is a function that performs some operation on every element in a list.
</div>
<pre><div class="mapLessonTypeSignature"><code class="mapInLesson">map</code> :: (a -> b) -> [a] -> [b]</div><div class="mapLessonBaseCase"><code class="mapInLesson">map</code> f [] = []</div><div class="mapLessonRecursiveCase"><code class="mapInLesson">map</code> f (x:xs) = f x : <code class="mapInLesson">map</code> f xs</div></pre>
<div class="mapLessonTypeSignature" >
<span><code class="mapInLesson">map</code> takes 2 inputs</span>
<ul style="padding-left: 30px;margin-top: 5px;">
<li>function of type <code>(a -> b)</code></li>
<li>list of type <code>[a]</code></li>
</ul>
<span>and returns</span>
<ul style="padding-left: 30px;margin-top: 5px;">
<li>list of type <code>[b]</code></li>
</ul>
</div>
<div class="mapLessonPatternMatches" style="padding-top: 10px;">
<div class="mapLessonBaseCase">
The base-case of <code class="mapInLesson">map</code> pattern matches on <code>[]</code> and returns <code>[]</code>.
</div>
</br>
<div class="mapLessonRecursiveCase">
The recursive-case of <code class="mapInLesson">map</code> pattern matches on the first list element <code> x </code> and returns <code>(f x) : <code class="mapInLesson">map</code> f xs</code>.
</div>
</div>
<div id="mapLines"></div>
</div>
<div id="foldlesson" style="margin-top: 10px">
<div style="margin-bottom:5px">
<code class="foldInLesson" style="font-size:1.8em; font-weight:bold; margin-bottom:10px">fold</code>
</div>
<div id="twoFoldDescriptions" style="margin-top:10px; max-width:700px;">
<span> <code class="foldInLesson">fold</code> describes 2 functions that "summarize" the elements in a list.</span>
<ul style="padding-left: 30px;margin-top: 5px;">
<li class="foldrInLessonMain" style="margin-bottom:15px"><code class="foldrInLesson">foldr</code> - "fold <span class="foldrInLesson">r</span>ight", applies <code>f</code> to <code>x</code> and the result of folding <code>f</code> over the rest (remember: <code class="foldrInLesson">foldr</code> moves to the <span class="foldrInLesson">r</span>ight as it computes with the computation on the outside)</li>
<li class="foldlInLessonMain"><code class="foldlInLesson">foldl</code> - "fold <span class="foldlInLesson">l</span>eft", evaluates <code>f x i</code> immediately and uses that as the new initial value for folding <code>f</code> over the rest (remember: <code class="foldlInLesson">foldl</code> stays on the <span class="foldlInLesson">l</span>eft as it computes with the computation on the inside)</li>
</ul>
</div>
<!-- FOLDR -->
<div id="foldrLesson">
<div class="foldrInLessonMain" style="margin-bottom:5px; margin-top:10px;">
<code class="foldrInLesson" style="font-size:1.3em; font-weight:bold; margin-bottom:10px">foldr</code>
</div>
<pre><div class="foldrLessonTypeSignature"><code class="foldrInLesson">foldr</code> :: (a -> b -> b) -> b -> [a] -> b</div><div class="foldrLessonBaseCase"><code class="foldrInLesson">foldr</code> f i [] = i</div><div class="foldrLessonRecursiveCase"><code class="foldrInLesson">foldr</code> f i (x:xs) = f x (<code class="foldrInLesson">foldr</code> f i xs)</div></pre>
<div class="foldrLessonTypeSignature" >
<span><code class="foldrInLesson">foldr</code> takes 3 inputs</span>
<ul style="padding-left: 30px;margin-top: 5px;">
<li>function of type <code>(a -> b -> b)</code></li>
<li>initial value of type <code>b</code></li>
<li>list of type <code>[a]</code></li>
</ul>
<span>and returns</span>
<ul style="padding-left: 30px;margin-top: 5px;">
<li>accumulated value of type <code>b</code></li>
</ul>
</div>
<div class="foldrLessonPatternMatches" style="padding-top: 10px;">
<div class="foldrLessonBaseCase">
The base-case of <code class="foldrInLesson">foldr</code> pattern matches on <code>[]</code> and returns <code>i</code>.
</div>
</br>
<div class="foldrLessonRecursiveCase">
The recursive-case of <code class="foldrInLesson">foldr</code> pattern matches on the first list element <code> x </code> and returns <code>f x (<code class="foldrInLesson">foldr</code> f i xs)</code>.
</div>
</div>
<div id="foldrLines"></div>
</div>
<!-- FOLDL -->
<div id="foldlLesson">
<div class="foldlInLessonMain" style="margin-bottom:5px; margin-top:10px;">
<code class="foldlInLesson" style="font-size:1.3em; font-weight:bold; margin-bottom:10px">foldl</code>
</div>
<pre><div class="foldlLessonTypeSignature"><code class="foldlInLesson">foldl</code> :: (a -> b -> a) -> a -> [b] -> a</div><div class="foldlLessonBaseCase"><code class="foldlInLesson">foldl</code> f i [] = i</div><div class="foldlLessonRecursiveCase"><code class="foldlInLesson">foldl</code> f i (x:xs) = <code><code class="foldlInLesson">foldl</code> f (f i x) xs</code></div></pre>
<div class="foldlLessonTypeSignature" >
<span><code class="foldlInLesson">foldl</code> takes 3 inputs</span>
<ul style="padding-left: 30px;margin-top: 5px;">
<li>function of type <code>(a -> b -> a)</code></li>
<li>initial value of type <code>a</code></li>
<li>list of type <code>[b]</code></li>
</ul>
<span>and returns</span>
<ul style="padding-left: 30px;margin-top: 5px;">
<li>accumulated value of type <code>a</code></li>
</ul>
</div>
<div class="foldlLessonPatternMatches" style="padding-top: 10px;">
<div class="foldlLessonBaseCase">
The base-case of <code class="foldlInLesson">foldl</code> pattern matches on <code>[]</code> and returns <code>i</code>.
</div>
</br>
<div class="foldlLessonRecursiveCase">
The recursive-case of <code class="foldlInLesson">foldl</code> pattern matches on the first list element <code> x </code> and returns <code><code class="foldlInLesson">foldl</code> f (f i x) xs</code>.
</div>
</div>
<div id="foldlLines"></div>
</div>
</div>
</div>
</body>
<script>
var toggleLessonHighlight = function(e) {
var blahs = document.getElementsByClassName(e.target.classList[0]);
for (var i = 0; i < blahs.length; i ++){
blah = blahs[i];
blah.classList.toggle("lesson-highlight");
};
};
names = ['mapLessonRecursiveCase', 'mapLessonBaseCase', 'mapLessonTypeSignature', 'foldrLessonTypeSignature', 'foldrLessonBaseCase','foldrLessonRecursiveCase', 'foldlLessonTypeSignature', 'foldlLessonBaseCase', 'foldlLessonRecursiveCase', 'foldrInLessonMain', 'foldlInLessonMain']
for (var j = 0; j < names.length; j++) {
var name = names[j];
var things = document.getElementsByClassName(name);
for (var i = 0; i < things.length; i ++){
thing = things[i];
thing.addEventListener('mouseenter', toggleLessonHighlight);
thing.addEventListener('mouseleave', toggleLessonHighlight);
}
}
React.renderComponent(
HaskellJSProgram({defaultValue: 'map addOne [1,2,3,4,5]'}),
document.getElementById('mapLines')
);
React.renderComponent(
HaskellJSProgram({defaultValue: 'foldr plus 0 [1,2,3,4,5]'}),
document.getElementById('foldrLines')
);
React.renderComponent(
HaskellJSProgram({defaultValue: 'foldl reverseCons [] [1,2,3,4,5]'}),
document.getElementById('foldlLines')
);
React.renderComponent(
FunctionEditor({defaultFunctionDefinitions: window.initialFunctionDefinitions}),
document.getElementById('function-editor')
);
</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-53516624-1', 'auto');
ga('send', 'pageview');
</script>
</html>