Discussions related to Visual Prolog
User avatar
Ferenc Nagy
VIP Member
Posts: 215
Joined: 24 Apr 2007 12:26

List::map and list:forall - shall I forget them?

Unread post by Ferenc Nagy »

The new VIP introduced

Code: Select all

foreach and [ || ]
structures.
The

Code: Select all

findall, list::map and list:forall
are earlier language features.
Shall I forget them and use

Code: Select all

foreach and [ || ]
in the future?
TIA, Regards,
Frank Nagy
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

You should forget findall, but the other ones may still be useful/preferred in some contexts.
Regards Thomas Linder Puls
PDC
Peter Muraya
VIP Member
Posts: 147
Joined: 5 Dec 2012 7:29

Unread post by Peter Muraya »

Frank,
My simple understanding is that the list comprehension, [||], replaced findall.

list::map and list::forall are additions that if they commonly appear in your code they are neater to use (and understand) than the equivalent coded using foreach. See the following examples....
...using list::map

Code: Select all

.... Z=list::map(Xs, Y), ...
... and not using list::map

Code: Select all

... C=varM:new(), foreach X in Xs do      C:value:=list::append(C:value, Y(X)) end foreach, Z=C:value, ...
Mutall Data Management Technical Support
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

It would be more natural to use list comprehension to map (and foreach to do forall):

Code: Select all

    % map     Ma = map(L, f),     Mb = [ f(X) || X in L],     % forall     forall(L, p),     foreach Y in L do         p(Y)     end foreach
If the function you map with and the predicate you "forall" with are existing as named entities in your code then the use of the map and forall predcates are quite simple and easy to read.

But if the code is more complex and you for example need to use anonymous predicates then the list comprehension and foreach versions may be prefarable.

Code: Select all

        Z = q(...),         % map         Ma =             map(L,                 { (X) = f(V, Z) :-                     V1 = g(Z, 12),                     V2 = h(V1)                 }),         Mb =             [ f(V, Z) ||                 X in L,                 V1 = g(Z, 12),                 V2 = h(V1)             ],         % forall         forall(L,             { (Y) :-                 V1 = a(Y, Z),                 V2 = b(Y, V1),                 p(V2)             }),         foreach Y in L do                 V1 = a(Y, Z),                 V2 = b(Y, V1),                 p(V2)         end foreach.
Regards Thomas Linder Puls
PDC
Post Reply