home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Demos / commaint.lgs < prev    next >
Text File  |  1994-06-23  |  2KB  |  48 lines

  1. This file contains the definition of commaint, a function which takes a
  2. single string argument containing a sequence of digits, and outputs the
  3. same sequence with commas inserted after every group of three digits,
  4. reading from the right hand end of the string.
  5.  
  6. >  commaint = reverse . foldr1 (\x y->x++","++y) . group 3 . reverse
  7. >     where group n = takeWhile (not.null) . map (take n) . iterate (drop n)
  8.  
  9. This definition uses the following library functions:
  10.  
  11.   reverse, (.), foldr1, (++), takeWhile, not, null, map, take, iterate, drop.
  12.  
  13. Example: evaluation of commaint "1234567"
  14.  
  15.            "1234567"
  16.                |
  17.                | reverse
  18.                V
  19.            "7654321" _______________________________
  20.                |                                    \
  21.                | iterate (drop 3)                    |
  22.                V                                     |
  23.            ["7654321", "4321", "1", "", "", ...]     |
  24.                |                                     |
  25.                | map (take 3)                        V  group 3
  26.                V                                     |
  27.            ["765", "432", "1", "", "", ...]          |
  28.                |                                     |
  29.                | takeWhile (not.null)                |
  30.                V     _______________________________/
  31.            ["765", "432", "1"]
  32.                |
  33.                | foldr1 (\x y->x++","++y)
  34.                V
  35.            "765,432,1"
  36.                |
  37.                | reverse
  38.                V
  39.            "1,234,567"
  40.  
  41. In a Gofer session:
  42.  
  43.     ? commaint "1234567"
  44.     1,234,567
  45.     (105 reductions, 203 cells)
  46.     ?
  47.  
  48.