[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

LOGO-L> eliminating random items from a list



Thank you to Frank Caggiano and George Mills for the procedures you
provided to help me select a random element and then eliminate it from a
list.

I have tested both suggestions using my list of procedure names, and I must
say that George Mills' method is the clear winner (after a glitch in
George's method was corrected by Frank).  I am mystified as to why I get
the results that I get using Frank's method.  I have run it repeatedly and
always get some form of this result, with the first elements of the list
becoming the first letter of the chosen item instead of remaining words.
Here is a sample of output from each method (followed after both by the
procedures used to create each):

Frank's method:

m m m m m printerQ keyboardQ mouseQ scannerQ speakersQ
k k k k k k mouseQ scannerQ speakersQ
k k k k k mouseQ scannerQ speakersQ
k k k k mouseQ scannerQ speakersQ
k k k mouseQ scannerQ speakersQ
s s s s speakersQ
s s s s
s s s
s s
s

George's method:

cdQ diskQ CPUQ monitorQ microphoneQ modemQ keyboardQ mouseQ scannerQ speakersQ
cdQ diskQ CPUQ microphoneQ modemQ keyboardQ mouseQ scannerQ speakersQ
cdQ diskQ CPUQ microphoneQ keyboardQ mouseQ scannerQ speakersQ
cdQ diskQ CPUQ microphoneQ keyboardQ scannerQ speakersQ
diskQ CPUQ microphoneQ keyboardQ scannerQ speakersQ
diskQ CPUQ microphoneQ keyboardQ speakersQ
diskQ CPUQ microphoneQ keyboardQ
diskQ CPUQ microphoneQ
diskQ microphoneQ
diskQ


Frank's method:

to remItemFrom :anItem :aList
        ;; empty listp output nil
        if empty? :aList [op [] ]

        ;; if anItem isn't in aList output  aList
        if not member? :anItem :aList [op :aList]

        ;; if anItem is the first element in aList output the butfirst of aList
        if equal? :anItem first :aList [op bf :aList]

        ;; other wise create a list (with se) with the first element in aList
        ;; and call us again with the rest of the list.
        op (se first :aList remItemFrom :anItem bf :aList)
end

to remRandomFrom :aList
        ;; first pick the element to remove

        make "anItem pick :aList

        ;; now remove it

        op remItemFrom :anItem :aList
end


George's method:

to TrueRemItemFrom :anItem :aList
   ;; If we have shifted the list down such that the first element
   ;; is the one to be removed then do it
   if :anItem = 1 [op butfirst :aList]

   ;; Shift the element we want to keep over and continue looking
   op (se first :aList TrueRemItemFrom :anItem - 1 butfirst :aList)
end

to TrueRemRandomFrom :aList
   ;; Pick a random POSITION within the list
   make "item (random (count :aList) ) + 1

   ;; Remove the element at that position
   op TrueRemItemFrom :item :aList
end

George had supplied this testing procedure:

to testrem
   ;; Test old method
   make "foo [3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3]
   repeat count :foo [make "foo remRandomFrom :foo show :foo]

   ;; Test new method
   make "foo [3 3 3 3 3 1 3 3 3 3 3 3 3 3 3 3 3]
   repeat count :foo [make "foo TrueRemRandomFrom :foo show :foo]
end

I modified it to include words instead of numbers, with the results I
provided at the top.  I guess this is evidence that sometimes we need to
test something in more than one way.  By the way, I also realized after
getting these replies that I should have specified that I was using
MicroWorlds 2.0 for Mac, but these procedures worked.  I was very
surprised, because NOTHING in the MicroWorlds reference manual indicates
that OP (output) can be used in this way.  I think LCSI's MicroWorlds is
wonderful but their support materials are entirely inadequate!  Here, in
its entirety (between the stars * * *) , is what the manual says about
OUTPUT:

* * *
output (op)  (word-or-list)
Stops the procedure and reports a word or list.  Output can only be used
within a procedure.  Use output to make reporter procedures.

This procedure takes a word as input.  It reports the first number
appearing in the word.
to firstnumber :word
if empty?  :word  [output "none]
if number?  first  :word  [output first  :word]
output firstnumber bf  :word
end

Try the procedure from the Command Center.
show firstnumber "abc6ef5g
6
If a text box Text1 contains the sentence "You got the 5th best score":
show firstnumber text1
5
* * *

I must confess that I'm still not sure if OP eliminates the item or if it
is the particular way in which it is used to reconstruct the list which
does the trick.  I usually can comprehend logo code pretty well, but this
is unfamiliar terrain for me.  I feel like a foreign traveller who has been
told what to say in a situation, and says it, and gets results, without any
real comprehension of the words.  As a result of not really understanding,
I made some mistakes at first.

(For instance, I had originally set up
make "questions [cdQ diskQ etc.] in my larger procedure, so I thought that
wherever I saw the word  :alist  I should substitute :questions in creating
the procedure, but when that didn't work, I then realized that my
:questions corresponded to :foo, that these procedures will work now with
any list and that I only use  :questions  in running the procedure, not
creating it.  As another example, I originally had different names for my
procedures:  Q.cd-rom.drive, Q.disk.drive, etc., and then when Frank's
method kept giving me output like   Q Q Q Q Q Q Q Q.modem Q.microphone, I
somehow, scratching my head, thought that maybe the Q followed by a period
was causing the problem.  After I changed the procedure names, I realized
that the code was somehow eliminating all but the first letter of the
selected word and then substituting that letter for the preceding words.  I
did manage to incorporate the procedures into my project to accomplish my
goal, which was not merely to eliminate one item at a time but to first run
that chosen procedure and then eliminate it from the list.  I just needed
to say

TrueRemRandomFrom  :questions
run :item

and the procedure name stored as "item  would be executed from within my
larger recursive procedure.)

If anyone could shed a bit more explanatory light in addition to the
notations included with the procedures, or explain why Frank's did what it
did, I'd be very grateful.

And don't you think that reference manuals should give more examples in a
variety of situations to illustrate the benefits of a particular command???

Thanks again for the invaluable help.

Wendy Petti
comp3@wis.edu   or   wap3@cornell.edu





---------------------------------------------------------------
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