home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / prolog / 2333 < prev    next >
Encoding:
Internet Message Format  |  1993-01-07  |  1.3 KB

  1. Path: sparky!uunet!stanford.edu!unix!unix.sri.com!dowding
  2. From: dowding@ai.sri.com (John Dowding)
  3. Newsgroups: comp.lang.prolog
  4. Subject: Worst use of a DCG you've ever seen
  5. Message-ID: <DOWDING.93Jan7144312@palm.ai.sri.com>
  6. Date: 7 Jan 93 22:43:12 GMT
  7. References: <8088@skye.ed.ac.uk> <8090@skye.ed.ac.uk>
  8. Sender: news@unix.SRI.COM
  9. Followup-To: comp.lang.prolog
  10. Organization: SRI International, Menlo Park, CA
  11. Lines: 38
  12. In-reply-to: ken@aiai.ed.ac.uk's message of 5 Jan 93 18:31:12 GMT
  13.  
  14. In article <8090@skye.ed.ac.uk> ken@aiai.ed.ac.uk (Ken Johnson) writes:
  15.  
  16.    Secondly: After that, I have written the largest-fit solution to the
  17.    problem as a DCG!
  18.  
  19.    (B) The problem expressed as a DCG
  20.  
  21.          sum(S) --> coin(V), {V =< S, !, T is S-V}, sum(T).
  22.          sum(0) --> [].
  23.  
  24.          coin(120) --> ['ten shilling note'].
  25.          coin(30)  --> ['half-crown'].
  26.          coin(24)  --> [florin].
  27.          coin(12)  --> [shilling].
  28.          coin(6)   --> [sixpence].
  29.          coin(3)   --> ['threepenny bit'].
  30.          coin(1)   --> [penny].
  31.  
  32.  
  33. I realised a while back that you can use DCGs for many programs that
  34. don't look like parsing.  My favorite (and most grotesque) is flatten:
  35.  
  36. flatten(List, FlattenedList):-
  37.     phrase(flatten(List), FlattenedList).
  38.  
  39. flatten([]) --> [].
  40.  
  41. flatten([Head|Rest]) -->
  42.     flatten(Head),
  43.     flatten(Rest).
  44.  
  45. flatten(List) -->
  46.     [List].
  47.  
  48.  
  49. --
  50. John Dowding
  51. dowding@ai.sri.com
  52.