Author Topic: (discussion) Costruct procedures with Lambda  (Read 10329 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
(discussion) Costruct procedures with Lambda
« on: March 02, 2006, 08:51:29 AM »
What do you think of using lambda in your procedures instead of separate named procedures. Example:

Code: [Select]
(defun square (x) (* x x) )

(defun sum-of-two-squares (x y)
  (+ (square x) (square y) )
 )
or even:
Code: [Select]
(defun sum-of-two-squares ( x y / sqaure )
 
    (defun square (x) (* x x) )

  (+ (square x) (square y) )
 )
v.s.
Code: [Select]
(defun sum-of-two-squares-redo (x y)
  ( (lambda (x y)
      (+ x y ))
   (* x x)
   (* y y)
   )
 )

My thoughts:
Pros:
o  I like the idea of no named procedures floating about even if they could be wrapped up. Makes the procedure seem cleaner.
o  Almost seems a bit more "readable"--More like it "shoud" be...kinda. Although the overall code looks/feels diff then either of the two above there arent any seperate named procedures floating about in the code when it comes time to maintain this procedure.

Cons:
o  Makes you think a bit more a head of time then the other two. (Which isnt that bad)
o  Its almost less readable upon first glance.

I'll think about this some more, but i wanted to get your thoughts on the subject.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: (discussion) Costruct procedures with Lambda
« Reply #1 on: March 02, 2006, 09:02:42 AM »
I have used lambda on various occasions when writing a standout function was not warranted, but I must say that if the procedure is something that can be used elsewhere, it is almost (note I said almost) always a good idea to globalize it within the code and utilize it as needed rather than rewrite sections of code.

In your above example, I would have not used lambda, but then I like to make things a bit more compact.

Code: [Select]
(defun sum-of-two-squares ( x y )
 (+ (* X X)(* Y Y))
)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #2 on: March 02, 2006, 09:06:40 AM »
Well neither would i--in the example above--but i needed an example. *lol*
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: (discussion) Costruct procedures with Lambda
« Reply #3 on: March 02, 2006, 09:09:35 AM »
I figured as much, but I felt compelled ... now why do I do that ... :)
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #4 on: March 02, 2006, 09:22:16 AM »
Hi kpblc, Welcome to theSwamp.

