Newbie looking for guidance
Posted: Mon Sep 18, 2006 4:45 pm
So let me start by introducing myself: I come from a HyperTalk background mostly, working with HyperCard, SuperCard, and Revolution. I've also worked with FileMaker and 4D, and in the distant past C, Pascal, and way back when BASIC.
I've always been fascinated by LISP, so here I am.
I have several questions:
1. Forgive me for being dense, but I'm having a hard time saving and retrieving source code. I wrote a few lines (referenced below) and I thought I had saved them in much the same way the example files are saved -- drag.lsp for example -- but when I try to reload the file, I get an error:
symbol is protected in function define : (exit)
When I open my saved file in a text editor, I see a bunch of things I didn't type in it. Is there a simple tutorial that walks through creating a simple application, step-by-step?
----------------
2. I'm trying to write a solution to a problem I'm working on as a way of getting started with newLISP. The problem is a puzzle (sorry the description is so long):
A warden says to 100 prisoners, "Next week I'm going to assemble 100 numbered boxes and put your names in them randomly, one to a box. At that point I'll lock you all in solitary (no communication of any sort) and let you into the room with the boxes one by one. Each of you will get to open 90 boxes, hoping to find his own name. Then the boxes will be closed and you will be returned to solitary, so another prisoner can try. If all 100 of you find your own names, you can go free."
Stop reading here if you don't want to know the answer to the puzzle.
By random chance, any prisoner has a 0.9 probability of finding his own name. That means that by random chance with no strategy, 100 prisoners have 0.9^100 = 0.000027 probability of succeeding -- not very good. But there is an answer.
Last chance to stop reading.
The prisoners agree to a random distribution of their names against the 100 boxes before they are moved to solitary. So each prisoner has a number, and knows the numbers of all the other prisoners as well. Let's say my number is 1. When it's my turn to try I open box 1. If it has my name in it, great. Otherwise I look and the name, and using the assignments we previously agreed to, I open _that_ person's number. I repeat this process until either I've opened 90 boxes and failed, or I've succeeded.
Believe it or not, this gives the prisoners about an 85% chance of success. I was stunned by this result. I can explain it if anyone cares. But for now, I just wanted to program a test of this in newLISP. So far I have this:
(define (rsequence x y z) (randomize (sequence x y (if z z 1))))
(setq warden (rsequence 0 99) prisoners (rsequence 0 99))
(set 'prisonerArray (array 100))
(map (fn (x y) (nth-set (prisonerArray x) y)) prisoners warden)
_If_ I understand correctly:
-- The first line gives me random sequences of numbers.
-- The second line sets up the warden's distribution, and the prisoners'.
-- The third line initializes an array of 100 elements. The array will be used to store the number of the next box, by the prisoners' and the warden's sequences, for each of the 100 boxes.
-- The fourth line populates the array with the correct values.
So here are my questions:
2a: Does the above do what I think it does?
2b: How would I go about verifying that? If I just wanted to dump the values of the array at the end, how would I do it?
2c: Is the above a reasonable way to start? Is there a better way?
The array generated by the warden's and the prisoners' distribution of the names into boxes naturally forms loops. Those loops have a length, which is critical to solving the puzzle. If no loop thus generated is longer than 90 elements, then the prisoners win. If the longest loop is greater than 90 elements long, the prisoners lose. So:
2d: My next step is to figure out the loops based on the array. In english that might work out to something like:
-- for each number (X) from 0 to 99
-- if X is already in a loop, skip to the next number
-- using the array, figure out what sequence of numbers the loop starting with X contains.
I have no idea how to code the above. Any suggestions?
Thanks to all,
regards,
Geoff
I've always been fascinated by LISP, so here I am.
I have several questions:
1. Forgive me for being dense, but I'm having a hard time saving and retrieving source code. I wrote a few lines (referenced below) and I thought I had saved them in much the same way the example files are saved -- drag.lsp for example -- but when I try to reload the file, I get an error:
symbol is protected in function define : (exit)
When I open my saved file in a text editor, I see a bunch of things I didn't type in it. Is there a simple tutorial that walks through creating a simple application, step-by-step?
----------------
2. I'm trying to write a solution to a problem I'm working on as a way of getting started with newLISP. The problem is a puzzle (sorry the description is so long):
A warden says to 100 prisoners, "Next week I'm going to assemble 100 numbered boxes and put your names in them randomly, one to a box. At that point I'll lock you all in solitary (no communication of any sort) and let you into the room with the boxes one by one. Each of you will get to open 90 boxes, hoping to find his own name. Then the boxes will be closed and you will be returned to solitary, so another prisoner can try. If all 100 of you find your own names, you can go free."
Stop reading here if you don't want to know the answer to the puzzle.
By random chance, any prisoner has a 0.9 probability of finding his own name. That means that by random chance with no strategy, 100 prisoners have 0.9^100 = 0.000027 probability of succeeding -- not very good. But there is an answer.
Last chance to stop reading.
The prisoners agree to a random distribution of their names against the 100 boxes before they are moved to solitary. So each prisoner has a number, and knows the numbers of all the other prisoners as well. Let's say my number is 1. When it's my turn to try I open box 1. If it has my name in it, great. Otherwise I look and the name, and using the assignments we previously agreed to, I open _that_ person's number. I repeat this process until either I've opened 90 boxes and failed, or I've succeeded.
Believe it or not, this gives the prisoners about an 85% chance of success. I was stunned by this result. I can explain it if anyone cares. But for now, I just wanted to program a test of this in newLISP. So far I have this:
(define (rsequence x y z) (randomize (sequence x y (if z z 1))))
(setq warden (rsequence 0 99) prisoners (rsequence 0 99))
(set 'prisonerArray (array 100))
(map (fn (x y) (nth-set (prisonerArray x) y)) prisoners warden)
_If_ I understand correctly:
-- The first line gives me random sequences of numbers.
-- The second line sets up the warden's distribution, and the prisoners'.
-- The third line initializes an array of 100 elements. The array will be used to store the number of the next box, by the prisoners' and the warden's sequences, for each of the 100 boxes.
-- The fourth line populates the array with the correct values.
So here are my questions:
2a: Does the above do what I think it does?
2b: How would I go about verifying that? If I just wanted to dump the values of the array at the end, how would I do it?
2c: Is the above a reasonable way to start? Is there a better way?
The array generated by the warden's and the prisoners' distribution of the names into boxes naturally forms loops. Those loops have a length, which is critical to solving the puzzle. If no loop thus generated is longer than 90 elements, then the prisoners win. If the longest loop is greater than 90 elements long, the prisoners lose. So:
2d: My next step is to figure out the loops based on the array. In english that might work out to something like:
-- for each number (X) from 0 to 99
-- if X is already in a loop, skip to the next number
-- using the array, figure out what sequence of numbers the loop starting with X contains.
I have no idea how to code the above. Any suggestions?
Thanks to all,
regards,
Geoff