Author Topic: More subroutine functions  (Read 9751 times)

0 Members and 1 Guest are viewing this topic.

Daniel J. Ellis

  • Swamp Rat
  • Posts: 809
More subroutine functions
« on: March 19, 2009, 10:03:57 AM »
I currently have a (working) routine that goes:
 
Code: [Select]
(DEFUN startnum ( / )

(SETQ
start (GETINT "\nEnter first plot to renumber<1>:")
stop (GETINT "\nEnter last plot to renumber<1000>:")
step (GETINT "\nEnter amount to increase each number by (negative values decrease)<1>:")
)
(IF
(NOT start)
(SETQ start 1)
)
(IF
(NOT stop)
(SETQ stop 1000)
)
(IF
(NOT step)
(SETQ step 1)
)
)

I'm trying to simplify it to:
Code: [Select]
(DEFUN startnum ( / )
(DEFUN checkdefault ( var val / )

(IF
(NOT (VL-PRINC-TO-STRING (EVAL var)))
(SETQ var val)
)
(PRINC "\nIn CHECKDEFAULT")
(RETURNVARS 'var)
)
(SETQ
start (GETINT "\nEnter first plot to renumber<1>:")
stop (GETINT "\nEnter last plot to renumber<1000>:")
step (GETINT "\nEnter amount to increase each number by (negative values decrease)<1>:")
)



(CHECKDEFAULT 'start 1)
(CHECKDEFAULT 'stop 1000)
(CHECKDEFAULT 'step 1)

)

However, this one doesn't work.

Can anybody suggest why, or what I should be doing instead

dJE

<edit: fixed code tags>
« Last Edit: March 19, 2009, 10:07:01 AM by CAB »
===
dJE

gile

  • Water Moccasin
  • Posts: 2275
  • Marseille, France
Re: More subroutine functions
« Reply #1 on: March 19, 2009, 12:12:08 PM »
Hi,

You can use an 'OR' statment

Code: [Select]
(defun StartNum ()
  (or
    (setq start (getint "\nEnter first plot to renumber<1>:"))
    (setq start 1)
  )
  (or
    (setq stop (getint "\nEnter last plot to renumber<1000>:"))
    (setq stop 1000)
  )
  (or
    (setq step (getint"\nEnter amount to increase each number by (negative values decrease)<1>:"))
    (setq step 1)
  )
)

As the 3 expression are quite similar, you can group them in a 'MAPCAR' statment

Code: [Select]
(defun StartNum ()
  (mapcar
    '(lambda (sym val msg)
       (or (set sym (getint (strcat msg "<" (itoa val) ">:")))
   (set sym val)
       )
     )
    '(start stop step)
    '(1 1000 1)
    '("\nEnter first plot to renumber"
      "\nEnter last plot to renumber"
      "\nEnter amount to increase each number by (negative values decrease)"
     )
  )
)
Speaking English as a French Frog

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #2 on: March 19, 2009, 12:17:37 PM »
Use COND instead of OR
(setq <var> (cond <blah> <return>))
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: More subroutine functions
« Reply #3 on: March 19, 2009, 12:57:29 PM »
Nice example Gile.
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Daniel J. Ellis

  • Swamp Rat
  • Posts: 809
Re: More subroutine functions
« Reply #4 on: March 19, 2009, 04:16:22 PM »
Use COND instead of OR
(setq <var> (cond <blah> <return>))

I thought COND worked in a "pick from a list" kinda way
If item 1, do x
if item 2, do y
if item 3, do z?

What I'm doing here is getting three values, but offering a default for each.

MAPCAR looks the way to go, thanks Giles

dJE
===
dJE

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #5 on: March 19, 2009, 05:44:22 PM »
No look them both up and look at the returns of each. OR returns either nil or t. ...Would you like an example instead?

Code: [Select]
(setq result
       (cond
( (getint "\nSpecify a value <7>: ") )
(7) )
      )
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

kdub

  • Mesozoic keyThumper
  • SuperMod
  • Swamp Rat
  • Posts: 1387
  • class keyThumper<T>:ILazy<T>
Re: More subroutine functions
« Reply #6 on: March 19, 2009, 05:55:39 PM »
I'd use an OR construct similar to the one posted by gile.

...the reader just needs to glance at it to understand the functionality.


 
I currently have a (working) routine that goes:
 
Code: [Select]
(DEFUN startnum ( / )

(SETQ
start (GETINT "\nEnter first plot to renumber<1>:")
stop (GETINT "\nEnter last plot to renumber<1000>:")
step (GETINT "\nEnter amount to increase each number by (negative values decrease)<1>:")
)
(IF
(NOT start)
(SETQ start 1)
)
(IF
(NOT stop)
(SETQ stop 1000)
)
(IF
(NOT step)
(SETQ step 1)
)
)

I'm trying to simplify it to: ...

dan, how about something like :-

Code: [Select]
;; ..... < preamble >

(or  start
      (SETQ start 1)
)
(or stop
      (SETQ stop 1000)
)
(or step
      (SETQ step 1)
)





called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: More subroutine functions
« Reply #7 on: March 19, 2009, 06:02:49 PM »
Another spin ... dumbed down (i.e. no initget argument etc.) ...

Code: [Select]
(defun _GetInt ( pmt default / result )

    (setq default (if (numberp default) (fix default) 0))
   
    (if (setq result (getint (strcat pmt " <" (itoa default) ">: ")))
        result
        default
    )

)

Code: [Select]
(setq
    start (_GetInt "Enter first plot to renumber" 1)
    stop  (_GetInt "Enter last plot to renumber" 1000)
    step  (_GetInt "Enter increment value (negative to decrement)" 1)
)

Different variant illuminating what Se7en was trying to demonstrate:

Code: [Select]
(defun _GetInt ( pmt default )

    (setq default (if (numberp default) (fix default) 0))
   
    (cond
        ((getint (strcat pmt " <" (itoa default) ">: ")))
        (default)
    )

)

Subtitle: Code reuse. :)
Engineering Technologist CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com http://cadanalyst.slack.com http://linkedin.com/in/cadanalyst

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #8 on: March 19, 2009, 11:05:40 PM »
I'd use an OR construct similar to the one posted by gile.

...the reader just needs to glance at it to understand the functionality.


Then you would just be perpetuating the misuse of code.

Then the reader needs to be better informed because its goofy and confusing. I think you like the use of OR because it seems like a fancy trick but if you stop and look at OR vs COND in this instance you will notice something...You know what? I'll just show you cause im sick of this crap.

OR statement (Lets remove the extra stuff and get down to the basics):
(or (setq var (getint "\nPrompt"))
   (setq var <DEFAULT>)
 )
Step thru that eval sequence.
1. run getint function
2. fill variable with result of getint function (worst case: nil).
3. if variable is nill then evaluate next process which is setting the variable to something else.
4. Return a Boolean value.

Now lets look at COND:
(setq var (cond ((getint "\nPrompt")) (<DEFAULT>) )
1. run getint function
2. if nil evaluate next (which is just a value)
3. return that value to fill the variable.

Do you see how it all plays out now?

OR can be good for running several different scenarios of process' not setting variables the statement is confusing.
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10395
Re: More subroutine functions
« Reply #9 on: March 20, 2009, 12:15:52 AM »
In the case you presented (setq var (cond
I do prefer that method but OR has it uses and don't be so rigid with the language that
creative applications of the language are rejected. As long as the intent of the code is
not obfuscated to the average programmer I don't see the harm. But then I'm a self taught
programmer and that's where I'm coming from.

To me the OR is short hand for (if (not ) then (if (not ....
Therefore if you don't care or don't need more than the return value (T or nil) then go ahead and use it.

Just my 2 cents. :-)


<edit: fixed typo>
« Last Edit: March 20, 2009, 08:43:47 AM by CAB »
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: More subroutine functions
« Reply #10 on: March 20, 2009, 04:00:15 AM »
I'd use an OR construct similar to the one posted by gile.

...the reader just needs to glance at it to understand the functionality.


Then you would just be perpetuating the misuse of code.

Then the reader needs to be better informed because its goofy and confusing. I think you like the use of OR because it seems like a fancy trick but if you stop and look at OR vs COND in this instance you will notice something...You know what? I'll just show you cause im sick of this crap.
< ... snip >

John, I will acknowledge that we have differing opinions an just leave it at that.

Regards
Kerry  (kdub)
« Last Edit: March 20, 2009, 04:12:41 AM by Kerry Brown »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

curmudgeon

  • Newt
  • Posts: 192
Re: More subroutine functions
« Reply #11 on: March 20, 2009, 04:10:37 AM »
I think I am going to like it in this room.
Must work now - you see, Autocad 2000 does not export to gbXML, and it needs to be able to do this thing.

As far as I know it doesn't, yet.

Lots of times I look at a "finished" piece and tell myself, and others, "that's ugly, but it runs."
I get a lot of blank stares.
Never express yourself more clearly than you are able to think.

Daniel J. Ellis

  • Swamp Rat
  • Posts: 809
Re: More subroutine functions
« Reply #12 on: March 20, 2009, 04:53:13 AM »
OK,

   Gile, I think I'm going to use your mapcar(lambda) solution - it's closest to what I'd originally wanted to do.
   Se7en, I like your COND method, purely for the "purity" of the coding: it's simpler but harder to understand at first read.
   MP, is (if numberp default) checking to make sure that the default variable a number?  very "pure," but possibly superfluous for a hard-coded value?

Thanks all,
dJE
===
dJE

gile

  • Water Moccasin
  • Posts: 2275
  • Marseille, France
Re: More subroutine functions
« Reply #13 on: March 20, 2009, 08:30:59 AM »
Se7en,

I do agree using COND is a better way.
I only tried to help danellis simplifying his code, I apologize if my purpose drives you sick, it wasn't my intention.
Speaking English as a French Frog

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9667
Re: More subroutine functions
« Reply #14 on: March 20, 2009, 08:44:17 AM »
:D No gile your fine. *lol*
TheSwamp.org (serving the CAD community since 2003)

Donate to TheSwamp.org