Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!asuvax!ncar!midway!quads.uchicago.edu!goer From: goer@quads.uchicago.edu (Richard L. Goerwitz) Newsgroups: comp.lang.icon Subject: Re: Multiple value assignment in icon??? Message-ID: <1991Mar15.013415.1499@midway.uchicago.edu> Date: 15 Mar 91 01:34:15 GMT References: <1991Mar13.180536.22613@cl.cam.ac.uk> Sender: goer@midway.uchicago.edu (Richard L. Goerwitz) Organization: University of Chicago Lines: 70 Bdb@cl.cam.ac.uk (Brian Brunswick) writes (with regard to the problem of assigning a series of results to a series of variables): > >Something like every (x|y):=f() is no good, of course. > This is sooo close to working. Remember that assignment works by evaluating expr1, then evaluating expr2, then assigning the result of expr2 to the result of expr1. Above we have two ex- pressions, 1) (x|y) 2) f() You want to assign each value suspended by f() to a succession of variables. The trouble is that (x|y) is evaluated first, pro- ducing x, then f() is evaluated. If f() succeeds, its result is assigned to x. End backtracking. If you add an every, creating every (x|y) := f() you'll get expr1 producing x, as before, but then you'll get f() producing every result it can, assigning each in turn to x. When it finally fails, x will hold the last result produced by f(). Then (x|y) will be resumed, producing y. The same thing will happen to y, namely it will be assigned the value of each result produced by f() until f() fails, leaving y holding its last pro- duced value. Both x and y will end up with the same value - the last result produced by f() (unless naughty side-effects are in- volved). What we really want is to pop off a result from expr1, and then pop a result off of expr2, assign the result of expr2 to expr1, then pop another result off of expr1, etc. Essentially, we are evaluating expr1 and expr2 in parallel. Interleaving them. How do we do this?? Aha! We've suddenly run into one very good use for coexpressions. Here's what to do: val := create f() every (x|y) := @val Now here's an excercize to see if you fully grasp how Icon de- references variables. Why will the following NOT work the way you want it to? var := create (x|y) every val := f() do var := val Referring to a solution offered (but not quoted here): > Am I missing something, or does this irritation spoil an otherwise > pretty language? Your solution isn't particularly idiomatic. You clearly sensed this. Hence your posting. Your question was really very good, though, be- cause you've clearly grasped precisely the sort of situation that makes coexpressions useful and elegant additions to the language. You aren't missing a thing, but rather demonstrating an understanding of the sort of logic that led to the implementation of coexpressions.... I hope this helps. -Richard Goerwitz (goer@sophist.uchicago.edu)