Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract data to list in Prolog?

Tags:

list

prolog

I have an array L of some type, I'm trying to extract the data to an array, for example:

L=[day(sunday),day(monday)]

to

Target=[sunday,monday]

Tried using forall and searched for related questions on Prolog lists.

extract_data_to_list(L,Target) :-
    member(day(Day),L),
    length(L, L1),
    length(Target, L1),
    member(Day,Target).

Current output:

?- extract_data_to_list([day(sunday),day(monday)],Target).
Target = [sunday, _5448] ;
Target = [_5442, sunday] ;
Target = [monday, _5448] ;
Target = [_5442, monday].

Desired output:

?- extract_data_to_list([day(sunday),day(monday)],Target).
Target=[sunday,monday]
like image 645
Dennis Vash Avatar asked Jan 27 '23 21:01

Dennis Vash


2 Answers

This is an ideal problem for maplist:

day_name(day(DayName), DayName).

dates_daylist(Dates, DayList) :-
    maplist(day_name, Dates, DayList).

Maplist applies day_name to each corresponding pair of elements in Dates and DayList.

like image 187
lurker Avatar answered Jan 30 '23 12:01

lurker


This is an ideal problem for library(lambda) for SICStus|SWI:

maplist(\day(N)^N^true, Dates, Daylist).
like image 30
false Avatar answered Jan 30 '23 12:01

false