You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<divclass="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>
44
44
<imgsrc="http://s3.amazonaws.com/lyah/calculator.png" alt="HA HA HA" class="left" width="220" height="190">
45
45
<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 <spanclass="fixed">"10 4 3 + 2 * -"</span> into a list of items like <spanclass="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 <ahref="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 <ahref="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>
47
47
<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>
48
48
<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, <spanclass="fixed">10, 4, 3</span>, we'll represent that as the list <spanclass="fixed">[3,4,10]</span>.</p>
49
49
<p>Now we have enough information to roughly sketch our function. It's going to take a string, like, <spanclass="fixed">"10 4 3 + 2 * -"</span> and break it down into a list of items by using <spanclass="fixed">words</span> to get <spanclass="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 <spanclass="fixed">[-4]</span>. We take that single item out of the list and that's our final result!</p>
0 commit comments