Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You <em>are</em> on the right path; lets take a look at some changes to your code I would suggest.</p> <p>Your <code>add1</code> function is quite close, but you've got two small problems - the first is the pattern match you provide: to be syntactically correct, you need the whole match to be inside the parentheses. The second is the second cons (colon). cons has a type of <code>a -&gt; [a] -&gt; [a]</code>, so it works perfectly with <code>(x+y)</code> as the first parameter; however, since <code>xs</code> has type <code>[Int]</code> already, you don't need to supply <code>: []</code>.</p> <pre><code>add1 (x:y:xs) = (x+y) : xs </code></pre> <p>Next, because you are dealing entirely with <code>Int</code>s and lists of <code>Int</code>s, using some type <code>a</code> does not make sense in this context.</p> <pre><code>push :: [Int] -&gt; Int -&gt; [Int] </code></pre> <p>Finally, your workhorse function. Because of the lack of loop constructs in Haskell, the way to do a user input loop (also known an <a href="http://en.wikipedia.org/wiki/REPL" rel="nofollow noreferrer">REPL</a>) is through recursion. Because of this, it would make some sense to accept a parameter. Let's make that your <code>[Int]</code> stack. The function to read one line from stdin as a string is <code>getLine</code>, which has type <code>IO String</code>. Finally, you actually have to handle that input. For simplicity, I've just included that logic in a <code>case</code> statement in <code>xcl</code>, but it could as well have been done using <code>dispatch</code> or a similar function (In fact, if your RPN calculater becomes more complicated, this would have its merits). The action for each case should recurse into your xcl "loop" with a modified stack. When you quit, it should just exit - which is a good use of <code>return</code>.</p> <pre><code>xcl :: [Int] -&gt; IO () xcl st = do print st answer &lt;- getLine case answer of "q" -&gt; return () "c" -&gt; xcl ([] ::[Int]) "+" -&gt; xcl $ add1 st x -&gt; xcl $ push st $ read x </code></pre> <p>In fact, you could take this one step further and protect against exceptions - what will happen when the user passes in some non-function, non-number string? The code above will fail, with a "no parse" exception. The best way around that is to use <code>reads</code> in place of <code>read</code>, as is discussed in <a href="https://stackoverflow.com/questions/5121371/how-to-catch-a-no-parse-exception-from-the-read-function-in-haskell/5121537#5121537">this answer</a>. <code>reads</code> returns either a list with a single entry - a tuple containing a parsed number and a remaining string, or an empty list (indicating a failed read). For example:</p> <pre><code> x -&gt; case (reads x) of [(num,_)] -&gt; xcl $ push st num _ -&gt; xcl st </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload