Author Topic: More on LAMBDA please?  (Read 4343 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: More on LAMBDA please?
« Reply #15 on: July 20, 2017, 04:43:08 PM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: More on LAMBDA please?
« Reply #16 on: July 20, 2017, 07:02:50 PM »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: More on LAMBDA please?
« Reply #17 on: July 20, 2017, 07:06:25 PM »
Instinct has me saying no as the variable is contained within the lambda call?

No, sorry. You must still localize variables created within a lambda. Although, if you need to create variables within a lambda, it's a very good indication you should create a named function instead.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: More on LAMBDA please?
« Reply #18 on: July 20, 2017, 07:08:05 PM »
This is very exiting, Se7en!  :-o
Thanks for posting it!

No problem.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: More on LAMBDA please?
« Reply #19 on: July 20, 2017, 07:18:23 PM »
... if you need to create variables within a lambda, it's a very good indication you should create a named function instead.

Support this assertion.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Hrishikesh

  • Guest
Re: More on LAMBDA please?
« Reply #20 on: July 20, 2017, 11:12:33 PM »
Wow!!!
Thats fantastic...
Thanks John...

ChrisCarlson

  • Guest
Re: More on LAMBDA please?
« Reply #21 on: July 21, 2017, 08:29:39 AM »
Instinct has me saying no as the variable is contained within the lambda call?

No, sorry. You must still localize variables created within a lambda. Although, if you need to create variables within a lambda, it's a very good indication you should create a named function instead.

If for some reason you needed to set a variable within the lambda call, that variable would now be globally available in that session?

ronjonp

  • Needs a day job
  • Posts: 7526
Re: More on LAMBDA please?
« Reply #22 on: July 21, 2017, 09:36:14 AM »
Instinct has me saying no as the variable is contained within the lambda call?

No, sorry. You must still localize variables created within a lambda. Although, if you need to create variables within a lambda, it's a very good indication you should create a named function instead.

If for some reason you needed to set a variable within the lambda call, that variable would now be globally available in that session?
Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda (n / i) (setq i n)) '(1 2 3 4 5 6 7 8))
  2. ;;;Command: !i
  3. ;;;nil
  4. (mapcar '(lambda (n) (setq i n)) '(1 2 3 4 5 6 7 8))
  5. ;;;Command: !i
  6. ;;;8

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: More on LAMBDA please?
« Reply #23 on: July 21, 2017, 10:37:30 AM »
Instinct has me saying no as the variable is contained within the lambda call?

No, sorry. You must still localize variables created within a lambda. Although, if you need to create variables within a lambda, it's a very good indication you should create a named function instead.

If for some reason you needed to set a variable within the lambda call, that variable would now be globally available in that session?


EDIT: ronjonp beat me to it.
***

Let me answer your question with a question (sorry).

What happens in a named function when you don't localize a variable?

Now, lets be clear here, we are quickly getting off topic here and going down the rabbit hole of "name resolution" -i.e. potentially breaking other parts of your/another program because of previously defined variables but, below is a link for you to read up on two different types of "scoping" and how different languages use it. Will the information gleaned from the following be useful? *meh* (maybe only if you get into other languages and/or compiler design--an absolute fascinating area to study if you ask me--but if you keep your AutoLisp variables localized, probably not).  Is it interesting? yeah.

https://en.wikipedia.org/wiki/Scope_(computer_science)#Static_versus_dynamic_scoping
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ChrisCarlson

  • Guest
Re: More on LAMBDA please?
« Reply #24 on: July 21, 2017, 10:56:01 AM »
Very interesting, I looked through my various routines and do not see any instances where I set a variable within the lambda call. Phew, I'm in the clear.

In a named function the variable remains until the session is closed. I'm quite intrigued as to how much lambda mimics defun.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
Re: More on LAMBDA please?
« Reply #25 on: July 21, 2017, 11:07:09 AM »
Very interesting, I looked through my various routines and do not see any instances where I set a variable within the lambda call. Phew, I'm in the clear.

In a named function the variable remains until the session is closed. I'm quite intrigued as to how much lambda mimics defun.

:) "Modular design" is your friend -i.e. creating a bunch of small (single purpose) functions is good.

Good! ...post your research/findings. From what I remember, almost entirely; defun allows you to name your functions and call them. defun-q allows you to look up and access contents.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Hrishikesh

  • Guest
Re: More on LAMBDA please?
« Reply #26 on: July 22, 2017, 02:08:21 AM »
Nice!!!
I was confused about this function, but after reading all posts in this topic I come to know how cleverly this function can be used in the code.
Thank you All..
« Last Edit: July 22, 2017, 02:28:40 AM by Hrishikesh »