Author Topic: Using a LET construct  (Read 985 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10653
Using a LET construct
« on: April 11, 2023, 01:41:22 PM »
We're doing some mathematic calculations in a program we're developing, and we've gotten a nice function to help in the construction called `sum`.

Code - Auto/Visual Lisp: [Select]
  1. (defun sum (term a next b)
  2.   ;; (sum <term> <lower-bounds> <next> <upper-bounds>)
  3.   ;; EX: (sum 'cube a 'inc b)
  4.   ;; summation of a series of integers
  5.   (if (> a b)
  6.     0
  7.     (+ (eval (list term a))
  8.        (sum term (eval (list next a)) next b))) )

For example, our program is summing the cubes of the integers from 1 to 10 and our functions are taking shape and we are getting the results we want.

Code - Auto/Visual Lisp: [Select]
  1. ;; support procedure
  2. (defun cube (n)
  3.   ;; (cube <integer>)
  4.   ;; Calculates the cube of a number
  5.   (* n n n))
  6.  
  7. (defun sum-cubes (a b)
  8.   ;; (sum-cubes <lower-bounds> <upper-bounds>)
  9.   ;; compute the sum of the cubes of the integers from 1 to 10.
  10.   ;;
  11.   ;; EX: (sum-cubes 1 10)
  12.   (sum 'cube a '1+ b))
  13.  
  14. ;; example:
  15. (sum-cubes 1 10)
  16. > 3025

And we're using our `sum` routine for other things like summing all numbers from 1 to 10:

Code - Auto/Visual Lisp: [Select]
  1. (defun sum-integers (a b)
  2.   ;; (sum-integers <lower-bounds> <upper-bounds>)
  3.   ;; Computes the sum of all the integers in a given range.
  4.   ;;
  5.   ;; EX: (sum-integers 1 10)
  6.   (sum 'eval a '1+ b) )
  7.  
  8. ;; example:  
  9. (sum-integers 1 10)
  10. > 55

And we're even approximating pi:

Code - Auto/Visual Lisp: [Select]
  1. (defun pi-sum (a b)
  2.   (defun pi-term (n) (/ 1.0 (* n (+ n 2))))
  3.   (defun pi-next (n) (+ n 4))
  4.   (sum 'pi-term a 'pi-next b) )
  5.  
  6. ;; example:
  7. (* 8 (pi-sum 1 1000))
  8. > 3.139

But we've started developing a design flaw (an annoyance); instead of having to define formal auxiliary procedures it would be more convenient if we directly write the operations for `pi-term` and `pi-next` inside the `pi-sum` function.

To overcome the need to define `pi-term` and `pi-next` we can obviously preform the job with a few simple `lambda` statements. Normally we'd accomplish that like this:

Code - Auto/Visual Lisp: [Select]
  1. (defun pi-sum (a b)
  2.   (sum
  3.     (lambda (x) (/ 1.0 (* x (+ x 2))))
  4.     a
  5.     (lambda (x) (+ x 4))
  6.     b)
  7.   )

But we have also lost a little descriptvness in our function. And `lambda` itself actually exposes a useful construct we can use to gain that functionality back.

In general lambda has the form of: (lambda (<parameters>) <body>) but `lambda` can also be used like this:
Code: [Select]
(
 (lambda (x y z) (+ x y z))
 1 2 3)

So our "expanded lambda" construct can be defined like this:
Code: [Select]
(
 (lambda (<variable 1> <variable 2>)
   <body>)
   <expression 1>
   <expression 2>
   )
In this form we are essentially binding the values 1, 2, 3 to `x, y, & z` and this is so useful in other lisp dialects a function is associated with the construct called `LET`.

Let is defined as:
Code: [Select]
(let ((<variable 1> <expression 1>)
      (<variable 2> <expression 2>))
  <body>
  )

In AutoLisp we don't have the function `LET` but it is essentially syntactic sugar (the same as) our `expanded lambda` syntax so we can now define `pi-sum` like this:
Code - Auto/Visual Lisp: [Select]
  1. (defun pi-sum (a b)
  2.   (
  3.    (lambda (pi-term pi-next)
  4.      (sum pi-term a pi-next b))
  5.    (lambda (x) (/ 1.0 (* x (+ x 2))))
  6.    (lambda (x) (+ x 4))
  7.    )
  8.   )


I hope you find the simple function lambda as usefull as I do.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

d2010

  • Bull Frog
  • Posts: 326
Re: Using a LET construct
« Reply #1 on: April 29, 2023, 09:26:50 AM »
Hello.. all-setq-s == from source{$N}
How we can transform all -setq/s at Lambda/s this code-source?
Please you do not transform manually , otherwise, with other program-lisp.lsp automatically-mode?
Can you share the "other program-lisp.fas" ? Please you send to me this program .lsp at post-message PM?
 :brow:
I need that program?
Code: [Select]
(defun;;{$N}
 str_pathtemp
      ( / rr e f k) (asserte "A131")
   (setq;|a8424|;
rr nil
f (dfn_var_isstr (getenv "TEMP"))) (if (and  (not rr) f) (setq;|a8533|;
rr f)) (setq;|a8563|;
f (if (not f) (dfn_var_isstr (getenv "TMP")) f)) (if (and  (not rr) f) (setq;|a8674|;
rr f)) (setq;|a8706|;
f (dfn_var_isstr (getvar "TEMPPREFIX"))
f (if (not f) (dfn_var_isstr (getenv "TMP")) f)) (setq;|a8835|;
f (dfn_var_isstr (getvar "XLOADPATH"))
f (if (not f) (dfn_var_isstr (getenv "TMP")) f)) (if (=  rr nil) (setq;|a8989|;
rr "")) (setq;|a9019|;
k (strlen rr)) (if (>  k 2) (progn (while (and  (>  k 1) (=  (substr rr k 1) " ")) (setq;|a9169|;
rr (substr rr 1 (- k 1)))) (progn  (while (and  (>  k 1) (wcmatch rr "*[\\]*")) (if (=  (substr rr k 1) "\\") (setq;|a9369|;
rr (strcat (substr rr 1 (- k 1)) "/" (substr rr (+ k 1))))) (setq;|a9505|;
k (- k 1)))) (setq;|a9549|;
c (substr rr (strlen rr))) (if (/= c "/") (setq;|a9630|;
rr (strcat rr "/"))))) rr)
:wink:


The result must be
Code: [Select]
(Defun  str_pathtemp  ( / rr e f k) (setq rr nil)
(
 (lambda (f)
  (if (and (= rr nil) (dfn_var_isstr (getenv f))) (setq rr f))
)
 "TEMP" "TMP" "TEMPPREFIX"  "XLOADPATH" )
 )
(setq
k (strlen rr)) (if (>  k 2) (progn (while (and  (>  k 1) (=  (substr rr k 1) " ")) (setq;|a9169|;
rr (substr rr 1 (- k 1)))) (progn  (while (and  (>  k 1) (wcmatch rr "*[\\]*")) (if (=  (substr rr k 1) "\\") (setq;|a9369|;
rr (strcat (substr rr 1 (- k 1)) "/" (substr rr (+ k 1))))) (setq;|a9505|;
k (- k 1)))) (setq;|a9549|;
c (substr rr (strlen rr))) (if (/= c "/") (setq;|a9630|;
rr (strcat rr "/"))))) rr)
:wink:
« Last Edit: April 29, 2023, 09:57:01 PM by d2010 »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2145
  • class keyThumper<T>:ILazy<T>
Re: Using a LET construct
« Reply #2 on: April 29, 2023, 06:45:43 PM »
@ d2010

What is the purpose of your post ?
. . . and why is it posted in this thread ?



Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.