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

0 Members and 1 Guest are viewing this topic.

Hrishikesh

  • Guest
More on LAMBDA please?
« on: July 19, 2017, 11:59:33 AM »
Hi All,
Today I searched for function used in Autolisp "LAMBDA"
More I read about this function get more confused.
At the end of the day I just come to know that "LAMBDA" in list is like "QUEEN" in chess.
Reason of my confusion is that, somewhere lambda is used with "Apply" somewhere with "function" with "Mapcar" somewhere.
May be my knowledge in Autolisp is less to understand the code where this "Lamda" function is used.
Please can you help me to understand more about this function "LAMBDA?"

Thanks,
Hrishikesh

MP

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

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: More on LAMBDA please?
« Reply #2 on: July 19, 2017, 01:08:22 PM »
About 13 years old but relevant.

Hope it helps.
How about some tri-LAMBDA? Or is that too much? Not enough? Wrong topic? It's the wrong topic, isn't it?
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Hrishikesh

  • Guest
Re: More on LAMBDA please?
« Reply #3 on: July 19, 2017, 01:20:48 PM »
Sorry for the same Topic again..
But I am new to language Autolisp, I am just trying to understand some functions that I have never used before.
I search on internet for this but get confused, so the best way I found to ask experts in Autolisp in the forum.
Sorry if I made any mistake...

Thanks,
Hrishikesh

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: More on LAMBDA please?
« Reply #4 on: July 19, 2017, 02:34:14 PM »
lambda is just a name-less function.

Code - Auto/Visual Lisp: [Select]
  1. (defun FOO ( / ) (print "a") )
is the same as
Code - Auto/Visual Lisp: [Select]
  1. (lambda ( / ) (print "a") )

the only difference is that you can "call" the first one (with " (FOO) " on the command line); -i.e. you cannot "call" the second in the same way.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: More on LAMBDA please?
« Reply #5 on: July 19, 2017, 02:41:58 PM »
TheSwamp.org  (serving the CAD community since 2003)

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: More on LAMBDA please?
« Reply #6 on: July 19, 2017, 02:44:16 PM »
Sorry for the same Topic again..
But I am new to language Autolisp, I am just trying to understand some functions that I have never used before.
I search on internet for this but get confused, so the best way I found to ask experts in Autolisp in the forum.
Sorry if I made any mistake...

Did you look at this? That is about as clear as it gets, in my opinion.

About 13 years old but relevant.
TheSwamp.org  (serving the CAD community since 2003)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: More on LAMBDA please?
« Reply #7 on: July 19, 2017, 02:50:17 PM »
Nice! Thanks Mark. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ChrisCarlson

  • Guest
Re: More on LAMBDA please?
« Reply #8 on: July 19, 2017, 04:26:37 PM »
First time reading that one and it's MUCH better than my trial and error method I used to understand lambda.

Hrishikesh

  • Guest
Re: More on LAMBDA please?
« Reply #9 on: July 20, 2017, 12:47:51 AM »
Thank You All,
My confusion is over.
I am sure I am at right place where all people are so helpful. You all are playing big role in my Autolisp Studies.
Thanks to all my "Autolisp GURU" from whom learn a lot.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: More on LAMBDA please?
« Reply #10 on: July 20, 2017, 03:38:25 AM »
The functions you mentioned are in different categories:
apply/mapcar
function/quote/apostrophe
lambda/defun

