TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnK on March 02, 2006, 08:51:29 AM

Title: (discussion) Costruct procedures with Lambda
Post by: JohnK 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Keith™ 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))
)
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK on March 02, 2006, 09:06:40 AM
Well neither would i--in the example above--but i needed an example. *lol*
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Keith™ on March 02, 2006, 09:09:35 AM
I figured as much, but I felt compelled ... now why do I do that ... :)
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK 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.)
Title: Re: (discussion) Costruct procedures with Lambda
Post by: kpblc 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
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: kpblc 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK on March 02, 2006, 09:57:19 AM
You got it.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Keith™ 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?
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK 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.)
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK 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) ]
Title: Re: (discussion) Costruct procedures with Lambda
Post by: MP 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 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK 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.)
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK on March 02, 2006, 10:37:52 AM
Oh ok.(I just saw the edit you added.) Im on it.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: whdjr 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))
       )
  )
)
Title: Re: (discussion) Costruct procedures with Lambda
Post by: CAB 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
Title: Re: (discussion) Costruct procedures with Lambda
Post by: whdjr 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. :-)
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Chuck Gabriel 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Kerry 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]
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Chuck Gabriel 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: ElpanovEvgeniy 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)))
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Kerry 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.   :-)



Title: Re: (discussion) Costruct procedures with Lambda
Post by: ElpanovEvgeniy on March 03, 2006, 03:42:19 AM
But here no LAMBDA  :-(
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Kerry on March 03, 2006, 03:44:45 AM
 :cry:
Title: Re: (discussion) Costruct procedures with Lambda
Post by: ElpanovEvgeniy 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
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Mark on March 03, 2006, 06:59:43 AM
Here is lambda!

Indeed it is.  :-o

Nice stuff Evgeniy, welcome to theswamp.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: Kerry on March 03, 2006, 07:19:03 AM
good fun Evgeniy .. that will keep John < Se7en > busy   :lol:
Title: Re: (discussion) Costruct procedures with Lambda
Post by: CAB 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.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: MP on March 03, 2006, 07:50:23 AM
Very elegant contributions ElpanovEvgeniy, welcome to the swamp.
Title: Re: (discussion) Costruct procedures with Lambda
Post by: ElpanovEvgeniy on March 03, 2006, 08:13:17 AM
Thanks everything, for kind words!
 :-)
Title: Re: (discussion) Costruct procedures with Lambda
Post by: JohnK 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 ]  (http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-26.html#%_sec_4.1.6)

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 "?
Title: Re: (discussion) Costruct procedures with Lambda
Post by: ElpanovEvgeniy 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...
Title: Re: (discussion) Costruct procedures with Lambda
Post by: ElpanovEvgeniy 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?
Title: Re: (discussion) Costruct procedures with Lambda
Post by: ElpanovEvgeniy 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))
Title: Re: (discussion) Costruct procedures with Lambda
Post by: ElpanovEvgeniy 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