Rooster Posted June 19, 2009 Posted June 19, 2009 New to writing LISPS, so please be patient & treat me like an idiot with no knowledge at all!! I'm writing a simple LISP. I've got to a point where I need to get input from the user as to which of 4 options they want, and to then run the chosen option. Could someone please help me out with the mechanics of doing this? Here's what I want to do in normal language, which I need translating into LISP language: -ask user for choice: option1, option2, option3, or option4 (if none of these are chosen then it needs to ask the question again until a valid choice is made) -if choice=option1 then do O1 -if choice=option2 then do O2 -if choice=option3 then do O3 -if choice=option4 then do O4 Hope that makes sense. Please ask if it doesn't! Quote
The Buzzard Posted June 19, 2009 Posted June 19, 2009 New to writing LISPS, so please be patient & treat me like an idiot with no knowledge at all!! I'm writing a simple LISP. I've got to a point where I need to get input from the user as to which of 4 options they want, and to then run the chosen option. Could someone please help me out with the mechanics of doing this? Here's what I want to do in normal language, which I need translating into LISP language: -ask user for choice: option1, option2, option3, or option4 (if none of these are chosen then it needs to ask the question again until a valid choice is made) -if choice=option1 then do O1 -if choice=option2 then do O2 -if choice=option3 then do O3 -if choice=option4 then do O4 Hope that makes sense. Please ask if it doesn't! I think this is better. (cond ((= OPT "1")(Then DO 1)) ((= OPT "2")(Then DO 2)) ((= OPT "3")(Then DO 3)) ((= OPT "4")(Then DO 4)) ) Quote
The Buzzard Posted June 19, 2009 Posted June 19, 2009 I think this is better. (cond ((= OPT "1")(Then DO 1)) ((= OPT "2")(Then DO 2)) ((= OPT "3")(Then DO 3)) ((= OPT "4")(Then DO 4)) ) With regard to the (Then DO 1)) etc. Not sure if you want to go to another function or you want to set a variable or whatever. If you post the code I would have a better idea. Quote
The Buzzard Posted June 19, 2009 Posted June 19, 2009 You can also change the OPT to CHOICE, Its upto you. Quote
Rooster Posted June 19, 2009 Author Posted June 19, 2009 If you post the code I would have a better idea. Thanks Buzzard - the code isn't written yet though! I guess what I have in mind is to have 4 separate functions and for the user input to dictate which of the 4 functions gets run. What do I need to do to ask the user for their option? (ie. for the question to appear on screen, for the LISP to pause for input, and to then assign that input to the variable which tells the LISP which function to run) Quote
The Buzzard Posted June 19, 2009 Posted June 19, 2009 Thanks Buzzard - the code isn't written yet though! I guess what I have in mind is to have 4 separate functions and for the user input to dictate which of the 4 functions gets run. What do I need to do to ask the user for their option? (ie. for the question to appear on screen, for the LISP to pause for input, and to then assign that input to the variable which tells the LISP which function to run) Something like this: (setq OPT "1") (setq OPT (getstring "\nSelect an option 1-4: < 1 > ")) Quote
The Buzzard Posted June 19, 2009 Posted June 19, 2009 Thanks Buzzard - the code isn't written yet though! I guess what I have in mind is to have 4 separate functions and for the user input to dictate which of the 4 functions gets run. What do I need to do to ask the user for their option? (ie. for the question to appear on screen, for the LISP to pause for input, and to then assign that input to the variable which tells the LISP which function to run) And something like this: (cond ((= OPT "1")(OPT1)) ;GOTO FUNCTION OPT1 ((= OPT "2")(OPT2)) ;GOTO FUNCTION OPT2 ((= OPT "3")(OPT3)) ;GOTO FUNCTION OPT3 ((= OPT "4")(OPT4)) ;GOTO FUNCTION OPT4 ) Quote
Lee Mac Posted June 19, 2009 Posted June 19, 2009 Something like this: (setq OPT "1") (setq OPT (getstring "\nSelect an option 1-4: < 1 > ")) It is better to use the getkword function. There are many ways to accomplish this task. The simplest version: (initget 1 "Yes No") (setq ans (getkword "\nYes or No? :")) But if you wanted a default: (initget "Yes No") (setq ans (getkword "\nYes or No? <Yes> :")) (and (not ans) (setq ans "Yes")) But this can be made more elaborate in many ways.... You can use a WHILE loop to filter incorrect entries: (while (progn (setq ans (cond ((getint "\nSpecify Number [1/2/3] <3>: ")) (3))) (if (not (vl-position ans '(1 2 3))) (princ "\nIncorrect Selection!")))) Or, you could use a GLOBAL variable to hold the default value: (or *ans* (setq *ans* "A")) (initget "A B C") (or (not (setq ans (getkword (strcat "\nSelect a Letter [A/B/C] <" *ans* ">: ")))) (setq *ans* ans)) Just providing a few options - there are loads of ways to do this Lee 1 Quote
BIGAL Posted June 22, 2009 Posted June 22, 2009 If you have a pull down menu or tool bar you can code the option number into it as the command ^c^c(setq opt 1)(load "mymassivelisp") Quote
Lee Mac Posted June 22, 2009 Posted June 22, 2009 If you have a pull down menu or tool bar you can code the option number into it as the command^c^c(setq opt 1)(load "mymassivelisp") This would leave the variable "opt" as a global variable - surely not what you want... Quote
Se7en Posted June 23, 2009 Posted June 23, 2009 New to writing LISPS, so please be patient & treat me like an idiot with no knowledge at all!! I'm writing a simple LISP. I've got to a point where I need to get input from the user as to which of 4 options they want, and to then run the chosen option. Could someone please help me out with the mechanics of doing this? Here's what I want to do in normal language, which I need translating into LISP language: -ask user for choice: option1, option2, option3, or option4 (if none of these are chosen then it needs to ask the question again until a valid choice is made) -if choice=option1 then do O1 -if choice=option2 then do O2 -if choice=option3 then do O3 -if choice=option4 then do O4 Hope that makes sense. Please ask if it doesn't! You maybe new but you touched on a pretty advanced topic (That's awesome! You will be a very good coder very soon). First, when you write down what you want in `normal language' it is called developing ``Pseudo code''. Very, very important step. Do that all the time for everything. Second, I have recently gotten the time to finish research/writeup on this very topic and I discovered that an uber someone in the programming world (Paul Graham) called the problem the `Anaphoric IF'. This was a very fun puzzle for me. I got *hours* of thought out of it and would love to have a discussion about it if you want. ...but at any rate, here is the answer in case you ``dont have the time right now to discuss it'' or ``just need the answer''. In the chance that you do have `the time' or `want' to discuss, let me know! (initget 0 "Length Width Depth Time") (setq Inp (cond ( (getkword "\nSpecify a dimension [Length/Width/Depth/Time] <Time>: ") ) ("Time")) ) Quote
Rooster Posted June 24, 2009 Author Posted June 24, 2009 Wow - thanks for all the replies. It's pretty hard for me to see which one is most appropriate for me though! To explain what I'm trying to do a little more, I want to ask the user what scale they will be plotting the drawing at (choose from 1:50, 1:100, 1:200, or 1:500). So after asking the user to choose from these four options I then want to go to a say 1:200 section in the LISP and run that part, while ignoring the 1:50, 1:100 & 1:500 parts. With my limited LISP knowledge, I'm struggling to pick out from all the replies which solution to try out! Quote
Lee Mac Posted June 24, 2009 Posted June 24, 2009 Something like this, following the code provided by Se7en: (initget "1:50 1:100 1:200 1:500") (setq uAns (cond ((getkword "\nSelect Scale 1:[50/100/200/500] <1:50> : ")) ("1:50"))) (cond ((eq uAns "1:50") ... Do something ... ) ; End condition 1 ((eq uAns "1:100") ... Do something ... ) ; End condition 2 etc... ) ; End COND Quote
Rooster Posted June 24, 2009 Author Posted June 24, 2009 that looks like what I want - thanks leemac. i'll try it out.... Quote
Se7en Posted June 24, 2009 Posted June 24, 2009 I'm confused by the logic of the last few posts. I thought this was a: If I get an item put it away. type of problem. not a: If I get an item: if it is a canned good put it on the top shelve, if it is a perishable put it on the bottom shelve, if it is a fruit put it on the counter.... type of problem. If it is the latter, then i suggest a rethink of the program layout/flow/design. Quote
Lee Mac Posted June 24, 2009 Posted June 24, 2009 John, I don't follow your metaphor - LISP is complicated enough for most, without using metaphors... Quote
Se7en Posted June 24, 2009 Posted June 24, 2009 I think your wrong; the code is the easy part, its the logic that is difficult. ...let me try to explain that a different way: Programing languages use the English language. The English language has a structure --verbs, nouns, pronouns, etc-. Words makeup sentences. Sentences make up paragraphs. ...If your sentences are all goofed up you will have a messed up paragraph which doesnt make sense. Where people have the real problem is when they dont think the sentence thru and they get stuck cobbling together verbs to try and make it make sense in the paragraph, etc.. My first sentence makes a sentence; easy to understand, clear, and concise. Try and make a sentence out of the second. Difficult? ...bad design. I see two `problems': "IT" and "AWAY". I have given you a way to define "IT" now you define "AWAY". Once we have the scale what are we going to be doing? For a 1:200 condition we are going to draw a line? for a 1:50 condition we are going to plot the drawing? ...i dont understand the logic. *smile* Quote
Lee Mac Posted June 24, 2009 Posted June 24, 2009 Once we have the scale what are we going to be doing? For a 1:200 condition we are going to draw a line? for a 1:50 condition we are going to plot the drawing? ...i dont understand the logic. I can see what you are saying here - but this information was not given - hence I was just providing my method with the information that was given to me. *cheeky grin* Quote
Se7en Posted June 24, 2009 Posted June 24, 2009 > ... > but this information was not given - hence I was just > providing my method with the information that was given > ... And hence my reason for questioning the logic and offering my opinion. So Rooster, now its your turn. Lead us. Quote
Rooster Posted June 25, 2009 Author Posted June 25, 2009 > ...> but this information was not given - hence I was just > providing my method with the information that was given > ... And hence my reason for questioning the logic and offering my opinion. So Rooster, now its your turn. Lead us. LOL - I think I'm lost.... The intention is that the lisp will get from the user what scale the drawing will be plotted at, and then change certain text heights accordingly. So if 1:500 is chosen, the text heights need to be larger so that they can be read once plotted. At 1:50 you don't want loads of huge text because it's not necessary. Whatever the choice is, the next step will be the same, just with different text heights. Does that help??! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.