Re: [eclipse-clp-users] List of Structures

From: Edgaonkar, Shrirang <Shrirang.Edgaonkar_at_...390...>
Date: Tue, 3 Feb 2015 08:19:50 +0000
Dear Joachim,

  As a matter of fact I have a template which is created out of Dbi library.
The result in my case is Result = [goodsinfo(101, "laptop"), goodsinfo(102, "notebook")].
Here I cannot declare the Template using { } brackets. The syntax is Template = goodsinfo(goodsid:10,goodsname:"dsad"),

session_sql_query(Session, Template, SQL, Cursor),
cursor_all_tuples(Cursor, Result),

filter(Result, goodsid > 101), % To be done

I have reached a stage where I can use the following 

findall(X,(member(X,Result),X=goodsinfo(102, _)),Sols),

to get Sols = [goodsinfo(102, "notebook")]. 
But I want to be able to use complex operators to filter the structure.

Thanks and Regards,
Shrirang Edgaonkar
________________________________________
From: Joachim Schimpf [jschimpf_at_...311...]
Sent: 03 February 2015 03:34:49
To: eclipse-clp-users_at_lists.sourceforge.net
Subject: Re: [eclipse-clp-users] List of Structures

On 02/02/2015 06:55, Edgaonkar, Shrirang wrote:
>
> I have been working on the following problem for some time. I wish to filter the following List.
>
> :- local struct( goodsinfo( goodsid, goodsname, price, saledate, term, zaikoamount, registerdate,
> categoryid, searchViews ) ).
>
>
> Result = [goodsinfo(101, "laptop", "tablet", "iPad"), goodsinfo(102, "notebook", "pendrive", "tab")]
>

Your structure syntax is wrong.  You have declared a structure
goodsinfo/9 (i.e. with 9 arguments), but the example list contains
goodsinfo/4 structures.


Note that there are two ways to write structures in ECLiPSe:

1. Canonical (Prolog-style) notation:  This style can be used without
any preceding struct-declaration, and has the simple form

    functor_name(argument1, ..., argumentN)

i.e. you use *round* brackets and you must write *all* N arguments
in the correct order from 1 to N.  The arity of the structure is N.


2. ECLiPSe's struct-syntax:  This requires a preceding struct-declaration,
and has the form

    functor_name{arg_name:arg_value, ...}

i.e. you use *curly* brackets, and you label every argument with its declared
field name.  You do *not* have to write all N arguments, and you can write the
arguments in any order.  The system knows the arity and argument position from
the struct-declaration.

ECLiPSe basically macro-expands the f{...} syntax into f(...) syntax.  See how
it works by issuing your struct-declaration above, and trying a query like

?- X = goodsinfo{price:9.99, goodsname:"mouse"}.

X = goodsinfo(_275, "mouse", 9.99, _278, _279, _280, _281, _282, _283)
Yes (0.00s cpu)

You see that the convenient {}-syntax has been expanded into the internal form
with all 9 arguments, the price and goodsname arguments have been put into the
correct argument slots, and the remaining arguments filled with free variables.


>
> I am trying something like this
>
> (foreach(goodsinfo( goodsid:GoodsId, goodsname, term, searchViews ),Result), fromto(List,Out,In,[]) do
>     X[goodsid of goodsinfo] == 101 -> Out=[X|In] ; Out=In, writeln( X[goodsid of goodsinfo] )),

The loop structure is essentially correct.  The following would work:

filter101(Xs, Ys) :-
    ( foreach(X,Xs), fromto(Ys,Ys2,Ys1,[]) do
        ( goodsinfo{goodsid:101}=X -> Ys2=[X|Ys1] ; Ys2=Ys1 )

        %%% Alternatively:
        %%% ( X[goodsid of goodsinfo]=:=101 -> Ys2=[X|Ys1] ; Ys2=Ys1 )
        %%% Alternatively:
        %%% arg(goodsid of goodsinfo, X, I), (I==101 -> Ys2=[X|Ys1] ; Ys2=Ys1 )
    ).



-- Joachim





------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
ECLiPSe-CLP-Users mailing list
ECLiPSe-CLP-Users_at_lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/eclipse-clp-users

______________________________________________________________________
Disclaimer: This email and any attachments are sent in strictest confidence
for the sole use of the addressee and may contain legally privileged,
confidential, and proprietary data. If you are not the intended recipient,
please advise the sender by replying promptly to this email and then delete
and destroy this email and any attachments without any further use, copying
or forwarding.
Received on Tue Feb 03 2015 - 08:32:52 CET

This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:21 CEST