[m-users.] Using string literals as character lists in a DCG

Volker Wysk post at volker-wysk.de
Tue Oct 18 03:37:38 AEDT 2022


Am Montag, dem 17.10.2022 um 21:56 +0530 schrieb Razetime:
> I am trying to include a string as a sequence of characters in a DCG.
> 
> This is my predicate l, which should take a string X and match the
> list of characters represented by X in a DCG:
> 
> :- pred l(string::in,cl::in,cl::out) is semidet.
> l(X,Y,Z):-string.to_char_list(X,X0),append(X0,Z,Y).
> 
> :- pred seq(list(T),list(T),list(T)).
> :- mode seq(out,in,out) is multi.
> seq([])     --> [].
> seq([E|Es]) --> [E], seq(Es).
> 
> I wish to use it in a dcg like so:
> thing --> l("thing").
> 
> such that the dcg matches ['t','h','i','n','g'].
> 
> My questions are:
> a) Is there a flag like :- set_prolog_flag(double_quotes, chars) that
> allows one to use strings easily in DCGs?

No.

> b) Otherwise, how can I frame my predicate to match a string correctly?

You already have it in your question. That predicate l will work, the way it
is.

Cheers, Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20221017/d5842907/attachment.sig>


More information about the users mailing list