Input from Parameter

Started by scottmaccal, April 02, 2008, 11:19:11 AM

Previous topic - Next topic

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

cormullion

#1
Hi Scott...



Do you mean this sort of thing:


(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 4n"
>


Only the most recent values were stored. If you want to keep a history, use append-file.

lithper

#2
the standard way to do variable interpolation inside a given string, as far as I understand, is by using the "format" function.


(write-line (format "blablabla %s blabla %d" string_var decimal_num_var))

scottmaccal

#3
Quote from: "lithper"the standard way to do variable interpolation inside a given string, as far as I understand, is by using the "format" function.


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

Lutz

#4
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

#5
Quote from: "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.


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

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



http://artfulcode.net\">Artful code

scottmaccal

#7
Quote from: "Jeff"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:


(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

#8
(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:


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



http://artfulcode.net\">Artful code

cormullion

#9
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.)


(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

#10
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