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

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
(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: 10626
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: 10626
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: 10626
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: 10626
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: 10626
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: 10626
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: 10626
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: 10626
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