Author Topic: Please teaching me .  (Read 7535 times)

0 Members and 1 Guest are viewing this topic.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Please teaching me .
« Reply #15 on: July 17, 2015, 07:03:37 AM »
ROY
formal parameter & actual parameter ?
Not for beginners ,,,,,
If you mean that a function with arguments is too hard to understand for a beginner, and I think that is what you mean, I disagree.

AIberto

  • Guest
Re: Please teaching me .
« Reply #16 on: July 17, 2015, 09:42:55 AM »
I prefer example 3 (see below).
In many code samples on this forum you will find nested functions. But there are some arguments against this practice:
  • As Kerry mentions: nested functions will make the code slower. Every time the main function is entered the nested functions will be redefined.
  • Unnested functions can be reused by other functions allowing for a modular coding style.
  • It is easier to debug unnested functions.
  • Related to the previous: Nested functions can easily lead to spaghetti code.

EXAMPLE 3:
Code: [Select]
(defun fun1 (a0 / v0)
  (setq a0 ...)
  ...
  (setq v0 ...)
  ...
  v0
)

(defun fun2 (a0 / v0 v1 v2)
  (setq v0 ...)
  (setq v1 (* (atoi a0) 13))
  (setq v2 (fun3 v2))
  ...
  v2
)

(defun fun3 (a0 / v0 v1 v2)
  (setq v0 ...)
  (setq v1 ...)
  (setq v2 (* (atoi a0) 10))
  ...
  v2
)

(defun main ( / v0 v1)
  (setq v0 ...)
  (setq v1 (fun2 v0))
  ...
)


Hi roy
So many repeated variable name . Why ?

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Please teaching me .
« Reply #17 on: July 17, 2015, 02:54:01 PM »
So many repeated variable name . Why ?
Since all functions in Example 3 are independent functions there is no reason why the same argument and variable names shouldn't be reused. This practice is in fact not uncommon. Examples of some frequently used variable names in Lisp code: i, lst, obj and str.

AIberto

  • Guest
Re: Please teaching me .
« Reply #18 on: July 17, 2015, 09:08:15 PM »
So many repeated variable name . Why ?
Since all functions in Example 3 are independent functions there is no reason why the same argument and variable names shouldn't be reused. This practice is in fact not uncommon. Examples of some frequently used variable names in Lisp code: i, lst, obj and str.


Hi roy
Yes,you are right. The variable name will rarely . But how make function with arguments
This e.g at #6

