Re: How to return clause in findall/3
From: reader (no-spam_at_sonic.net)
Date: 08/12/04
- Next message: Pere Montolio: "Re: How to return clause in findall/3"
- Previous message: reader: "Re: Help I'm stuck!"
- In reply to: rhapsody: "How to return clause in findall/3"
- Next in thread: Pere Montolio: "Re: How to return clause in findall/3"
- Reply: Pere Montolio: "Re: How to return clause in findall/3"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 12 Aug 2004 19:59:51 GMT
rhapsody wrote:
> Hi, I am new to Prolog. What is the correct way of returning a list of
> clauses 'sc/3' to the variable X? Below is my codes:
> Database
> sc(symbol Sid,symbol Cid, integer ExamScore).
>
> Clauses
> sc("S1","C1",55).
> sc("S1","C2",60).
> sc("S1","C3",70).
> Goal
> findall(sc(),sc(X,_,_),List), X="S1", write(List), nl.
>
> Thanks in advance!
> Stanly
>
/*
There is no such thing as a list of clauses in that sense,
but there is nothing to stop you from constructing a term that
has the same form as a clause.
*/
DOMAINS
Datum = datum(symbol Sid,symbol Cid, integer ExamScore).
List = Datum*
Predicates
collect(List,List).
Clauses
collect(In,Out) :-
retract(sc(Sid,Cid,ExamScore)),
!,
collect([datum(Sid,Cid,ExamScore)|In],Out).
collect(In,In). /* Note: reverses order! */
Goal
collect([],List).
whence
List=[datum("S1","C3",70),datum("S1","C2",60),datum("S1","C1",55)]
1 Solution
- Next message: Pere Montolio: "Re: How to return clause in findall/3"
- Previous message: reader: "Re: Help I'm stuck!"
- In reply to: rhapsody: "How to return clause in findall/3"
- Next in thread: Pere Montolio: "Re: How to return clause in findall/3"
- Reply: Pere Montolio: "Re: How to return clause in findall/3"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|