Input from Parameter
-
- Posts: 20
- Joined: Fri Mar 28, 2008 2:12 am
Input from Parameter
Greetings,
I am trying to come up with a way to get user input in the form of (circle number1 number2) where circle is a defined function and the number1 and number2 are input from the user. I would like number1 and number2 input to go into a (write-line) function that already has characters in it. Here is some of the code I have written thus far.
(define (circle)
(set 'out-file (open "c:/users/name/desktop/circle" "write"))
(write-line "circle number1 number2 " out-file)
(close out-file))
(circle)
Any help would be greatly appreciated.
I am trying to come up with a way to get user input in the form of (circle number1 number2) where circle is a defined function and the number1 and number2 are input from the user. I would like number1 and number2 input to go into a (write-line) function that already has characters in it. Here is some of the code I have written thus far.
(define (circle)
(set 'out-file (open "c:/users/name/desktop/circle" "write"))
(write-line "circle number1 number2 " out-file)
(close out-file))
(circle)
Any help would be greatly appreciated.
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Hi Scott...
Do you mean this sort of thing:
Only the most recent values were stored. If you want to keep a history, use append-file.
Do you mean this sort of thing:
Code: Select all
(define (circle x y)
(set 'out-file (open "circle" "write"))
(write-line (string "circle " x " " y) out-file)
(close out-file))
> (circle 1 2)
true
> (circle 3 4)
true
> (read-file "circle")
"circle 3 4\n"
>
"format" function
the standard way to do variable interpolation inside a given string, as far as I understand, is by using the "format" function.
Code: Select all
(write-line (format "blablabla %s blabla %d" string_var decimal_num_var))
-
- Posts: 20
- Joined: Fri Mar 28, 2008 2:12 am
Re: "format" function
Thanks for the response cormullion and lithper!lithper wrote:the standard way to do variable interpolation inside a given string, as far as I understand, is by using the "format" function.
Code: Select all
(write-line (format "blablabla %s blabla %d" string_var decimal_num_var))
My question now is how do I go about getting input from the user to fill num_var in a defined function?
What would such a function look like?
-
- Posts: 20
- Joined: Fri Mar 28, 2008 2:12 am
Thank you.Lutz wrote:You can use (read-line) to get input from stdin/keyboard. This will work only in a command shell/terminal window, not in the console window of the newLISP-GS Java front-end installed on Windows or Mac OS X.
That gets me heading in the right direction.
Whether gods exist or not, there is no way to get absolute certainty about ethics. Without absolute certainty, what do we do? We do the best we can. --Richard Stallman
-
- Posts: 20
- Joined: Fri Mar 28, 2008 2:12 am
How would I go about handling things that are not digits?Jeff wrote:You will also want to cast them to integers or floats using (int foo) or (float foo).
My latest challenge is writing code that will call a defined function after the user has inputed a certain sting. This is the code I have so far:
Code: Select all
(print "What shape: ")
(set 'answer (read-line))
(if
(answer "draw-circle")
(circle))
Whether gods exist or not, there is no way to get absolute certainty about ethics. Without absolute certainty, what do we do? We do the best we can. --Richard Stallman
Code: Select all
(print "What shape: ")
(set 'answer (read-line))
(if (= answer "draw-circle")
(circle))
Code: Select all
(print "What radius: ")
(set 'answer (read-line))
(if (set 'answer (float answer))
(do-something-with answer)
(begin
(print "What *float* radius: ")
(set 'answer (read-line))))
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Just for fun, here's a gui version of a circle-drawing routine. I was reminded by your posts of the fun we had typing responses to commands, and how complicated the business of getting the right information from a user on the command line could be (decimal commas, exponents, ranges and limits, etc.)
Code: Select all
(load (append (env "NEWLISPDIR") "/guiserver.lsp"))
(gs:init)
(gs:frame 'f 50 50 550 600) ; a window
(gs:set-flow-layout 'f) ; layout type
(gs:canvas 'a-canvas) ; a canvas
(gs:set-background 'a-canvas gs:white)
(gs:set-size 'a-canvas 500 500)
(gs:slider 'radius-slider 'radius-slider-handler "horizontal" 0 800 100) ; a slider
(gs:label 'radius-label {radius}) ; a label
(define (radius-slider-handler id value) ; handler
(cond
((= id "MAIN:radius-slider")
(set 'scale (div (float value) 100))
(gs:set-scale scale scale)
(gs:set-text 'radius-label (string (int (div value 2)) { pixels}))
(gs:update))))
; assemble
(gs:add-to 'f 'a-canvas 'radius-slider 'radius-label)
(gs:set-translation 250 250)
(set 'circle-radius 30)
(gs:draw-circle 'circle 0 0 circle-radius)
(gs:set-visible 'f true)
; run
(gs:listen)
-
- Posts: 20
- Joined: Fri Mar 28, 2008 2:12 am
Jeff,
Thank you! That info was a big help.
Cormullion,
Thanks for sharing the GUI code. Even though it looks quite complex, it is remarkable to me how readable it is.
Thank you! That info was a big help.
Cormullion,
Thanks for sharing the GUI code. Even though it looks quite complex, it is remarkable to me how readable it is.
Whether gods exist or not, there is no way to get absolute certainty about ethics. Without absolute certainty, what do we do? We do the best we can. --Richard Stallman