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 ) ). -- JoachimReceived on Mon Feb 02 2015 - 18:34:59 CET
This archive was generated by hypermail 2.2.0 : Tue Feb 03 2015 - 12:13:12 CET