Jump to content

options within a lisp


Rooster

Recommended Posts

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! :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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... :huh:

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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*

Link to comment
Share on other sites

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*

Link to comment
Share on other sites

> ...

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

Link to comment
Share on other sites

> ...

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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...