Input from Parameter

Notices and updates
Locked
scottmaccal
Posts: 20
Joined: Fri Mar 28, 2008 2:12 am

Input from Parameter

Post by scottmaccal »

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.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Hi Scott...

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"
>
Only the most recent values were stored. If you want to keep a history, use append-file.

lithper
Posts: 39
Joined: Sun Feb 24, 2008 12:58 am
Location: USA

"format" function

Post by lithper »

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

scottmaccal
Posts: 20
Joined: Fri Mar 28, 2008 2:12 am

Re: "format" function

Post by scottmaccal »

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))
Thanks for the response cormullion and lithper!

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?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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.

scottmaccal
Posts: 20
Joined: Fri Mar 28, 2008 2:12 am

Post by scottmaccal »

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

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

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

You will also want to cast them to integers or floats using (int foo) or (float foo).
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

scottmaccal
Posts: 20
Joined: Fri Mar 28, 2008 2:12 am

Post by scottmaccal »

Jeff wrote:You will also want to cast them to integers or floats using (int foo) or (float foo).
How would I go about handling things that are not digits?

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))
Could you point me 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

Jeff
Posts: 604
Joined: Sat Apr 07, 2007 2:23 pm
Location: Ohio
Contact:

Post by Jeff »

Code: Select all

(print "What shape: ")
(set 'answer (read-line))

(if (= answer "draw-circle")
  (circle))
That would make *that* work properly. What I was talking about was that read-line interns strings. After (set 'answer (read-line)), answer now contains a string. If you were accepting, for example, a radius for a circle that would get used as a float, you would do:

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))))
Jeff
=====
Old programmers don't die. They just parse on...

Artful code

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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)

scottmaccal
Posts: 20
Joined: Fri Mar 28, 2008 2:12 am

Post by scottmaccal »

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

Locked