TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started 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:
(defun square (x) (* x x) )
(defun sum-of-two-squares (x y)
(+ (square x) (square y) )
)
or even:
(defun sum-of-two-squares ( x y / sqaure )
(defun square (x) (* x x) )
(+ (square x) (square y) )
)
v.s.
(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.
-
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.
(defun sum-of-two-squares ( x y )
(+ (* X X)(* Y Y))
)
-
Well neither would i--in the example above--but i needed an example. *lol*
-
I figured as much, but I felt compelled ... now why do I do that ... :)
-
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.)
-
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:
(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
-
Oh no need to be sorry.
Im a moderator I can delete your post if you wish.
-
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.
-
You got it.
-
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?
-
Ok, I removed the post. (I have it saved in a file on my computer if you wish to have it.)
-
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) ]
-
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.
-
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.)
-
Oh ok.(I just saw the edit you added.) Im on it.
-
I really like using lambda however I rarely use it on it's own. Here are two functions I used alot:
(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))
)
)
)
-
How about this?
(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
-
WOOHOO!!!!
Finally someone used Mapcar, Lambda, and Apply all in the same routine. :evil:
In case your wondering I do like your version. :-)
-
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.
-
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.
-
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]
-
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.
-
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.
-
How about this?
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '(lambda (x) (* x x)) lst)))
Compare...
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '* lst lst)))
-
How about this?
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '(lambda (x) (* x x)) lst)))
Compare...
(defun sum-squares-lst (lst) (apply '+ (MAPCAR '* lst lst)))
Hello ElpanovEvgeniy, Welcome to theSwamp !
Sorry, my English it is awful...
Your LISP is VERY good ! Nice code. :-)
-
But here no LAMBDA :-(
-
:cry:
-
:cry:
Here is lambda!
;| 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
-
Here is lambda!
Indeed it is. :-o
Nice stuff Evgeniy, welcome to theswamp.
-
good fun Evgeniy .. that will keep John < Se7en > busy :lol:
-
Now that is lambda. Let me take some time to digest it.
Welcome to the swamp ElpanovEvgeniy.
-
Very elegant contributions ElpanovEvgeniy, welcome to the swamp.
-
Thanks everything, for kind words!
:-)
-
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 "?
-
>Se7en
I compared time of performance
setq + while >> time >> mapcar + lambda
PS.But now, I would write not so...
-
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.
(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...
(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?
-
(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))
-
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