From: "Kjeld H. Mortensen" To: designCPN@daimi.au.dk Date: Wed, 23 Jul 2003 09:19:00 +0200 Subject: (dCPN) Re: Accessing values in lists of tuples Dear Johannes Giere Re 1: The formal type of ABClist is "(string * int * string) list", i.e., a list of triples. You should be aware of that CPN ML is like SML/NJ but with syntactic sugar, e.g., colour sets and variable declarations. From SML/NJ you get a number of useful functions for manipulating lists, e.g., List.nth: The Standard ML Basis Library http://www.smlnj.org/doc/basis/ http://www.smlnj.org/doc/basis/pages/list.html >From CPN ML you get some additional functions, e.g., mkst_col'ABSlist for getting a string representation of a colour, see also: CPN ML Reference http://www.daimi.au.dk/designCPN/man/ Re 2: You can easily use List.exist to find a triple where the integer is 5: val ex1 = [("a",4,"b"),("c",5,"d")]; val ex2 = [("a",4,"b"),("c",6,"d")]; fun exists_5 l = List.exists (fn (_,i,_) => i=5) l; exists_5 ex1; (* returns true *) exists_5 ex2; (* returns false*) Now you can use exists_5 in a guard. Re 3: Writing functions with the pattern matching and currying techniques is quite powerful: fun search_n (n:int) nil = ("",~1,"") | search_n n ((head as (_,i,_))::tail) = if (i=n) then head else (search_n n tail); val search_5 = search_n 5; search_5 ex1; (* returns ("c",5,"d") *) search_5 ex2; (* returns ("",~1,"") suggesting nothing found *) If you don't know of these techniques I'll suggest that you read one of the books on ML listed on the following Web page. Mastering these techniques is extremely useful -- in particular pattern matching. Standard ML Resources http://www.daimi.au.dk/designCPN/sml/ Regards, -- Kjeld H. Mortensen * khm@daimi.au.dk * www.daimi.au.dk/~khm University of Aarhus * Department of Computer Science * Denmark Quoting Johannes Giere : > I have a question concerning the use of functions with lists of tupels: > > A list, which is based on a tupel colorset should be used to model a system. > > color A = string; > color B = int; > color C = string; > > color ABC = product A*B*C; > color ABClist = list ABC; > > var a : A; > var b : B; > var c : C; > var abc : ABC; > > The following questions respectively problems occur: > > 1. What is the formal type of ABClist? A list or rather something like a > listpair structure? > The question is, which predefined functions may I use in the context of > this list (e.g. list.find) ? > > 2. How can I construct a query based on only one element of the tuple? > > Example: The ABClist may contain the following initial marking > [("o",2,"p"),("u",3,"m")]. Now I would like to model a decision based on > the second value of the tuple, i.e. var b? The transition should only fire, > i.e. the list of all tuples should only be transmitted if any of the > variables b evaluates to 5. Among other functions I tried to model this > problem by means of the predefined function exists, but various > misunderstandings between Design/CPN and me occurred due to the fact that I > do not know how to get a hold of a variable inside the tuple. > > 3. If the problem described before can be solved, another question > arises. If I know that there exists a tuple containing for example a value > b=5, it would also be of utmost interest to receive the values of the > corresponding tuple, i.e. the values of a and c (for the case b=5). > > The problems/questions stated above are mainly based on the fact that I > have no idea on how to get access to information stored within the tuple, > of course only if the tuple is part of a list. > > Thanks a lot > Johannes Giere > > > ----------------------------------------------------------------------------- - > Dipl.-Ing. Johannes Giere > > Institut für Geotechnik // Institute of Geotechnics > Technische Universität Darmstadt // Darmstadt University of > Technology > Petersenstraße 13 > 64287 Darmstadt > GERMANY > > fon +49-6151-16 34 49 > fax +49-6151-16 66 83 > e-mail giere@geotechnik.tu-darmstadt.de > ---------------------------------------------------------------------------- > > --- > > --- > [[ Post messages and summary of replies: designCPN@daimi.au.dk > ]] > [[ To (un)subscribe, send "help" to: Majordomo@daimi.au.dk > ]] > [[ The moderator's address: designCPN-owner@daimi.au.dk > ]] > [[ World Wide Web URL: http://www.daimi.au.dk/designCPN/email/ > ]] > --- [[ Post messages and summary of replies: designCPN@daimi.au.dk ]] [[ To (un)subscribe, send "help" to: Majordomo@daimi.au.dk ]] [[ The moderator's address: designCPN-owner@daimi.au.dk ]] [[ World Wide Web URL: http://www.daimi.au.dk/designCPN/email/ ]]