title: Prolog Numbers to Words
body: <p>I am working with the following predicates to convert numbers to words:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>word(0) :- write('zero').
  word(1) :- write('one').
  word(2) :- write('two').
  word(3) :- write('three').
  word(4) :- write('four').
  word(5) :- write('five').
  word(6) :- write('six').
  word(7) :- write('seven').
  word(8) :- write('eight').
  word(9) :- write('nine').
  dash(X) :- 
      write('-'). %need to add some code here to not print dash after last dig
      
  
  num_words(Nums) :- 
      Nums &gt; 0,
      NMod is Nums mod 10,
      NDiv is Nums // 10,     
      word(NMod),
      dash(Nums), 
      num_words(NDiv).</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Desired Output:</strong></p>
  
  <pre><code>num_words(123) =&gt; one-two-three
  </code></pre>
  
  <p><strong>Current Output:</strong></p>
  
  <pre><code>num_words(123) =&gt; three-two-one-
  </code></pre>
  
  <p>Looking at my output, it looks like my logic is almost correct, but I am failing to grasp how to change the recursion to print the numbers "backwards" so that they are in the right order and how to alter my <code>dash(x)</code> predicate to not print a dash after the last digit.</p>
  
  <p>Do you guys have any suggestions on how to solve this problem?</p>