Skip to content

Commit 8b93e1b

Browse files
authored
Merge pull request #11 from memowe/cleanup
Cleanup higher-order-functions page
2 parents b16678b + c0763e6 commit 8b93e1b

File tree

3 files changed

+12
-519
lines changed

3 files changed

+12
-519
lines changed

docs/functionally-solving-problems.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ <h1>Functionally Solving Problems</h1>
4343
<div class="hintbox"><em>Protip:</em> it really helps to first think what the type declaration of a function should be before concerning ourselves with the implementation and then write it down. In Haskell, a function's type declaration tells us a whole lot about the function, due to the very strong type system.</div>
4444
<img src="http://s3.amazonaws.com/lyah/calculator.png" alt="HA HA HA" class="left" width="220" height="190">
4545
<p>Cool. When implementing a solution to a problem in Haskell, it's also good to think back on how you did it by hand and maybe try to see if you can gain any insight from that. Here we see that we treated every number or operator that was separated by a space as a single item. So it might help us if we start by breaking a string like <span class="fixed">"10 4 3 + 2 * -"</span> into a list of items like <span class="fixed">["10","4","3","+","2","*","-"]</span>.</p>
46-
<p>Next up, what did we do with that list of items in our head? We went over it from left to right and kept a stack as we did that. Does the previous sentence remind you of anything? Remember, in the section about <a href="higher-order-functions/index.html#folds">folds</a>, we said that pretty much any function where you traverse a list from left to right or right to left one element by element and build up (accumulate) some result (whether it's a number, a list, a stack, whatever) can be implemented with a fold.</p>
46+
<p>Next up, what did we do with that list of items in our head? We went over it from left to right and kept a stack as we did that. Does the previous sentence remind you of anything? Remember, in the section about <a href="higher-order-functions.html#folds">folds</a>, we said that pretty much any function where you traverse a list from left to right or right to left one element by element and build up (accumulate) some result (whether it's a number, a list, a stack, whatever) can be implemented with a fold.</p>
4747
<p>In this case, we're going to use a left fold, because we go over the list from left to right. The accumulator value will be our stack and hence, the result from the fold will also be a stack, only as we've seen, it will only have one item.</p>
4848
<p>One more thing to think about is, well, how are we going to represent the stack? I propose we use a list. Also I propose that we keep the top of our stack at the head of the list. That's because adding to the head (beginning) of a list is much faster than adding to the end of it. So if we have a stack of, say, <span class="fixed">10, 4, 3</span>, we'll represent that as the list <span class="fixed">[3,4,10]</span>.</p>
4949
<p>Now we have enough information to roughly sketch our function. It's going to take a string, like, <span class="fixed">"10 4 3 + 2 * -"</span> and break it down into a list of items by using <span class="fixed">words</span> to get <span class="fixed">["10","4","3","+","2","*","-"]</span>. Next, we'll do a left fold over that list and end up with a stack that has a single item, so <span class="fixed">[-4]</span>. We take that single item out of the list and that's our final result!</p>

0 commit comments

Comments
 (0)