i.e.:
Code - Auto/Visual Lisp: [Select]
  1. ; apply - apply a function to the whole list:
  2. _$ (apply 'strcat '("a" "b" "c")) -> "abc"
  3.  
  4. ; mapcar - map a function to every element in the list:
  5. _$ (mapcar 'strcase '("a" "b" "c")) -> ("A" "B" "C")
  6.  
  7. ; function/quote/apostrophe are used to unevaluate the subfunction, so it could be passed to a function like apply/mapcar:
  8. _$ (lambda (x) (strcat (strcase x) (strcase x t))) -> #<USUBR @000000be6614b2f0 -lambda-> ; evaluated function, it won't work with apply/mapcar
  9. _$ '(lambda (x) (strcat (strcase x) (strcase x t))) -> (LAMBDA (X) (STRCAT (STRCASE X) (STRCASE X T))) ; unevaluated function, it will work with apply/mapcar
  10. _$ (quote (lambda (x) (strcat (strcase x) (strcase x t)))) -> (LAMBDA (X) (STRCAT (STRCASE X) (STRCASE X T))) ; unevaluated function, it will work with apply/mapcar
  11. _$ (function (lambda (x) (strcat (strcase x) (strcase x t)))) -> (quote #<USUBR @000000be6614b390 -lambda->) ; unevaluated function, it will work with apply/mapcar
  12. ; the (function) function is used when compiling the code to .vlx, since the quote/apostrophe passes literally the subfunction to the mapcar/apply - and that messes-up the compiled code
  13. ; thats why (function foo) return is different than (quote foo) and 'foo
  14.  
  15. ; So the above means that these are equivalent:
  16. (mapcar 'strcase '("a" "b" "c")) -> ("A" "B" "C")
  17. (mapcar (quote strcase) '("a" "b" "c")) -> ("A" "B" "C")
  18. (mapcar (function strcase) '("a" "b" "c")) -> ("A" "B" "C")
  19. (mapcar strcase '("a" "b" "c")) -> Error: bad function: #<SUBR @000000be64cbe368 STRCASE>
  20.  
  21. _$ (mapcar '(lambda (x) (strcat (strcase x) (strcase x t))) '("a" "b" "c")) -> ("Aa" "Bb" "Cc")
  22. _$ (apply '(lambda (x) (strcat (strcase x) (strcase x t))) '("abc")) -> "ABCabc"
  23.  
  24.  
  25. _$ (lambda (x) (print x)(princ)) -> #<USUBR @000000be6614b480 -lambda-> ; lambda - unnamed function
  26. _$ (defun myprint (x) (print x)(princ)) -> MYPRINC ; define a named function
  27.  
  28. ; So now the named (defun) and the unnamed (lambda) functions are equivalent - they do the same thing:
  29. ;(<function> <arguments>)
  30. _$ (myprint "abc") -> "abc"
  31. _$ ((lambda (x) (print x)(princ)) "abc") -> "abc"
  32.  
  33. ; As you can see the difference is that when (lambda) is an unnamed function, you must type what it should do to each element of the list, since (myprint) is already defined:
  34. _$ (mapcar 'myprint '("Hello" "World" "!"))
  35.  
  36. "Hello"
  37. "World"
  38. "!" (  )
  39. _$ (mapcar '(lambda (x) (print x)(princ)) '("Hello" "World" "!"))
  40.  
  41. "Hello"
  42. "World"
  43. "!" (  )
  44.  
  45. ; so the above is like writing:
  46. (myprint "Hello")
  47. (myprint "World")
  48. (myprint "!")
  49.  
  50. ; and the same for (lambda):
  51. ((lambda (x) (print x)(princ)) "Hello")
  52. ((lambda (x) (print x)(princ)) "World")
  53. ((lambda (x) (print x)(princ)) "!")
  54.  
  55. ; The reason to use (lambda) instead of (defun) is that you just want to make some function on the fly to perform a small operation, i.e.:
  56. (lambda (a b) (* (/ (+ a b) 2.) 5))
  57. ; And you won't use it anywhere else
  58.  

Ok, now I feel that I start to copy from Lee Mac's tutorials so just visit his website and check them.
I advise you to start from here, since you mentioned that you're new in LISP.

EDIT:
And a small comparsion with (foreach) :
Code - Auto/Visual Lisp: [Select]
  1. ; I guess for one its easy to use the (foreach) function:
  2. (foreach item MyList ; the "dxfdata item_layer item_type MyListOfLayers MyListOfTypes" symbols must be localised in the main code
  3.   (setq dxfdata (entget item))
  4.   (setq item_layer (cdr (assoc 8 dxfdata)))
  5.   (setq item_type (cdr (assoc 0 dxfdata)))
  6.   (setq MyListOfLayers (cons item_layer MyListOfLayers))
  7.   (setq MyListOfTypes (cons item_type MyListOfTypes))
  8.   (entdel item)
  9. )
  10.  
  11. ; So how the above will be written, using mapcar'n'lambda ?, Like this:
  12.   (function ; the "dxfdata item_layer item_type" symbols are localised inside the (lambda) function
  13.     (lambda (item / dxfdata item_layer item_type ) ; and "MyListOfLayers MyListOfTypes" symbols must be localised in the main code
  14.       (setq dxfdata (entget item))
  15.       (setq item_layer (cdr (assoc 8 dxfdata)))
  16.       (setq item_type (cdr (assoc 0 dxfdata)))
  17.       (setq MyListOfLayers (cons item_layer MyListOfLayers))
  18.       (setq MyListOfTypes (cons item_type MyListOfTypes))
  19.     )
  20.   )
  21.   MyList
  22. )
  23.  
  24. ; But the real reason to use mapcar'n'lambda instead of (foreach) is:
  25. ; (foreach) - Return Values: The result of the last expr evaluated. If no expr is specified, foreach returns nil.
  26. ; (mapcar) -  Return Values: A list.
  27.  
  28. ; This means rather than doing:
  29. (foreach item MyList ; "MyListOfLayers" symbol must be localised in the main code
  30.   (setq MyListOfLayers (cons (cdr (assoc 8 (entget item))) MyListOfLayers))
  31. )
  32.  
  33. ; you can just:
  34. (setq MyListOfLayers (mapcar '(lambda (item) (cdr (assoc 8 (entget item)))) MyList))
  35.  

So if you are still confused yet, just stick with (foreach) function [like Tharwat advised me once].
« Last Edit: July 20, 2017, 04:02:26 AM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Hrishikesh

  • Guest
Re: More on LAMBDA please?
« Reply #11 on: July 20, 2017, 04:51:11 AM »
Hi,
After reading your post, No confusion at all.
Thanks!!!

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: More on LAMBDA please?
« Reply #12 on: July 20, 2017, 09:09:08 AM »
Bonus question:
Do you still have to localize any variables defined in a lambda function?

And to add to the academic layer of muck in TheSwamp, you can have some real fun with variables, lambdas, and etc. For instance, I was playing around porting some of the CL and Scheme language gems to AutoLisp years ago and ported the LET function. You can learn a lot about Autolisp--and the interpreter--by re-writing functions or porting functions over from another language entirely.

Code - Auto/Visual Lisp: [Select]
  1. (defun let ( bindings body / replace )
  2.   ;; let
  3.   ;; evaluate process with localized variables.
  4.   ;;
  5.   ;; Ported to AutoLisp By: John (Se7en) K
  6.   ;;
  7.   ;; Synopsis: let <bindings> <body>
  8.   ;;
  9.   ;;  (let '((a (+ 1 (* x y)))
  10.   ;;         (b (- 1 y)))
  11.   ;;     '(+ (* x (square a))
  12.   ;;         (* y b)
  13.   ;;         (* a b))
  14.   ;;   )
  15.   ;;  ~~>
  16.   ;;  ((LAMBDA nil (+ (* X (SQUARE (+ 1 (* X Y))))
  17.   ;;            (* Y (- 1 Y))
  18.   ;;            (* (+ 1 (* X Y)) (- 1 Y)))))
  19.   ;;
  20.   ;;  ==> Evaluated value
  21.   ;;
  22.   ;; EX1:
  23.   ;;  (setq x 5)
  24.   ;;  (+ (let '((x 3))
  25.   ;;          '(+ x (* x 10)))
  26.   ;;   x)
  27.   ;;
  28.   ;;  ==> 38
  29.   ;;
  30.   ;; EX2:
  31.   ;;  (setq x 2 y 4)
  32.   ;;  (let '((x 3)
  33.   ;;         (y (+ x 2)))
  34.   ;;       '(* x y))
  35.   ;;
  36.   ;;  ==> 12
  37.   ;;
  38.   ;; EX3:
  39.   ;;
  40.   ;;    (let
  41.   ;;      '((ss-vp
  42.   ;;          (ssget "x"
  43.   ;;                 (list '(0 . "VIEWPORT")
  44.   ;;                       '(-4 . "/=")
  45.   ;;                       '(68 . 1)
  46.   ;;                       (cons 410 (getvar "ctab"))))))
  47.   ;;      '((if (null ss-vp)
  48.   ;;          (progn
  49.   ;;            (alert "\n No VIEWPORTS in current layout")
  50.   ;;            (princ) )
  51.   ;;          ss-vp))
  52.   ;;      )
  53.     (eval
  54.         (cons (append (list 'lambda (mapcar 'car bindings)) body)
  55.               (mapcar 'cadr bindings))) )
  56.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: More on LAMBDA please?
« Reply #13 on: July 20, 2017, 02:35:41 PM »
This is very exiting, Se7en!  :-o
Thanks for posting it!
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

ChrisCarlson

  • Guest
Re: More on LAMBDA please?
« Reply #14 on: July 20, 2017, 02:48:10 PM »
Bonus question:
Do you still have to localize any variables defined in a lambda function?

And to add to the academic layer of muck in TheSwamp, you can have some real fun with variables, lambdas, and etc. For instance, I was playing around porting some of the CL and Scheme language gems to AutoLisp years ago and ported the LET function. You can learn a lot about Autolisp--and the interpreter--by re-writing functions or porting functions over from another language entirely.

Instinct has me saying no as the variable is contained within the lambda call?

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: 10646
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: 10646
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: 10646
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: 7529
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: 10646
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: 10646
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 »