Re: "maplist" for DCG?

Edison Mera <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <CAKkUgY5nXZ9UPMvTttYSaeURW8wr1uMMNwHafRbSsuh-OcJeug@mail.gmail.com>
Once ago I have a similar need, and implemented a set of maplist_dcg//N
predicates in one of my libraries, basically is the code Richard wrote for
mapnt, but using the call/(N+1) predicates:

:- module(maplist_dcg, [maplist_dcg/4,
                        maplist_dcg/5,
                        maplist_dcg/6,
                        maplist_dcg/7,
                        maplist_dcg/8,
                        maplist_dcg/9]).

:- meta_predicate maplist_dcg(3, ?, ?, ?).

maplist_dcg(Goal, List) -->
    maplist_dcg_(List, Goal).

:- meta_predicate maplist_dcg_(?, 3, ?, ?).

maplist_dcg_([],    _) --> [].
maplist_dcg_([E|T], Goal) -->
    call(Goal, E),
    maplist_dcg_(T, Goal).

:- meta_predicate maplist_dcg(4, ?, ?, ?, ?).

maplist_dcg(Goal, List1, List2) -->
    maplist_dcg_(List1, List2, Goal).

:- meta_predicate maplist_dcg_(?, ?, 4, ?, ?).

maplist_dcg_([],       [],      _) --> [].
maplist_dcg_([E1|T1] , [E2|T2], Goal) -->
    call(Goal, E1, E2),
    maplist_dcg_(T1, T2, Goal).
...

The source is part of the refactoring tool I made:
https://github.com/edisonm/refactor/blob/master/prolog/maplist_dcg.pl
Regards,

Edison <http://edisonm.com/>


2013/11/21 Michael Ben Yosef <[email protected]>

> > maplist(F, In, Out) is one thing, but how do you do the equivalent with
> a DCG, so the effect is something//2
> > something(f, [In1, In2, In3])
> > is equivalent to
> > f(In1), f(In2), f(In3)
> >
> > where f/1 might be:
> > f(X) --> { code(X,Y)}, [Y].
> >
> > In other words each input gets processed and appended to the DCG list.
>  I feel like there's probably a standard predicate for this.
> >
> >
> > Alan Baljeu
>
> Is the something//2 you're looking for not simply foldl/4? (Sorry I'm
> late to this thread.)
>
> Michael
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
-------------- next part --------------
HTML attachment scrubbed and removed
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.