newLISP Fan Club

Forum => newLISP newS => Topic started by: scottmaccal on April 02, 2008, 11:19:11 AM

Title: Input from Parameter
Post by: scottmaccal on April 02, 2008, 11:19:11 AM
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.
Title:
Post by: cormullion on April 02, 2008, 11:45:26 AM
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.
Title: "format" function
Post by: lithper on April 02, 2008, 04:41:38 PM
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))
Title: Re: "format" function
Post by: scottmaccal on April 02, 2008, 06:07:50 PM
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?
Title:
Post by: Lutz on April 02, 2008, 06:26:37 PM
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.
Title:
Post by: scottmaccal on April 03, 2008, 04:53:57 PM
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.
Title:
Post by: Jeff on April 03, 2008, 05:17:08 PM
You will also want to cast them to integers or floats using (int foo) or (float foo).
Title:
Post by: scottmaccal on April 04, 2008, 06:49:05 AM
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?
Title:
Post by: Jeff on April 04, 2008, 07:39:33 AM
(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))))
Title:
Post by: cormullion on April 04, 2008, 10:04:54 AM
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)
Title:
Post by: scottmaccal on April 04, 2008, 05:18:21 PM
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.