Author Topic: More on LAMBDA please?  (Read 4345 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: 10605
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: 28753
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: 28753
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: 10605
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?