fork download
  1. word(0) :- write('zero').
  2. word(1) :- write('one').
  3. word(2) :- write('two').
  4. word(3) :- write('three').
  5. word(4) :- write('four').
  6. word(5) :- write('five').
  7. word(6) :- write('six').
  8. word(7) :- write('seven').
  9. word(8) :- write('eight').
  10. word(9) :- write('nine').
  11.  
  12. num_words(Nums) :- % This is top-level predicate.
  13. NDiv is Nums // 10, % It prints the first digit unconditionally,
  14. dash_num_words(NDiv),% letting you handle the case when the number is zero.
  15. NMod is Nums mod 10,
  16. word(NMod).
  17.  
  18. dash_num_words(0). % When we reach zero, we stop printing.
  19.  
  20. dash_num_words(Nums) :- % Otherwise, we follow your algorithm
  21. Nums > 0, % with one modification: the dash is printed
  22. NDiv is Nums // 10, % unconditionally before printing the digit.
  23. dash_num_words(NDiv),
  24. NMod is Nums mod 10,
  25. word(NMod),
  26. write('-').
  27.  
  28. :- num_words(123), nl, num_words(0), nl.
Success #stdin #stdout #stderr 0.02s 6204KB
stdin
Standard input is empty
stdout
one-two-three
zero
stderr