首页 > 解决方案 > How to filter in Prolog with SWISH

问题描述

I need to make a predicate called fAtomPairs so that given an atom (first argument), and a list of pairs (each pair is in turn a list of two atoms), unify a third parameter with the filtered list of pairs by selecting only those pairs that have as their first component the atom which is the first argument.

For example :

fAtomPairs(sA,[[basA,absAb],[ab,bbsA],[sA,abbsB],[bsA,sAsB],[sA,bb]],X)  

must result in

X = [[sA,abbsB],[sA,bb]]

How can I do this?, I'm currently working in SWISH

标签: listprologfiltering

解决方案


:- use_module(library(reif)).   % SICStus|SWI
:- use_module(library(lambda)). % SICStus|SWI

fAtomPairs(Sel, DEs, Es) :-
   tfilter(\[A,_]^ ( Sel = A ), DEs, Es).

推荐阅读