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

0 Members and 1 Guest are viewing this topic.

AIberto

  • Guest
Please teaching me .
« on: July 17, 2015, 01:57:03 AM »
Dear all Lisp Master

Please patience to check  these two codes .please teaching me where is not reasonable .

Grammatical Structure reasonable ?
Local variables used are correct ?

Thanks very much!

Example 1
Code - Auto/Visual Lisp: [Select]
  1. (defun main (/ v1 v3 subfun1 subfun2)
  2.   (setq v1 (......))
  3.   (defun subfun1(/ v2)
  4.                 (setq v2 (.....))
  5.                 (setq v3 (......))
  6.     ......
  7.   );;end_defun_subfun1
  8.   (defun subfun2(/ subb v4 v5 v6)
  9.     (defun subb(/ a b c)
  10.       (setq a (...))
  11.                   (setq b (...))
  12.                   (setq c (*(atoi v6) 10))
  13.     );;end_defun_subb
  14.     (setq v4 (.....))
  15.     (setq v5 (*(atoi v3) 13))
  16.     (setq v6 (.....))
  17.     (subb)
  18.   );;end_defun_subfun2
  19. );;end_defun_main
  20.  

Example 2
Code - Auto/Visual Lisp: [Select]
  1. (defun main (/ v1 v3 subfun1 subfun2 subb)
  2.   (setq v1 (......))
  3.   (defun subfun1(/ v2)
  4.     (setq v2 (.....))
  5.     (setq v3 (......))
  6.     ......
  7.   );;end_defun_subfun1
  8.   (defun subfun2(/  v4 v5 v6)
  9.     (setq v4 (.....))
  10.     (setq v5 (*(atoi v3) 13))
  11.     (setq v6 (.....))
  12.     (subb)
  13.   );;end_defun_subfun2
  14.   (defun subb(/ a b c)
  15.     (setq a (...))
  16.     (setq b (...))
  17.     (setq c (*(atoi v6) 10))
  18.   );;end_defun_subb
  19. );;end_defun_main
  20.  
« Last Edit: July 17, 2015, 02:19:21 AM by AIberto »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Please teaching me .
« Reply #1 on: July 17, 2015, 02:56:30 AM »
I don't qualify as a master, but I'll comment anyway.
Where/how are you calling the nested functions?

The construction looks fine at first glance.

Keep in mind that 'form follows functionality'
... There is no "ideal" structure as such because the code design depends on what problem you are trying to solve.

Personally I prefer to keep my code modular; that is, use lots of smaller functions rather than one big one.
I find that this makes testing easier and helps to simplify changes to the code.

Something that a lot of people forget is to document the function.
NOT write comments every second line but rather explain the purpose of the function, the parameter types and the expected input and output.
Here's an example of a function signature that explains at a glance the purpose of the function. This becomes important when you re-visit the code later.
http://www.theswamp.org/index.php?topic=49771.msg549351#msg549351


Regards,

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

AIberto

  • Guest
Re: Please teaching me .
« Reply #2 on: July 17, 2015, 03:26:18 AM »
I want know Local variable definition is reasonable ?  ??? yes or no.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Please teaching me .
« Reply #3 on: July 17, 2015, 03:52:03 AM »

As I said,
'The construction looks fine at first glance.'
the V6 variable is the only one that I had to look at twice :)


Something that will make your life easier :
Prefix ALL your nested functions with an underscore
eg: _subb

Some projects I will also prefix variable with recognisable characters.
This helps when you're dealing with 1000+ lines of code that share variables.
 




kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

AIberto

  • Guest
Re: Please teaching me .
« Reply #4 on: July 17, 2015, 04:22:17 AM »

As I said,
'The construction looks fine at first glance.'
the V6 variable is the only one that I had to look at twice :)


Something that will make your life easier :
Prefix ALL your nested functions with an underscore
eg: _subb

Some projects I will also prefix variable with recognisable characters.
This helps when you're dealing with 1000+ lines of code that share variables.

Thank you kerry.

Yes ,use V6 variable  twice,  This is a problem?






« Last Edit: July 17, 2015, 04:40:16 AM by AIberto »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Please teaching me .
« Reply #5 on: July 17, 2015, 04:31:14 AM »
No, not a problem.
As I said,
'The construction looks fine at first glance.'

trivia:
Did you know that you could ( if you wanted to do so) just have all the variables listed in the main ? ..
But I prefer the way you've done it, declaring the variables for each nested function.
« Last Edit: July 17, 2015, 04:35:24 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

AIberto

  • Guest
Re: Please teaching me .
« Reply #6 on: July 17, 2015, 04:41:54 AM »
eg.
 func1 need  variable  "current_time ", so  I put "current_time"  in main function local variables list.
 not in get_time function local variables list.

 This is a problem?

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.  
« Last Edit: July 17, 2015, 04:53:32 AM by AIberto »

AIberto

  • Guest
Re: Please teaching me .
« Reply #7 on: July 17, 2015, 05:00:03 AM »
trivia:
Did you know that you could ( if you wanted to do so) just have all the variables listed in the main ? ..
But I prefer the way you've done it, declaring the variables for each nested function.

Hi kerry .

Example 1 and Example 2 ,  Which do you prefer ?

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Please teaching me .
« Reply #8 on: July 17, 2015, 05:06:15 AM »
I prefer Example 1.

I recall reading something years ago that suggested that nested functions in lisp were slower than un-nested functions.
My testing at that time could not determine a discernable difference.

Regards,

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

AIberto

  • Guest
Re: Please teaching me .
« Reply #9 on: July 17, 2015, 05:16:00 AM »
I prefer Example 1.

 nested functions in lisp were slower than un-nested functions.

Regards,

My understanding is
un-nested functions loaded in memory . other functions can call it.
After the end of the main function. nested functions will unloading and release . not in memory. and other functions can not call

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Please teaching me .
« Reply #10 on: July 17, 2015, 05:29:18 AM »
Yes,
and what happens when you run the routine again ??
Very few people actually know the exact mechanism used.



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Please teaching me .
« Reply #11 on: July 17, 2015, 05:33:39 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))
  ...
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Please teaching me .
« Reply #12 on: July 17, 2015, 05:42:09 AM »
Hi Roy,
The issue I've noticed with non-nested functions in code I've reviewed is that global variables are not treated findly and the scope of variables usually becomes an issue.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Please teaching me .
« Reply #13 on: July 17, 2015, 06:55:47 AM »
Hi Roy,
The issue I've noticed with non-nested functions in code I've reviewed is that global variables are not treated findly and the scope of variables usually becomes an issue.
Do you mean global variables used by non-nested functions as opposed to variables within the scope of a main function that are (also) used by its nested functions?

77077

  • Guest
Re: Please teaching me .
« Reply #14 on: July 17, 2015, 06:55:56 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))
  ...
)

ROY
formal parameter & actual parameter ?
Not for beginners ,,,,,