How make function "get_time"  with arguments ?
Code - Auto/Visual Lisp: [Select]
  1. (defun main (/ get_time func1  current_time )
  2.  ;; func1 need  variable  "current_time ", so  I put "current_time"  in main function local variables list
  3. (defun get_time (/ tm)
  4.   (if (and (null *itime*) (setq tm (LM:InternetTime "YYYYMODD")))
  5.     (progn
  6.           (setq Current_time (LM:InternetTime "YYYYMODD"))
  7.           (setq ju_time (DTOJ (atof tm)))        
  8.     );end_progn
  9.     (progn
  10.           (setq Current_time (thedate (getvar "cdate") ""))
  11.           (setq ju_time (DTOJ (atof (thedate (getvar "cdate") ""))))
  12.     );end_progn
  13.   );;end_if
  14. );end_defun
  15.  
  16. ..........
  17. ..........
  18.  
  19. (defun func1()
  20. (get_time)
  21. (if (= Current_time .................);;(need function "get_time" 's variable "current_time )
  22. ......
  23. ......
  24. );;end_defun
  25.  
  26.  

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Please teaching me .
« Reply #19 on: July 18, 2015, 06:16:54 AM »
I don't understand your get_time function. Notably the handling of *itime* is confusing.
But generally speaking not all functions require arguments:
Code: [Select]
; (Add_2_Numbers 3 6) => 9
(defun Add_2_Numbers (num1 num2)
  (+ num1 num2)
)

; (Pi_Divided_By_2) => 1.5708
(defun Pi_Divided_By_2 ()
  (/ pi 2.0)
)

; (Get_DateDay) => 2457222
(defun Get_DateDay ()
  (fix (getvar 'date))
)

AIberto

  • Guest
Re: Please teaching me .
« Reply #20 on: July 18, 2015, 06:27:16 AM »
Hi roy

Code - Auto/Visual Lisp: [Select]
  1. (defun get_time (/ tm)
  2.   (if (setq tm (LM:InternetTime "YYYYMODD"))
  3.     (progn
  4.           (setq Current_time (LM:InternetTime "YYYYMODD"))
  5.           (setq ju_time (DTOJ (atof tm)))        
  6.     );end_progn
  7.     (progn
  8.           (setq Current_time (thedate (getvar "cdate") ""))
  9.           (setq ju_time (DTOJ (atof (thedate (getvar "cdate") ""))))
  10.     );end_progn
  11.   );;end_if
  12. );end_defun
  13.  

How make this function returns two value ? "current_time" and " ju_time"

returns a list ?  and if other function need "current_time" or "ju_time" ,so use "(car list)" or "(cadr list)"  ?? That is right ?

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Please teaching me .
« Reply #21 on: July 18, 2015, 06:35:28 AM »
You can either return the two values in a list:

Code - Auto/Visual Lisp: [Select]
  1. (defun get_time ( / tmp )
  2.     (if (setq tmp (LM:internettime "YYYYMODD"))
  3.         (list tmp (dtoj (atoi tmp)))
  4.         (list (itoa (fix (getvar 'cdate))) (fix (getvar 'date)))
  5.     )
  6. )

...or pass the function two quoted symbols to which the values will be assigned:

Code - Auto/Visual Lisp: [Select]
  1. (defun get_time ( outsym1 outsym2 )
  2.     (if (set outsym1 (LM:internettime "YYYYMODD"))
  3.         (set outsym2 (dtoj (atoi (eval outsym1))))
  4.         (progn
  5.             (set outsym1 (itoa (fix (getvar 'cdate))))
  6.             (set outsym2 (fix (getvar 'date)))
  7.         )
  8.     )
  9.     nil
  10. )
Code - Auto/Visual Lisp: [Select]
  1. (get_time 'mydate 'myjuliandate)

For the latter, note that the supplied symbols cannot be the same as the symbols used to represent the function parameters, else the values will remain local to the function.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Please teaching me .
« Reply #22 on: July 18, 2015, 06:41:40 AM »
@ AIberto:
I think you should think about creating a function that returns a single value. I would advise that the output be a number in the same format as the DATE variable. It would make the most sense to revise Lee's LM:InternetTime for this purpose...

Tip: Take a good look at your code. You are unnecessarily calling functions twice.

AIberto

  • Guest
Re: Please teaching me .
« Reply #23 on: July 19, 2015, 05:21:17 AM »
@ AIberto:
I think you should think about creating a function that returns a single value. I would advise that the output be a number in the same format as the DATE variable. It would make the most sense to revise Lee's LM:InternetTime for this purpose...

Tip: Take a good look at your code. You are unnecessarily calling functions twice.

Hi roy . Thank you

Oops!  I have noticed that calling functions twice. 
 (setq tm (LM:InternetTime "YYYYMODD"))  and  (setq Current_time (LM:InternetTime "YYYYMODD"))

Thank you for your advice .



AIberto

  • Guest
Re: Please teaching me .
« Reply #24 on: July 19, 2015, 05:32:19 AM »
You can either return the two values in a list:

Code - Auto/Visual Lisp: [Select]
  1. (defun get_time ( / tmp )
  2.     (if (setq tmp (LM:internettime "YYYYMODD"))
  3.         (list tmp (dtoj (atoi tmp)))
  4.         (list (itoa (fix (getvar 'cdate))) (fix (getvar 'date)))
  5.     )
  6. )

...or pass the function two quoted symbols to which the values will be assigned:

Code - Auto/Visual Lisp: [Select]
  1. (defun get_time ( outsym1 outsym2 )
  2.     (if (set outsym1 (LM:internettime "YYYYMODD"))
  3.         (set outsym2 (dtoj (atoi (eval outsym1))))
  4.         (progn
  5.             (set outsym1 (itoa (fix (getvar 'cdate))))
  6.             (set outsym2 (fix (getvar 'date)))
  7.         )
  8.     )
  9.     nil
  10. )
Code - Auto/Visual Lisp: [Select]
  1. (get_time 'mydate 'myjuliandate)


Lee ,  Useful !  It will take a little time to study .  Many thanks !

Quote
For the latter, note that the supplied symbols cannot be the same as the symbols used to represent the function parameters, else the values will remain local to the function.

Lee ,you mean can call this function like this : (get_time 'mydate 'myjuliandate) or (get_time 'md  'mj)  ,But can't be like this : ( get_time  'outsym1  'outsym2 ) , yes or no ?
« Last Edit: July 19, 2015, 06:00:47 AM by AIberto »

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Please teaching me .
« Reply #25 on: July 19, 2015, 06:01:22 AM »
Quote
For the latter, note that the supplied symbols cannot be the same as the symbols used to represent the function parameters, else the values will remain local to the function.
I can't understand. Can you specifically talk about it?  or  give me an example ? Thanks.

Observe:

Code - Auto/Visual Lisp: [Select]
  1. _$ (defun foo ( a b ) (set a 1) (set b 2) (princ))
  2. FOO
  3. _$ (foo 'c 'd)
  4. _$ c
  5. 1
  6. _$ d
  7. 2
  8. _$ (foo 'a 'b)
  9. _$ a
  10. nil
  11. _$ b
  12. nil

AIberto

  • Guest
Re: Please teaching me .
« Reply #26 on: July 19, 2015, 06:05:45 AM »
Quote
For the latter, note that the supplied symbols cannot be the same as the symbols used to represent the function parameters, else the values will remain local to the function.
I can't understand. Can you specifically talk about it?  or  give me an example ? Thanks.

Observe:

Code - Auto/Visual Lisp: [Select]
  1. _$ (defun foo ( a b ) (set a 1) (set b 2) (princ))
  2. FOO
  3. _$ (foo 'c 'd)
  4. _$ c
  5. 1
  6. _$ d
  7. 2
  8. _$ (foo 'a 'b)
  9. _$ a
  10. nil
  11. _$ b
  12. nil


Lee, Thank you , You reply so quickly . I have already modified previous post . I understand .

DuanJinHui

  • Guest
Re: Please teaching me .
« Reply #27 on: July 19, 2015, 11:01:51 AM »

...or pass the function two quoted symbols to which the values will be assigned:

Code - Auto/Visual Lisp: [Select]
  1. (defun get_time ( outsym1 outsym2 )
  2.     (if (set outsym1 (LM:internettime "YYYYMODD"))
  3.         (set outsym2 (dtoj (atoi (eval outsym1))))
  4.         (progn
  5.             (set outsym1 (itoa (fix (getvar 'cdate))))
  6.             (set outsym2 (fix (getvar 'date)))
  7.         )
  8.     )
  9.     nil
  10. )


Lee, Why use "set" rather than "setq" ?

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Please teaching me .
« Reply #28 on: July 19, 2015, 11:29:41 AM »

...or pass the function two quoted symbols to which the values will be assigned:

Code - Auto/Visual Lisp: [Select]
  1. (defun get_time ( outsym1 outsym2 )
  2.     (if (set outsym1 (LM:internettime "YYYYMODD"))
  3.         (set outsym2 (dtoj (atoi (eval outsym1))))
  4.         (progn
  5.             (set outsym1 (itoa (fix (getvar 'cdate))))
  6.             (set outsym2 (fix (getvar 'date)))
  7.         )
  8.     )
  9.     nil
  10. )


Lee, Why use "set" rather than "setq" ?

Because I require both arguments supplied to the set function to be evaluated: the first argument yields the symbol to which the value should be assigned (that is, the quoted symbol supplied as an argument to the 'get_time' function); the second argument yields the value (as per setq).

If setq were used, the first argument would not be evaluated and the corresponding value would be assigned to the 'outsym1' or 'outsym2' symbols.

Observe:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq a 'b)
  2. B
  3. _$ (set a 1)
  4. 1
  5. _$ a
  6. B
  7. _$ b
  8. 1
« Last Edit: July 19, 2015, 11:34:18 AM by Lee Mac »

AIberto

  • Guest
Re: Please teaching me .
« Reply #29 on: July 19, 2015, 12:09:56 PM »
Lee, Why use "set" rather than "setq" ?

(setq my-value "my string")

(set (quote my-value) "my string") = (set 'my-value "my string")


Because I require both arguments supplied to the set function to be evaluated: the first argument yields the symbol to which the value should be assigned (that is, the quoted symbol supplied as an argument to the 'get_time' function); the second argument yields the value (as per setq).

If setq were used, the first argument would not be evaluated and the corresponding value would be assigned to the 'outsym1' or 'outsym2' symbols.

Observe:
Code - Auto/Visual Lisp: [Select]
  1. _$ (setq a 'b)
  2. B
  3. _$ (set a 1)
  4. 1
  5. _$ a
  6. B
  7. _$ b
  8. 1

Lee .Good explanation.


I use
Code: [Select]
(defun thedate (date1 sep1 /)
(setq date1 (rtos date1 2 8))
(strcat (substr date1 1 4)
sep1
(substr date1 5 2)
sep1
(substr date1 7 2)
)
)
Code: [Select]
(thedate (getvar "cdate") "")    and  (dtoj (atoi (thedate (getvar "cdate") "")))

you use
Code: [Select]
(itoa (fix (getvar 'cdate)))   and  (fix (getvar 'date))

You are wonderful !!! 
« Last Edit: July 19, 2015, 12:14:36 PM by AIberto »