Article View: comp.lang.prolog
Article #2452Worst use of a DCG you've ever seen
From: John Dowding
Date: Thu, 07 Jan 1993 22:43
Date: Thu, 07 Jan 1993 22:43
38 lines
1421 bytes
1421 bytes
In article <8090@skye.ed.ac.uk> ken@aiai.ed.ac.uk (Ken Johnson) writes:
Secondly: After that, I have written the largest-fit solution to the
problem as a DCG!
(B) The problem expressed as a DCG
sum(S) --> coin(V), {V =< S, !, T is S-V}, sum(T).
sum(0) --> [].
coin(120) --> ['ten shilling note'].
coin(30) --> ['half-crown'].
coin(24) --> [florin].
coin(12) --> [shilling].
coin(6) --> [sixpence].
coin(3) --> ['threepenny bit'].
coin(1) --> [penny].
I realised a while back that you can use DCGs for many programs that
don't look like parsing. My favorite (and most grotesque) is flatten:
flatten(List, FlattenedList):-
phrase(flatten(List), FlattenedList).
flatten([]) --> [].
flatten([Head|Rest]) -->
flatten(Head),
flatten(Rest).
flatten(List) -->
[List].
--
John Dowding
dowding@ai.sri.com
Technical Details
Message-ID:
<DOWDING.93Jan7144312@palm.ai.sri.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!unix!unix.sri.com!dowdingReferences:
<8088@skye.ed.ac.uk> <8090@skye.ed.ac.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!stanford.edu!unix!unix.sri.com!dowding From: dowding@ai.sri.com (John Dowding) Newsgroups: comp.lang.prolog Subject: Worst use of a DCG you've ever seen Message-ID: <DOWDING.93Jan7144312@palm.ai.sri.com> Date: 7 Jan 93 22:43:12 GMT References: <8088@skye.ed.ac.uk> <8090@skye.ed.ac.uk> Sender: news@unix.SRI.COM Followup-To: comp.lang.prolog Organization: SRI International, Menlo Park, CA Lines: 38 In-reply-to: ken@aiai.ed.ac.uk's message of 5 Jan 93 18:31:12 GMT