Tell me how I talk funny
Posted: Tue Sep 19, 2006 7:37 pm
So, I've successfully written my first newLISP routine (it's short -- does it qualify as a program??) Hooray!
Coming from a background of HyperTalk, I'm pretty sure I speak newLISP with a terrible accent. Here's the code I wrote, please tell me the write way to do it. Feel free to bash away...
For those who are interested, the problem description is posted in my other thread: Newbie looking for guidance. Briefly, the below generates a random list for both a warden and his prisoners, representing boxes the warden has set up with the prisoners' names in them. The prisoners' goal is to have all of them independently find their names within a certain number of tries. The code below uses a strategy that maximizes the prisoners' chances of success, and reports whether the method would have succeeded or not.
The equivalent code in Revolution (a HyperTalk variant) can also be found in the other thread: roughly 20 lines of code there becomes roughly 14 of the lines below.
Coming from a background of HyperTalk, I'm pretty sure I speak newLISP with a terrible accent. Here's the code I wrote, please tell me the write way to do it. Feel free to bash away...
For those who are interested, the problem description is posted in my other thread: Newbie looking for guidance. Briefly, the below generates a random list for both a warden and his prisoners, representing boxes the warden has set up with the prisoners' names in them. The prisoners' goal is to have all of them independently find their names within a certain number of tries. The code below uses a strategy that maximizes the prisoners' chances of success, and reports whether the method would have succeeded or not.
The equivalent code in Revolution (a HyperTalk variant) can also be found in the other thread: roughly 20 lines of code there becomes roughly 14 of the lines below.
Code: Select all
#choose how many prisoners, and the measure of success
(set 'prisonerCount 100 'prisonerGoal 90)
#set up random lists for the warden and the prisoners
(set 'warden (randomize (sequence 0 (- prisonerCount 1)))
'prisoners (randomize (sequence 0 (- prisonerCount 1))))
#initialize an array that will store how the two lists map to each other
(set 'prisonerArray (array prisonerCount))
#populate the array with entries.
#the index is the number from the prisoners' list
#the value is the number from the warden's list
(map (fn (x y) (nth-set (prisonerArray x) y)) prisoners warden)
#initialize the results lists
(set 'loopList '() 'prisonerDoneList (array prisonerCount))
#for each prisoner entry
(dolist (i prisoners)
#if the prisoner has already been included in a list, go next
(until (prisonerDoneList i)
#mark the prisoner as done
(nth-set (prisonerDoneList i) 1)
#find what is in the prisoner's box,
#and start a list with the prisoner
(set 'x (prisonerArray i) 'oneLoop (list i))
#repeat until we find a loop
(until (= x i)
#put the new box at the end of the list
(push x oneLoop -1)
#mark the new prisoner/box as done
(nth-set (prisonerDoneList x) 1)
#move on to the next box in the loop
(set 'x (prisonerArray x)))
#when we have a complete loop,
#add it to the list of loops
(push oneLoop loopList -1)))
#create a sorted list with the lengths of the loops
#any way to reverse sort this?
(set 'loopCount (sort(map length loopList)))
#print the results
(println warden)
(println prisoners)
(println loopList)
(println loopCount)
(println (if (> (last loopCount) prisonerGoal)
"BOO -- The prisoners LOSE!"
"HOORAY -- The prisoners WIN!"))