I looked at the link--I know its in, what looks to me like Russian, but i looked at all the code--I dont see what your trying to convey. Can you try to explain it to me again. (Im not understanding.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kpblc

  • Bull Frog
  • Posts: 396
Re: (discussion) Costruct procedures with Lambda
« Reply #5 on: March 02, 2006, 09:41:34 AM »
Ooohhh, sorry! I'm terrible sorry!
I'd like to show lambda-functions code, but the code was the recursion example!
> Moderators : Please, erase my posts in this topic...
Anyway, author send me this code for getting summ of squares:
Code: [Select]
(defun sum-squares-lst (lst)
  (if lst
    (+ (* (car lst) (car lst)) (sum-squares-lst (cdr lst)))
    0
  ) ;_  if
) ;_  defun
;;(sum-squares-lst '(2 3)); => 13
;;(sum-squares-lst '(2 3 4)); => 29
;;(sum-squares-lst '(1 2 3 4 5 6 7 8 9 10)); => 385
Sorry for my English.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #6 on: March 02, 2006, 09:49:08 AM »
Oh no need to be sorry.

Im a moderator I can delete your post if you wish.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

kpblc

  • Bull Frog
  • Posts: 396
Re: (discussion) Costruct procedures with Lambda
« Reply #7 on: March 02, 2006, 09:56:32 AM »
Yes, I think it's good idea to delete my first post "Posted on: Today at 09:14:35 AM" - it's out of theme.
Thank you.
Sorry for my English.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #8 on: March 02, 2006, 09:57:19 AM »
You got it.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: (discussion) Costruct procedures with Lambda
« Reply #9 on: March 02, 2006, 09:59:08 AM »
Might it be a good idea to simply clean up the post and leave the link to the other site for other users wishing to see what might be available there?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #10 on: March 02, 2006, 10:00:07 AM »
Ok, I removed the post. (I have it saved in a file on my computer if you wish to have it.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #11 on: March 02, 2006, 10:02:08 AM »
Might it be a good idea to simply clean up the post and leave the link to the other site for other users wishing to see what might be available there?

To late. *lol* Good idea.
I saved the link. Here it is: [ http://dwg.ru/forum/viewtopic.php?t=6571&start=15) ]
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: (discussion) Costruct procedures with Lambda
« Reply #12 on: March 02, 2006, 10:25:23 AM »
Generally speaking, putting aside the use of lambda in mapcar or apply calls, I use lambda where --

  • I need a function but not a formal declaration.
  • [to me] it improves the clarity or intent of my code.
  • where I envision a code cluster may later become a formally declared function.
  • I wish to exploit the fact I can localize variables -- declaring and using them right where I need 'em.

I personally wouldn't use it the way you demonstrated as it seemed to obfuscate things a bit without adding any tangible benefit that I could see, though that gets into the whole "style" thing where things can be rather subjective.

It's a very interesting topic John. I apologise, but I can't contribute more than the above (like examples) at this time. If you search the swamp using "mp" and "lambda" you may find some examples of my use, good and bad no doubt.
« Last Edit: March 02, 2006, 10:33:13 AM by MP »
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: (discussion) Costruct procedures with Lambda
« Reply #13 on: March 02, 2006, 10:36:35 AM »
MP, So more in a traditional mapcar-lambda type of situation?
...Do you have a small example lying arround you can post? (Dosent have to be anything fancy just what ever you got infront of you.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #14 on: March 02, 2006, 10:37:52 AM »
Oh ok.(I just saw the edit you added.) Im on it.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

whdjr

  • Guest
Re: (discussion) Costruct procedures with Lambda
« Reply #15 on: March 02, 2006, 11:19:39 AM »
I really like using lambda however I rarely use it on it's own.  Here are two functions I used alot:
Quote
(defun *layout_list* (/ lst)
  (vlax-map-collection
    (vla-get-layouts
      (vla-get-activedocument (vlax-get-acad-object))
    )
    '(lambda (x) (setq lst (cons x lst)))
  )
  (cdr
    (*sort* lst 'vla-get-taborder)
  )
)

(defun *sort* (lst func)
  (vl-sort lst
      '(lambda (e1 e2)
         (< ((eval func) e1) ((eval func) e2))
       )
  )
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (discussion) Costruct procedures with Lambda
« Reply #16 on: March 02, 2006, 11:42:15 AM »
How about this?

Code: [Select]
(defun sum-squares-lst (lst)
  (apply '+ (MAPCAR '(lambda (x) (* x x)) lst))
)

;;(sum-squares-lst '(2 3)); => 13
;;(sum-squares-lst '(2 3 4)); => 29
;;(sum-squares-lst '(1 2 3 4 5 6 7 8 9 10)); => 385
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

whdjr

  • Guest
Re: (discussion) Costruct procedures with Lambda
« Reply #17 on: March 02, 2006, 12:51:24 PM »
WOOHOO!!!!

Finally someone used Mapcar, Lambda, and Apply all in the same routine.   :evil:





In case your wondering I do like your version. :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #18 on: March 02, 2006, 04:29:09 PM »
I will admit that I was going to respond with the same old "*sigh*" or a "What are you guys talking about" but instead; Ive thought, and thought about some of your responces. I was forced to read, and re-read. And I think I may finaly have an understanding. Are you ready for this. (This is what it all amounts down to.) I dosent matter. Not one bit. There is (or should be) any loss in prformance, speed, etc in the end.

As far as I can tell from my readings, the interpretor does not see a named abstraction as a 'name' during the evaluation process.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: (discussion) Costruct procedures with Lambda
« Reply #19 on: March 02, 2006, 05:20:57 PM »
As far as I can tell from my readings, the interpretor does not see a named abstraction as a 'name' during the evaluation process.

Maybe not, but the reader/maintainer does, and THAT is where a great deal of its value lies.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (discussion) Costruct procedures with Lambda
« Reply #20 on: March 02, 2006, 05:25:09 PM »
As far as I can tell from my readings, the interpretor does not see a named abstraction as a 'name' during the evaluation process.

Maybe not, but the reader/maintainer does, and THAT is where a great deal of its value lies.

[nodding.gif]
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #21 on: March 02, 2006, 06:35:15 PM »
STOP THE PRESS! I will have to do more thinking on this subject.



> Readability
So is that an agrement towards the use of lambda vs a name or visa-versa? Because one could argue both ways.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Chuck Gabriel

  • Guest
Re: (discussion) Costruct procedures with Lambda
« Reply #22 on: March 02, 2006, 09:52:52 PM »
I suppose it comes down to personal preference in the end.  I generally find named abstractions to be easier to follow, though there is the disadvantage of having to flip back and forth between the function definition and the places where it is used.  However, for very short functions that are used only in one place, I can see the argument for using a lambda function inline.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #23 on: March 03, 2006, 03:12:47 AM »
How about this?

Code: [Select]
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '(lambda (x) (* x x)) lst)))
Compare...
Code: [Select]
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '* lst lst)))

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (discussion) Costruct procedures with Lambda
« Reply #24 on: March 03, 2006, 03:23:50 AM »
How about this?

Code: [Select]
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '(lambda (x) (* x x)) lst)))
Compare...
Code: [Select]
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '* lst lst)))


Hello ElpanovEvgeniy, Welcome to theSwamp !
Quote
Sorry, my English it is awful...

 Your LISP is VERY good ! Nice code.   :-)



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.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #25 on: March 03, 2006, 03:42:19 AM »
But here no LAMBDA  :-(

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (discussion) Costruct procedures with Lambda
« Reply #26 on: March 03, 2006, 03:44:45 AM »
 :cry:
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.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #27 on: March 03, 2006, 03:59:34 AM »
:cry:
Here is lambda!
Code: [Select]
;| A library function
; Multiple changing one elements of a list
; On other.
; Function usables in all events when it is possible
; Check on correspondence, through "EQUAL"
; Writer Evgeniy Elpanov.
; ***********************************************************
; Arguments:
; lst - a list in which it is necessary to make changings
; lst-i - correspondences ((changed replacer)... ())
; ***********************************************************
; An example of a call
; (multi-subst '(1 2 3 4 5)' ((1 "a") (3 "b")))
; Function will return ("a" 2 "b" 45)
|;
(defun multi-subst (lst lst-i)
  (eval
    (list
      (function mapcar)
      (list
        (function function)
        (list
          (function lambda)
          '(a)
          (cons
            (function cond)
              (reverse
               (cons
                '(t a)
                  (mapcar
                    (function
                      (lambda (a)
                        (list
                          (list
                            (function equal)
                            (car a)
                            'a
                          ) ; _ list
                          (cadr a)
                        ) ; _ list
                      ) ; _ lambda
                    ) ; _ function
                    lst-i
                  ) ; _ mapcar
                ) ; _ cons
             ) ; _ reverse
          ) ; _ cons
        ) ; _ cons
      ) ; _ list
      'lst
    ) ; _ list
  ) ; _ eval
) ; _ defun
« Last Edit: March 05, 2006, 04:09:30 PM by ElpanovEvgeniy »

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: (discussion) Costruct procedures with Lambda
« Reply #28 on: March 03, 2006, 06:59:43 AM »
Here is lambda!

Indeed it is.  :-o

Nice stuff Evgeniy, welcome to theswamp.
TheSwamp.org  (serving the CAD community since 2003)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: (discussion) Costruct procedures with Lambda
« Reply #29 on: March 03, 2006, 07:19:03 AM »
good fun Evgeniy .. that will keep John < Se7en > busy   :lol:
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: (discussion) Costruct procedures with Lambda
« Reply #30 on: March 03, 2006, 07:41:32 AM »
Now that is lambda. Let me take some time to digest it.

Welcome to the swamp ElpanovEvgeniy.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: (discussion) Costruct procedures with Lambda
« Reply #31 on: March 03, 2006, 07:50:23 AM »
Very elegant contributions ElpanovEvgeniy, welcome to the swamp.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #32 on: March 03, 2006, 08:13:17 AM »
Thanks everything, for kind words!
 :-)

JohnK

  • Administrator
  • Seagull
  • Posts: 10646
Re: (discussion) Costruct procedures with Lambda
« Reply #33 on: March 03, 2006, 09:47:07 AM »
W H A T   I S   T H A T?!
*smile* Thank you Evgeniy. Welcome to theSwamp.

This all came about when I started to think about a simple mapcar-lambda statment so then I turned to my favorite book S.I.C.P. for answers. But then I got really confused and frustrated... This is what im reading. [ LINK ]

But some of the things i was looking to answer were.
*If a lambda is an unnamed definition and typicaly used at the point of the use.*
o  when used in a looping statment is that lambda redefined each time a new item is picked off.
o  By Using lambda vs a solid name, would your program slow down and become " bloated "?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #34 on: March 04, 2006, 05:42:12 PM »
>Se7en

I compared time of performance
setq + while >> time >> mapcar + lambda
PS.But now, I would write not so...

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #35 on: March 05, 2006, 04:44:27 PM »
But some of the things i was looking to answer were.
*If a lambda is an unnamed definition and typicaly used at the point of the use.*
o  when used in a looping statment is that lambda redefined each time a new item is picked off.
o  By Using lambda vs a solid name, would your program slow down and become " bloated "?
Then review here. I am assured, you will understand sense.
Code: [Select]
(defun multi-subst-fun (lst-i)
  (eval
    (list
      'defun
      'multi-subst
      '(lst)
      (list
        'mapcar
        (list 'function
              (list
                'lambda
                '(a)
                (cons
                  'cond
                  (reverse
                    (cons
                      '(t a)
                      (mapcar
                        '(lambda (a)
                           (list
                             (list
                               'equal
                               (car a)
                               'a
                             ) ;_  list
                             (cadr a)
                           ) ;_  list
                         ) ;_  lambda
                        lst-i
                      ) ;_  mapcar
                    ) ;_  cons
                  ) ;_  reverse
                ) ;_  cons
              ) ;_  list
        ) ;_  list
        'lst
      ) ;_  list
    ) ;_  list
  ) ;_  eval
) ;_  defun
;; do the test...
Code: [Select]
(multi-subst-fun '((1 "a") (3 "b")))

(multi-subst '(1 2 3 4 5))
(multi-subst '(1 6 4 3 7))
I have answered your question?

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #36 on: May 18, 2006, 06:26:08 AM »
Code: [Select]
(setq lst '(1 2 3 4 5 6 7 8 9 10 11 12))
  ; Variant through defun
(defun lst-3d-pt (lst)
  (if lst
    (cons (list (car lst) (cadr lst) (caddr lst)) (lst-3d-pt (cdddr lst)))
  ) ; _ if
) ; _ defun

(setq 3d-lst (lst-3d-pt lst))
; 3d-lst = '((1 2 3) (4 5 6) (7 8 9) (10 11 12))


  ; Variant through lambda
(setq f      (lambda (x)
               (if x
                 (cons (list (car x) (cadr x) (caddr x)) (f (cdddr x)))
               ) ; _ if
             ) ; _ lambda
      3d-lst (f lst)
) ; _ setq
; 3d-lst = '((1 2 3) (4 5 6) (7 8 9) (10 11 12))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: (discussion) Costruct procedures with Lambda
« Reply #37 on: October 08, 2006, 11:05:29 AM »
I have found very interesting site on a theme 'Lambda...
http://autolisp.mapcar.net/lambda.html



<edit: repair link>
Here it is translated into English. No Word wrap! CAB
http://tinyurl.com/lq7ct
« Last Edit: October 08, 2006, 11:40:49 AM by CAB »