You can think of a list as a typeless dynamic array (which may contain elements that are also typeless dynamic arrays). Which also so happens to be the same form procedure definitions are kept in (among many other things). All the posts on this subject have not varied by that much. It drives me nuts when people think a version of code is more complex because someone took the time to comment it and break it down to smaller steps as Frank did. It's actually kinda sad. Brian's code below is really on the more complex side even though it LOOKS short. Brians parser handles variable number of arguments. If the argument is not supplied he will generate a random value based on another argument (all before you even get into the body of the procedure !!!). Also keep in mind some folks have used built in functions not available to everyone while others have worked with basic common functions to all logos. Also there have been 2 (or more) DIFFERENT problems (and solutions for them presented here). Which I hope I got through the difference to most people (in summary are, we dealing with a SET or LIST?). First it was return the "list or a set?" with a random item removed. Frank looked at it as if it was a set (same as PICK/REMOVE accept he wrote remove). I looked at it as if it was a list. Then it turned out it was also required to make available the item itself that was removed. That piece of data is exposed in franks method it's not in mine. Brian's solution below Outputs both (as it should, rather than snatch it from the bowels of another procedure). I suggested he simplify out the "random part" and let the user choose if he wants a random element or not. make "alist [1 2 3 2] ;; "PICK" a random index of list make "aitem 1 + random count :alist ;; Show the item picked ;; Yes it's the same as PICK but now you have the index to ;; what was PICKed (important for the next step). show ITEM :aitem :alist 2 ;; We are not picking out the 2 (like REMOVE) we are removing ;; the 4th item (REMOVE/PICK would of given you [1 3 2]) show BUTITEM :aitem :alist [1 2 3] Here is BUTITEM (all comments left out for (Uh-Hmmm) clarity To ButItem :aItem :aList If :aItem = 1 [Output Butfirst :aList] Output (Sentence First :aList ButItem :aItem - 1 ButFirst :aList) End The above concept is very much like the PICK and REMOVE accept that it works on an ordered list with duplicates in addition to sets. You could create a PICKINDEX but that seems overkill. But if you did then you could have something that looks just like the PICK/REMOVE example. show BUTITEM PICKINDEX :alist :alist [1 2 3] (Save the output of PICKINDEX if you need it) Where PICKINDEX would be To PickIndex :aList Output 1 + Random Count :aList End It's also consistent with other functions common to 99% of all logo's: show first :alist 1 show butfirst :alist [2 3 2] and show last :alist 2 show butfirst :alist [1 2 3] show item 4 :alist 2 show butitem 4 :alist [1 2 3] Item function already exists. Is that long winded enough for ya :-) Chuck Shavit wrote: > > Brian Harvey wrote: > > I am beginning to think that what's needed is a new library procedure > > called something like EXTRACT: > > > > to extract :stuff [:index 1+random count :stuff] [:sofar []] > > if :index = 1 [output list (first :stuff) (se (reverse :sofar) (bf > :stuff))] > > output (extract (bf :stuff) (:index-1) (fput first :stuff :sofar)) > > end > > ? > > What do you all think? > > I am a Logo illiterate; My knowledge of Logo lets me understand the clear > programs of Yehuda, but that's about it. Every once in a while I read a > question in Logo-L about list manipulation, and am rather amazed at the > replies. Typically, replies fall in two categories: very long functions, > or two liners (like the one above). To my eyes they all look > incomprehensible, and maybe too complex for what one tries to achieve. > > So my questions to the forum are, can lists be treated as [dynamic] arrays? > What is the difference between a list and an array? If they differ, why > is that difference required? > > The reason I ask is that if one considers a list as an array, then > accessing one or more contiguous elements can be done with great ease, IMO. > Operations like extracting or inserting elements could be done even by > non-Gurus. > > Chuck Shavit > --------------------------------------------------------------- > Please post messages to the Logo forum to logo-l@gsn.org. Mail > questions about the list administration to logofdn@gsn.org. To > unsubscribe send unsubscribe logo-l to majordomo@gsn.org. -- =============================================================== George Mills email: mills@softronix.com http://www.softronix.com The www page contains some very powerful educational software. Our single most important investment is our kids. --------------------------------------------------------------- Please post messages to the Logo forum to logo-l@gsn.org. Mail questions about the list administration to logofdn@gsn.org. To unsubscribe send unsubscribe logo-l to majordomo@gsn.org.
Global SchoolNet Foundation -
Linking Kids Around the World!
Copyright GSN - All Rights Reserved
- Comments
& Questions
Visit GSN's
Global
Schoolhouse for more exciting learning resources!
Search our Site
-
Home