iad@chaos.cs.brandeis.edu (Ivan Derzhanski) (11/24/90)
SOS! I've been only using Icon for about four weeks, and I'm enjoying it more than I can tell. However, today I ran into the following problem: I was trying to write a procedure that takes a list of coexpressions (its length must be allowed to vary) and suspends lists of the corresponding values of the arguments. For example, the call f ([create 1 to 3, create "Q" | "Z"]) would produce the result sequence { [1,"Q"], [1,"Z"], [2,"Q"], [2,"Z"], [3,"Q"], [3,"Z"] }. I thought is should be easy (given the overall power of Icon), but somehow I can't think of any way of doing it without recursion. Can anyone give me a hint? Thanks, Ivan A. Derzhanski P.S. My apologies if it turns out to be actually very simple. -- Ivan A. Derzhanski iad@chaos.brandeis.edu MB 1766 Brandeis University P.O.Box 9110 Waltham, MA 02254-9110
nowlin@iwtqg.att.com (11/27/90)
> Date: 23 Nov 90 20:54:07 GMT > From: usc!bbn.com!nic!chaos.cs.brandeis.edu!iad@rutgers.edu > Subject: Help/hint needed > > SOS! > > I've been only using Icon for about four weeks, and I'm enjoying it > more than I can tell. However, today I ran into the following problem: > > I was trying to write a procedure that takes a list of coexpressions > (its length must be allowed to vary) and suspends lists of the > corresponding values of the arguments. For example, the call > > f ([create 1 to 3, create "Q" | "Z"]) > > would produce the result sequence > > { [1,"Q"], [1,"Z"], [2,"Q"], [2,"Z"], [3,"Q"], [3,"Z"] }. > > I thought is should be easy (given the overall power of Icon), but > somehow I can't think of any way of doing it without recursion. Can > anyone give me a hint? > > Thanks, > > Ivan A. Derzhanski > > P.S. My apologies if it turns out to be actually very simple. > -- > Ivan A. Derzhanski iad@chaos.brandeis.edu > MB 1766 Brandeis University P.O.Box 9110 Waltham, MA 02254-9110 Admittedly coexpressions are not a piece of Icon I use much. I can't think of a way to do this with coexpressions without recursing either. But I stopped and asked my self why should I use coexpressions if what I want is all the possible combinations of a list of generators? Maybe the example you used was oversimplifying your problem but the following program generates the sequences you asked for without even requiring a procedure: procedure main() every l := [1 to 3, "Q" | "Z"] do { writes("[ ") every writes(!l," ") write("]") } end This isn't the normal coding style I'd use but it isolates the piece of code that's pertinent to the original question. To make this more general you can use a procedure but it's a simple procedure. The following is the same basic program but with a procedure call. The key is to use a list of generators instead of a list of coexpressions. I just prefer to simplify. procedure main() every l := f( [1 to 3, "Q" | "Z"] ) do { writes("[ ") every writes(!l," ") write("]") } end procedure f(lg) suspend lg end This seems almost too easy so if I've really missed the point please let me know. Otherwise, isn't Icon great! Jerry Nowlin (att!iwtqg!nowlin)